java:map:分别通过key和value进行排序

1.只适用于value格式为字符串 

package testMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

public class TestMap {

	public static void main(String[] args) {
		//1
//		System.out.println(
//			new HashMap<String, Object>(){
//			{put("name", "香港");put("value", "");}
//			
//		} );
		
		
		//2
		Map<String, String> map = new TreeMap<String, String>();

        map.put("KFC", "kfc");
        map.put("WNBA", "wnba");
        map.put("NBA", "nba");
        map.put("CBA", "cba");

        map.put("0", "2.24");
        map.put("1", "10.4");//不能排序10.4 和 1.4
        map.put("2", "1.4");
        map.put("3", "3.6");
        map.put("4", "0.3");
        Map<String, String> resultMap = sortMapByKey(map);    //按Key进行排序
//        Map<String, String> resultMap = sortMapByValue(map); //按Value进行排序

        for (Map.Entry<String, String> entry : resultMap.entrySet()) {
            System.out.println(entry.getKey() + " " + entry.getValue());
        }
		

	}

	
	/**
     * 使用 Map按key进行排序
     * @param map
     * @return
     */
    public static Map<String, String> sortMapByKey(Map<String, String> map) {
        if (map == null || map.isEmpty()) {
            return null;
        }

        Map<String, String> sortMap = new TreeMap<String, String>(
                new MapKeyComparator());

        sortMap.putAll(map);

        return sortMap;
    }
	
	 /**
     * 使用 Map按value进行排序
     * @param oriMap
     * @return
     */
    public static Map<String, String> sortMapByValue(Map<String, String> oriMap) {
        if (oriMap == null || oriMap.isEmpty()) {
            return null;
        }
        Map<String, String> sortedMap = new LinkedHashMap<String, String>();
        List<Map.Entry<String, String>> entryList = new ArrayList<Map.Entry<String, String>>(
                oriMap.entrySet());
        Collections.sort(entryList, new MapValueComparator());

        Iterator<Map.Entry<String, String>> iter = entryList.iterator();
        Map.Entry<String, String> tmpEntry = null;
        while (iter.hasNext()) {
            tmpEntry = iter.next();
            sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
        }
        return sortedMap;
    }
	
}

key排序:

value排序:

key比较器类:

class MapKeyComparator implements Comparator<String>{

    @Override
    public int compare(String str1, String str2) {
        
        return str1.compareTo(str2);
    }
}

value比较器类“” 

class MapValueComparator implements Comparator<Map.Entry<String, String>> {

    @Override
    public int compare(Entry<String, String> me1, Entry<String, String> me2) {

        return me1.getValue().compareTo(me2.getValue());
    }
}

2.适用于value格式为Double格式(同Integer)

package testMap;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

public class TestSortMapForDouble {

	public static void main(String[] args) {
		
		 // 默认情况,TreeMap按key升序排序
	    Map<String, Double> map = new TreeMap<String, Double>();
	    map.put("bac1", 0.1);
	    map.put("acb1", 0.4);
	    map.put("acb100", 0.01);
	    map.put("acb10", 0.3);
	    map.put("bca1", 30.3);
	    map.put("cab1", 80.2);
	    map.put("cba1", 1.3);
	    map.put("abc1", 10.3);
	    map.put("abc2", 12.6699999);
	    Map<String, Double> resultMap =valueUpSort(map) ;

	    for (Map.Entry<String, Double> entry : resultMap.entrySet()) {
            System.out.println(entry.getKey() + "-->" + entry.getValue());
        }
	}

	
	
	public static Map<String, Double> valueUpSort(Map<String, Double> map) {
		List<Map.Entry<String, Double>> wordMap = new ArrayList<Map.Entry<String, Double>>(map.entrySet());
//		System.out.println(wordMap);
		Collections.sort(wordMap, new Comparator<Map.Entry<String, Double>>() {// 根据value排序
		public int compare(Map.Entry<String, Double> o1, Map.Entry<String, Double> o2) {
		double result = o1.getValue() - o2.getValue();//升序降序交换位置即可
		if (result > 0)
		return 1;
		else if (result == 0)
		return 0;
		else
		return -1;
		}
		});
	    System.out.println("------------map按照value升序排序--------------------");
	    Map<String, Double> ma = new LinkedHashMap<String, Double>();
	    for (Map.Entry<String, Double> entry : wordMap) {
//	        System.out.println(entry.getKey() + "->" + entry.getValue());
	        ma.put(entry.getKey(),entry.getValue());
	    }
	    
	    return ma;
	}
}

 参考:https://blog.csdn.net/qinchao2011/article/details/69764708
https://blog.csdn.net/yangyutong0506/article/details/78190245

猜你喜欢

转载自blog.csdn.net/weixin_38750084/article/details/88419041