Example #1
0
	@SuppressWarnings("unchecked")
	public static <K, V> Map.Entry<K, V> maxEntryByValue(Map<K, V> map) {
		Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
		Map.Entry<K, V> cur = it.next();
		K curkey = cur.getKey();
		V curval = cur.getValue();

		while (it.hasNext()) {
			Map.Entry<K, V> next = it.next();
			K nextkey = next.getKey();
			V nextval = next.getValue();
			if (((Comparable<V>)nextval).compareTo(curval) > 0) {
				curkey = nextkey;
				curval = nextval;
			}
		}
		return Maps.<K, V>immutableEntry(curkey, curval);
	}
Example #2
0
	public static <K, V> Map.Entry<K, V> maxEntryByValue(Map<K, V> map, Comparator<? super V> cmp) {
		if (cmp == null) { return maxEntryByValue(map); }
		Iterator<Map.Entry<K, V>> it = map.entrySet().iterator();
		Map.Entry<K, V> cur = it.next();
		K curkey = cur.getKey();
		V curval = cur.getValue();

		while (it.hasNext()) {
			Map.Entry<K, V> next = it.next();
			K nextkey = next.getKey();
			V nextval = next.getValue();
			if (cmp.compare(nextval, curval) > 0) {
				curkey = nextkey;
				curval = nextval;
			}
		}
		return Maps.<K, V>immutableEntry(curkey, curval);
	}