Java Map 集合根据value复杂排序


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

public class TestMap {

    public static void main(String[] args) {

        Map<String, List<Map<String, String>>> testMap = new TreeMap<String, List<Map<String, String>>>();
        init(testMap);
        System.out.println("start=============1");
        for(Map.Entry< String,List<Map<String, String>>> temp : testMap.entrySet()) {
            System.out.println(temp.getKey()+"==" + temp.getValue());
        }
        System.out.println("end=============1");
        List<Entry< String,List<Map<String, String>>>> list = new ArrayList<Entry<String, List<Map<String, String>>>>(testMap.entrySet()); 
       
        System.out.println("start=============2");
        for(Entry< String,List<Map<String, String>>> temp : list) {
            System.out.println(temp.getKey()+"==" + temp.getValue());
        }
        System.out.println("end=============2");
        Collections.sort(list, new Comparator<Map.Entry<String,List<Map<String, String>>>>() {
            @Override
            public int compare(Entry<String, List<Map<String, String>>> o1,
                    Entry<String, List<Map<String, String>>> o2) {
                // TODO Auto-generated method stub
                List<Map<String, String>> first = o1.getValue();
                List<Map<String, String>> second = o2.getValue();
                
                if ((first == null || first.size() == 0) && (second == null || second.size() == 0)) {
                    return 0;
                }

                if ((first == null || first.size() == 0) && (second != null && second.size() > 0)) {
                    return -1;
                }

                if ((first != null && first.size() > 0) && (second == null || second.size() == 0)) {
                    return 1;
                }
                //first.
                Collections.sort(first,  new Comparator<Map<String, String>>() {

                    @Override
                    public int compare(Map<String, String> first, Map<String, String> second) {
                        // TODO Auto-generated method stub
                        if ((first == null || first.size() == 0) && (second == null || second.size() == 0)) {
                            return 0;
                        }

                        if ((first == null || first.size() == 0) && (second != null && second.size() > 0)) {
                            return -1;
                        }

                        if ((first != null && first.size() > 0) && (second == null || second.size() == 0)) {
                            return 1;
                        }
                        String strPre = "pre";
                        if (first.containsKey(strPre) && first.get(strPre) != null
                                && (!second.containsKey(strPre) || second.get(strPre) == null || second.get(strPre).trim() == "")) {
                            return -1;
                        }

                        if (second.containsKey(strPre) && second.get(strPre) != null
                                && (!first.containsKey(strPre) || first.get(strPre) == null || first.get(strPre).trim() == "")) {
                            return 1;
                        }

                        Double one = Double.valueOf(first.get("pre"));
                        Double two = Double.valueOf(second.get("pre"));
                        if (one > two) {
                            return 1;
                        }
                        if (one < two) {
                            return -1;
                        }
                        return 0;
                    }
                });
                
                Collections.sort(second, new Comparator<Map<String, String>>() {

                    @Override
                    public int compare(Map<String, String> first, Map<String, String> second) {
                        // TODO Auto-generated method stub
                        if ((first == null || first.size() == 0) && (second == null || second.size() == 0)) {
                            return 0;
                        }

                        if ((first == null || first.size() == 0) && (second != null && second.size() > 0)) {
                            return -1;
                        }

                        if ((first != null && first.size() > 0) && (second == null || second.size() == 0)) {
                            return 1;
                        }
                        String strPre = "pre";
                        if (first.containsKey(strPre) && first.get(strPre) != null
                                && (!second.containsKey(strPre) || second.get(strPre) == null || second.get(strPre).trim() == "")) {
                            return -1;
                        }

                        if (second.containsKey(strPre) && second.get(strPre) != null
                                && (!first.containsKey(strPre) || first.get(strPre) == null || first.get(strPre).trim() == "")) {
                            return 1;
                        }

                        Double one = Double.valueOf(first.get("pre"));
                        Double two = Double.valueOf(second.get("pre"));
                        if (one > two) {
                            return 1;
                        }
                        if (one < two) {
                            return -1;
                        }

                        return 0;
                    }
                });
                
                Map<String, String> firstMap = first.get(0);
                Map<String, String> secondMap = second.get(0);
                if ((firstMap == null || firstMap.size() == 0) && (secondMap == null || secondMap.size() == 0)) {
                    return 0;
                }

                if ((firstMap == null || firstMap.size() == 0) && (secondMap != null && secondMap.size() > 0)) {
                    return -1;
                }

                if ((firstMap != null && firstMap.size() > 0) && (secondMap == null || secondMap.size() == 0)) {
                    return 1;
                }
                String strPre = "pre";
                if (firstMap.containsKey(strPre) && firstMap.get(strPre) != null && (!secondMap.containsKey(strPre)
                        || secondMap.get(strPre) == null || secondMap.get(strPre).trim() == "")) {
                    return -1;
                }

                if (secondMap.containsKey(strPre) && secondMap.get(strPre) != null && (!firstMap.containsKey(strPre)
                        || firstMap.get(strPre) == null || firstMap.get(strPre).trim() == "")) {
                    return 1;
                }

                Double one = Double.valueOf(firstMap.get("pre"));
                Double two = Double.valueOf(secondMap.get("pre"));
                if (one > two) {
                    return 1;
                }
                if (one < two) {
                    return -1;
                }
                
                if (one == two) {
                  //如果值相等,则比较key值
                    //return -1;
                    String key1 = o1.getKey();
                    String key2 = o2.getKey();
                    return key1.compareTo(key2);
                }
                return 0;
            }
        });
        
        
        System.out.println("start=============3");
        for(Entry< String,List<Map<String, String>>> temp : list) {
            System.out.println(temp.getKey()+"==" + temp.getValue());
        }
        System.out.println("end=============3");
    }

    
    
    public static void init(Map<String, List<Map<String, String>>> map) {
        List<Map<String, String>> temp = new ArrayList<Map<String, String>>();
        Map<String, String> tempMap01 = new HashMap<String,String>();
        tempMap01.put("pre", "0.1");
        Map<String, String>  tempMap02 = new HashMap<String,String>();
        tempMap02.put("pre", "0.9");
        temp.add(tempMap01);
        temp.add(tempMap02);
        map.put("aaaaa", temp);
        
        
        temp = new ArrayList<Map<String, String>>();
        tempMap01 = new HashMap<String,String>();
        tempMap01.put("pre", "0.01");
        tempMap02 = new HashMap<String,String>();
        tempMap02.put("pre", "0.001");
        temp.add(tempMap01);
        temp.add(tempMap02);
        map.put("uuuuu", temp);
        
        temp = new ArrayList<Map<String, String>>();
        tempMap01 = new HashMap<String,String>();
        tempMap01.put("pre", "0.01");
        tempMap02 = new HashMap<String,String>();
        tempMap02.put("pre", "0.001");
        temp.add(tempMap01);
        temp.add(tempMap02);
        map.put("bbbbbbb", temp);
        
        temp = new ArrayList<Map<String, String>>();
        tempMap01 = new HashMap<String,String>();
        tempMap01.put("pre", "1.01");
        tempMap02 = new HashMap<String,String>();
        tempMap02.put("pre", "2.001");
        temp.add(tempMap01);
        temp.add(tempMap02);
        map.put("ppppppp", temp);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值