欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Map按單個或多個Value排序當(dāng)Value相同時按Key排序

 更新時間:2023年02月03日 15:13:26   作者:向著百萬年薪努力的小趙  
Map可以先按照value進(jìn)行排序,然后按照key進(jìn)行排序。 或者先按照key進(jìn)行排序,然后按照value進(jìn)行排序,這樣操作都行,這篇文章主要介紹了Map按單個或多個Value排序,當(dāng)Value相同時按Key排序,需要的朋友可以參考下

Map可以先按照value進(jìn)行排序,然后按照key進(jìn)行排序。 或者先按照key進(jìn)行排序,然后按照value進(jìn)行排序,這都是可以的。

并且,大家可以制定自己的排序規(guī)則。
按單個value排序:

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
 
import static java.util.Map.Entry.comparingByValue;
import static java.util.stream.Collectors.toMap;
 
public class SortTest {
 
    public static void main(String[] args) throws Exception {
 
        // 創(chuàng)建一個字符串為Key,數(shù)字為值的map
        Map<String, Integer> budget = new HashMap<>();
        budget.put("clothes", 120);
        budget.put("grocery", 150);
        budget.put("transportation", 100);
        budget.put("utility", 130);
        budget.put("rent", 1150);
        budget.put("miscellneous", 90);
        System.out.println("排序前: " + budget);
 
        // 按值排序 升序
        Map<String, Integer> sorted = budget
                .entrySet()
                .stream()
                .sorted(comparingByValue())
                .collect(
                        toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2,
                                LinkedHashMap::new));
 
        System.out.println("升序按值排序后的map: " + sorted);
 
        // 按值排序降序
        sorted = budget
                .entrySet()
                .stream()
                .sorted(Collections.reverseOrder(comparingByValue()))
                .collect(
                        toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2,
                                LinkedHashMap::new));
 
        System.out.println("降序按值排序后的map: " + sorted);
    }
}

按多個value排序:

data = data.stream().sorted(Comparator.comparing(o -> {
    StringBuffer key = new StringBuffer();
    fieldList.stream().forEach((a)-> {
        key.append(o.get(a)+"");
    });
    return key.toString();
} )).collect(Collectors.toList());

下面的代碼中,首先按照value的數(shù)值從大到小進(jìn)行排序,當(dāng)value數(shù)值大小相同時,再按照key的長度從長到短進(jìn)行排序,這個操作與Stream流式操作相結(jié)合。

    /**
     * Map按照整數(shù)型的value進(jìn)行降序排序,當(dāng)value相同時,按照key的長度進(jìn)行排序
     *
     * @param map
     * @return
     */
    public static LinkedHashMap<String, Integer> sortMap(Map<String, Integer> map) {
        return map.entrySet().stream().sorted(((item1, item2) -> {
            int compare = item2.getValue().compareTo(item1.getValue());
            if (compare == 0) {
                if (item1.getKey().length() < item2.getKey().length()) {
                    compare = 1;
                } else if (item1.getKey().length() > item2.getKey().length()) {
                    compare = -1;
                }
            }
            return compare;
        })).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new));
    }

補(bǔ)充:對Map中的Value進(jìn)行降序排序,當(dāng)Value相同時,按照Key降序排序

package com.ethjava;
import java.util.*;
public class mappaixu1 {
    public static void main(String[] args){
 
        Map<Integer,Integer> hashMap=new HashMap<Integer, Integer>();
        hashMap.put(1,10);
        hashMap.put(5,7);
        hashMap.put(2,9);
        hashMap.put(3,7);
        hashMap.put(3,6);//key是不可重復(fù)的,當(dāng)這里再次輸入Key=3時的,將會覆蓋掉前面的(3,7)
        hashMap.put(4,7);
 
        //遍歷
        for(Map.Entry<Integer,Integer> e:hashMap.entrySet()){
            System.out.println("Key: "+e.getKey()+"對應(yīng)的Value: "+e.getValue());
        }
        //Key: 1對應(yīng)的Value: 10
        //Key: 2對應(yīng)的Value: 9
        //Key: 3對應(yīng)的Value: 6
        //Key: 4對應(yīng)的Value: 7
        //Key: 5對應(yīng)的Value: 7
        //這里為什么自動按照key升序排序輸出???為什么
        // 某夢說,這里是因為湊巧正序輸出,hashMap輸出相對于輸入是無序的。
 
        //下面按照Value進(jìn)行倒序排列
        ArrayList<Map.Entry<Integer,Integer>> arrayList=new ArrayList<Map.Entry<Integer, Integer>>(hashMap.entrySet());
 
        Collections.sort(arrayList,new Comparator<Map.Entry<Integer,Integer>>(){
            @Override
 
            public int compare(Map.Entry<Integer,Integer> o1,Map.Entry<Integer,Integer> o2 ){
                //按照Value進(jìn)行倒序,若Value相同,按照Key正序排序
                //方法1:return o2.getValue() - o1.getValue();
                //方法2:return o2.getValue().compareTo(o1.getValue());//對于Integer,String都是可以應(yīng)用的
                //按照Value進(jìn)行倒序,若Value相同,按照Key倒序排序
                int result = o2.getValue().compareTo(o1.getValue());
                //方法學(xué)習(xí):public int compareTo( NumberSubClass referenceName )
                //referenceName -- 可以是一個 Byte, Double, Integer, Float, Long 或 Short 類型的參數(shù)。
                //返回值:如果指定的數(shù)與參數(shù)相等返回0。
                // 如果指定的數(shù)小于參數(shù)返回 -1。
                //如果指定的數(shù)大于參數(shù)返回 1
                if(result!=0){
                    return result;//即兩個Value不相同,就按照Value倒序輸出
                }else{
                    return o2.getKey().compareTo(o1.getKey());}
                    //若兩個Value相同,就按照Key倒序輸出
            }
        });
        //這里arrayList里的順序已經(jīng)按照自己的排序進(jìn)行了調(diào)整
        for(int i=0;i<arrayList.size();i++){
            System.out.println(arrayList.get(i));
            //方法一和方法二輸出:
            //1=10
            //2=9
            //4=7
            //5=7
            //3=6
            //當(dāng)按照Value倒序排序,但是當(dāng)Value相同時,按照Key順序正序排序
 
            //方法二
            //1=10
            //2=9
            //5=7
            //4=7
            //3=6
            //當(dāng)按照Value倒序輸出,但是當(dāng)Value相同時,按照Key倒序輸出
        }
 
        for(Map.Entry<Integer,Integer> e:hashMap.entrySet()){
 
            System.out.println(e);
            //1=10
            //2=9
            //3=6
            //4=7
            //5=7
            //這里表明hashMap中存取的內(nèi)容順序并沒有進(jìn)行任何改變,改變的是arrayList里的內(nèi)容的順序
        }
    }
}

參考文獻(xiàn):

https://blog.csdn.net/LvJinYang/article/details/102875095

https://blog.csdn.net/u014388729/article/details/80156645

到此這篇關(guān)于Map按單個或多個Value排序,當(dāng)Value相同時按Key排序的文章就介紹到這了,更多相關(guān)Map按單個或多個Value排序內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論