`
yaerfeng1989
  • 浏览: 225513 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

java map按照value值来比较大小并且返回最终结果

阅读更多

代码下载地址:http://www.zuidaima.com/share/1830834176347136.htm

原文:java map按照value值来比较大小并且返回最终结果

package com.zuidaima.util;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class ValueComparator implements Comparator<Long> {

	Map<Long, Double> base;

	public ValueComparator(Map<Long, Double> base) {
		this.base = base;
	}

	public int compare(Long a, Long b) {
		if (base.get(a).doubleValue() >= base.get(b).doubleValue()) {
			return -1;
		} else {
			return 1;
		}
	}

	public static <K, V extends Comparable<V>> Map<K, V> sortByValues(
			final Map<K, V> map) {
		Comparator<K> valueComparator = new Comparator<K>() {
			public int compare(K k1, K k2) {
				int compare = map.get(k2).compareTo(map.get(k1));
				if (compare == 0)
					return 1;
				else
					return compare;
			}
		};
		Map<K, V> sortedByValues = new TreeMap<K, V>(valueComparator);
		sortedByValues.putAll(map);
		return sortedByValues;
	}

	public static void main(String[] args) {
		HashMap<Long, Double> map = new HashMap<Long, Double>();
		map.put(1l, 99.5);
		map.put(2l, 67.2);
		map.put(3l, 67.5);
		map.put(4l, 67.6);

		ValueComparator bvc = new ValueComparator(map);
		TreeMap<Long, Double> sorted_map = new TreeMap<Long, Double>(bvc);

		System.out.println("unsorted map: " + map);

		sorted_map.putAll(map);

		System.out.println("results: " + sorted_map);

		Map<Long, Double> sorted_map2 = sortByValues(map);
		
		System.out.println("results2: " + sorted_map2);
	}
}

输出结果:

unsorted map: {1=99.5, 2=67.2, 3=67.5, 4=67.6}
results: {1=99.5, 4=67.6, 3=67.5, 2=67.2}
results2: {1=99.5, 4=67.6, 3=67.5, 2=67.2}

 

通过key比较大小的原理类似,大家改写下就好了。enjoy it。

2
0
分享到:
评论
2 楼 mfkvfn 2014-06-05  
按key排序,直接用TreeMap就可以了。这个默认key升序排列的排序树。
如果要按key降序排列。用
Map<Long,Double> map=new TreeMap(new Comparator<Long>(){
  public int compare(Long o1, Long o2){
      return o2-o1;
  }
});
map.put(1l, 99.5);  
map.put(2l, 67.2);  
map.put(3l, 67.5);  
map.put(4l, 67.6);  
System.out.println(result); //出来的结果已经按key降序排列了
1 楼 canghailan 2014-06-05  
package test;

import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
	public static void main(String[] args) {
		HashMap<Long, Double> map = new HashMap<Long, Double>();
		map.put(1l, 99.5);
		map.put(2l, 67.2);
		map.put(3l, 67.5);
		map.put(4l, 67.6);
		System.out.println(map);

		Map.Entry<Long, Double>[] temp = toArray(map);
		sortByValue(temp);
		Map<Long, Double> result = toMap(temp);
		System.out.println(result);
	}

	private static <K, V extends Comparable<V>> Map.Entry<K, V>[] toArray(
			Map<K, V> map) {
		return map.entrySet().toArray(new Map.Entry[map.size()]);
	}

	private static <K, V extends Comparable<V>> void sortByValue(
			Map.Entry<K, V>[] array) {
		Arrays.sort(array, new Comparator<Map.Entry<K, V>>() {
			public int compare(Entry<K, V> o1, Entry<K, V> o2) {
				return o2.getValue().compareTo(o1.getValue());
			}
		});
	}

	private static <K, V extends Comparable<V>> Map<K, V> toMap(
			Map.Entry<K, V>[] array) {
		Map<K, V> result = new LinkedHashMap<K, V>(array.length);
		for (Map.Entry<K, V> entry : array) {
			result.put(entry.getKey(), entry.getValue());
		}
		return result;
	}
}

相关推荐

Global site tag (gtag.js) - Google Analytics