自定义Map映射排序

package map;
import java.util.*;
import java.util.Map.*;
public class MapCustomSort{
	public static void main(String args[]) {
		Map<String,Double> map=new HashMap<String,Double>();
		map.put("zlm", 98.6);
		map.put("zjh", 55.9);
		map.put("ycl", 1.1);
		map.put("fs", 68.9);
		map.put("xj", 88.8);
		map.put("whp", 74.5);
		Set<Entry<String,Double>> set=map.entrySet();
		List<Entry<String,Double>> list=new ArrayList<Entry<String,Double>>(set);
		Collections.sort(list,new Comparator<Entry<String,Double>>() {
			public int compare(Entry<String,Double> e1,Entry<String,Double> e2) {
				return e1.getValue().compareTo(e2.getValue());
			}
		});
		Map<String,Double> map2=new LinkedHashMap<String,Double>();
		for(Entry<String,Double> ent:list)
			map2.put(ent.getKey(),ent.getValue());
		for(Entry<String,Double> ent:map2.entrySet())
			System.out.println(ent.getKey()+"  "+ent.getValue());
	}
}

运行结果:
在这里插入图片描述

发布了53 篇原创文章 · 获赞 1 · 访问量 2795

猜你喜欢

转载自blog.csdn.net/weixin_43873198/article/details/103548339