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;
	}

좋은 웹페이지 즐겨찾기