ハッシュの値でソートするには

ハッシュの値でソートする

以下の関数を適当にコピーし、不足したクラスを適宜importする。

	public static ArrayList<Map.Entry<String, Integer>> sortHash(HashMap<String, Integer> map){
		ArrayList<Map.Entry<String, Integer>> mapValuesList = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
 
		Collections.sort(mapValuesList, new Comparator<Map.Entry<String, Integer>>() {
			@Override
			public int compare(Map.Entry<String, Integer> entry1, Map.Entry<String, Integer> entry2) {
				return ((Integer) entry2.getValue()).compareTo((Integer) entry1.getValue());
			}
		});
		return mapValuesList;
	}

値でソートしたキーを得るには

		ArrayList<Map.Entry<String, Integer>> sortedList =sortHash(map); // mapがHashMap
		System.out.println(sortedList.get(0).getKey()); // ArrayListの一番最初の項目(0)に最も値の大きいKey/Valueのペアがくる。

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /home/webpark1634/www/yosh/lib/plugins/authplain/auth.php on line 420

Fatal error: Allowed memory size of 536870912 bytes exhausted (tried to allocate 20480 bytes) in /home/webpark1634/www/yosh/inc/ErrorHandler.php on line 76