实现对Map按照value升序排序

/**
	 * @param map
	 * @return 实现对map按照value升序排序
	 */
	@SuppressWarnings("unchecked")
	public static Map.Entry[] getSortedHashtableByValue(Map map) {
		Set set = map.entrySet();
		Map.Entry[] entries = (Map.Entry[]) set.toArray(new Map.Entry[set
				.size()]);
		Arrays.sort(entries, new Comparator() {
			public int compare(Object arg0, Object arg1) {
				Long key1 = Long.valueOf(((Map.Entry) arg0).getValue()
						.toString());
				Long key2 = Long.valueOf(((Map.Entry) arg1).getValue()
						.toString());
				return key1.compareTo(key2);
			}
		});

		return entries;
	}

 

 

/**
	 * 实现对map按照value升序排序
	 * @param map
	 * @return
	 */
	public static List<Entry<Object, Object>> getSortByValue(Map map) {
		Set<Entry<Object, Object>> set = map.entrySet();
		List<Map.Entry<Object, Object>> list = new ArrayList<Map.Entry<Object, Object>>(set);
		Collections.sort(list, new Comparator<Map.Entry<Object, Object>>() {
			public int compare(Map.Entry<Object, Object> obj1,
					Map.Entry<Object, Object> obj2) {
				return ((String) obj1.getKey()).compareTo((String) obj2.getKey());// 按key降排序
				// return (Integer)obj1.getValue() - (Integer)obj2.getValue();//按value升序排序
				// return (Integer)obj2.getValue() - (Integer)obj1.getValue();//按value降序排序
			}
		});
		return list;
	}

 

你可能感兴趣的:(value)