hashmap은value값에 따라 정렬하여 실현
1220 단어 HashMap
static class EntryComparator implements
Comparator<Map.Entry<String, Integer>> {
public EntryComparator() {
}
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2) {
return o2.getValue().intValue() - o1.getValue().intValue();//
}
}
@SuppressWarnings("unchecked")
public static Map<String, Integer> outputSortedHashMap(
Map<String, Integer> map) {
Map<String, Integer> tempMap = new LinkedHashMap<String, Integer>();
List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String, Integer>>(
map.entrySet());
Collections.sort(list, new EntryComparator());
Iterator<Map.Entry<String, Integer>> i = list.iterator();
while (i.hasNext()) {
Map.Entry<String, Integer> entry = i.next();
tempMap.put(entry.getKey(), entry.getValue());
// System.out.println("key=" + entry.getKey() + ", value="+
// entry.getValue());
}
return tempMap;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[프로그래머스 / 완전 탐색] 메뉴 리뉴얼 (c++)문제와 제한 사항이 조금 복잡해서 직접 읽는 것이 더 편할 것이다 이번 문제 역시 직접 값들을 하나하나 찾아야 한다. 문제 해결을 2파트로 나눌 수 있다. 1. orders 배열에 있는 각 주문들이 만들 수 있는 조...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.