검색해보니 여러가지 방법이 있는데 그 중에서 가장 간단한 방법 기록용 -ㅅ -;;
max값이 여러개 일 때 여러 key를 list로 받는 방법도 있으므로 참고.
Key max_key = Collections.max(map.entrySet(), Map.Entry.comparingByValue()).getKey();
Comparator를 활용하는 방법
Comparator<Entry<Long, Integer>> comparator = new Comparator<Entry<Long, Integer>>() {
@Override
public int compare(Entry<Long, Integer> e1, Entry<Long, Integer> e2) {
if (e1.getValue() == e2.getValue())
return e1.getKey().compareTo(e2.getKey());
else
return e1.getValue().compareTo(e2.getValue());
}
};
Entry<Long, Integer> pe = Collections.max(plus.entrySet(), comparator);
'프로그래밍 > JAVA' 카테고리의 다른 글
[==null 의문] 대체 왜..? + ArrayList의 삭제 연산 (0) | 2021.11.13 |
---|---|
퀵 정렬 Quick Sort에 대하여 (0) | 2021.11.07 |
진수를 활용한 경우의 수 구하기 (0) | 2021.11.05 |
Math.pow 보다 long형에 직접 지수 값을 계산하는 것이 나은 이유 (0) | 2021.11.02 |
댓글