Map按照Value排序(升序,降序)---(Comparator)

本文介绍如何在Java开发中使用Comparator对Map的Value进行排序,包括升序和降序排列。内容涉及在Map中Value为double类型时的排序方法,以及对上篇关于String类型Value排序的补充。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发的时候,写一个搜索逻辑,用到好多排序,因为用到了map,数据都放在了map中,这时候需要排序才能用,就有了,这个排序:Map按照Value排序(升序,降序)


public class mapValueCompareSort {
	    public static void main(String[] args) {  
	  
	        HashMap<String,Double> mapDesc = new HashMap<String,Double>();  
	        ValueComparator bvcDESC =  new ValueComparator(mapDesc);  
	        TreeMap<String,Double> sorted_map_Desc = new TreeMap<String,Double>(bvcDESC);  
	  
	        mapDesc.put("A",99.5);  
	        mapDesc.put("B",67.4);  
	        mapDesc.put("C",67.4);  
	        mapDesc.put("D",67.3); 
	        mapDesc.put("E",11.0);
	        mapDesc.put("F",-0.1);
	        mapDesc.put("G",0.1);
	        mapDesc.put("H",-99.1);
	  
	        System.out.println("降序=====unsorted map: "+mapDesc);  
	  
	        sorted_map_Desc.putAll(mapDesc);  
	  
	        System.out.println("降序=====results: "+sorted_map_Desc);  
	        
	    	HashMap<String,Double> mapAsc = new H
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值