WonYong-Jang / algorithm

0 stars 0 forks source link

Hash ( HashCode ) #8

Open WonYong-Jang opened 6 years ago

WonYong-Jang commented 6 years ago

객체 해시코드 ( HashCode() )

객체의 변수 2개 이상 비교할 경우

static class Node {
        int dx, dy;
        Node(int a, int b) {
            dx=a; dy=b;
        }

        @Override
        public boolean equals(Object obj) {
            if(obj instanceof Node) {
                Node n = (Node)obj;
                if(dx == n.dx && dy == n.dy) return true;
            }
            return false;
        }
        @Override
        public int hashCode() {
            // TODO Auto-generated method stub
            return Objects.hash(dx,dy);
        }
    }

객체 변수 하나만 비교할 경우

static class Node {
        int dx;
        Node(int a) {
            dx=a;
        }

        @Override
        public boolean equals(Object obj) {
            if(obj instanceof Node) {
                Node n = (Node)obj;
                if(dx == n.dx ) return true;
            }
            return false;
        }
        @Override
        public int hashCode() {
            // TODO Auto-generated method stub
            return dx;
        }
    }

참고 : https://tlsdusrms.tistory.com/8

WonYong-Jang commented 4 years ago

관련 문제

hashMap 연습(leetcode)

  1. Maximum Frequency Stack

HashMap 에서 모든 key 와 value를 print

HashMap<String, Integer> map = new HashMap<>();

1) 모든 Key 프린트

Set<String> set = map.keySet();
set.forEach(key -> System.out.println(key));

2) 모든 Value 프린트

Collection<Integer> value = map.values();
value.forEach(v -> System.out.println(v));

3) 모든 key 와 value를 같이 프린트

Set<Map.Entry<String, Integer>> set = map.entrySet(); // import 주의 ( Entry )

for(Entry<String, Integer> cur : set)
{
            System.out.println(cur.getKey());
            System.out.println(cur.getValue());
}

map.forEach((k,v) -> {
            System.out.println(k);
            System.out.println(v);
});

Key 가 존재하는 지 알고 싶은 경우

중복 문자열 갯수 세기 == > getOrDefault 또는 merge

Map<String, Integer> map = new HashMap();
        for (String word: A.split(" "))
            map.put(word, map.getOrDefault(word, 0) + 1);
while(st.hasMoreTokens())
{
            cur = st.nextToken();
            map.merge(cur, 0, (k,v)-> ++v);
}

HashMap 정렬

public HashMap<String, Integer> map = new HashMap<>();
List<String> list = new ArrayList<>(map.keySet());
        Collections.sort(list, new mySort());

참고링크 : https://riptutorial.com/ko/java/example/421/java-8%EC%97%90%EC%84%9C-map%EC%9D%98-%EA%B8%B0%EB%B3%B8-%EB%A9%94%EC%86%8C%EB%93%9C-%EC%82%AC%EC%9A%A9%ED%95%98%EA%B8%B0

WonYong-Jang commented 4 years ago

일반적인 HashMap<String, ArrayList> map

  List<String> list = map.get(values[0]);
  if (list == null) {
    list = new ArrayList<String>();
  }
  list.add(values[1]);
  map.put(values[0], list);

getOrDefault

public void putPhoneNumber(Map<String, List<String>> map, String[] values) {
  List<String> list = map.getOrDefault(values[0], new ArrayList<String>());
  list.add(values[1]);
  map.put(values[0], list);
}

computeIfAbsent

public void putPhoneNumber(Map<String, List<String>> map, String[] values) {
  map.computeIfAbsent(values[0], k -> new ArrayList<String>()).add(values[1]);
}
WonYong-Jang commented 3 years ago

LinkedHashMap

  1. LRU Cache ( 그 외에 다른 방법도 풀기)