java排序

x33g5p2x  于2022-02-09 转载在 Java  
字(11.2k)|赞(0)|评价(0)|浏览(506)

java排序

Map根据key排序

//升序
Map<Integer,String> treeMap = new TreeMap<>();
        treeMap.put(1,"111");
        treeMap.put(8,"888");
        treeMap.put(4,"444");
        treeMap.put(2,"222");
System.out.println(treeMap);  //输出{1=111, 2=222, 4=444, 8=888}

//降序:
 Map<Integer,String> treeMap = new TreeMap<>((o1,o2)->{
            return o2.compareTo(o1);
        });
        treeMap.put(1,"111");
        treeMap.put(8,"888");
        treeMap.put(4,"444");
        treeMap.put(2,"222");
 System.out.println(treeMap);输出{8=888, 4=444, 2=222, 1=111}

Map根据value排序

对value降序:

Map<String, Double> budget = new HashMap<>();
        budget.put("普通账户", 1000.00);
        budget.put("信用账户", 2000.00);
        budget.put("期权账户",500.00);
        budget.put("理财账户", 4000.00);
//jdk8通过stream实现对Map降序排序
   Map<String, Double> sorted = budget
                .entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
//sorted为{理财账户=4000.0, 信用账户=2000.0, 普通账户=1000.0, 期权账户=500.0}

对value降序并只要前俩个:

Map<String, Double> budget = new HashMap<>();
        budget.put("普通账户", 1000.00);
        budget.put("信用账户", 2000.00);
        budget.put("期权账户",500.00);
        budget.put("理财账户", 4000.00);

//jdk8通过stream实现对Map降序排序
   Map<String, Double> sorted = budget
                .entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
                .limit(2)
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
//sorted为{理财账户=4000.0, 信用账户=2000.0}

对value升序:

Map<String, Double> budget = new HashMap<>();
        budget.put("普通账户", 1000.00);
        budget.put("信用账户", 2000.00);
        budget.put("期权账户",500.00);
        budget.put("理财账户", 4000.00);

//jdk8通过stream实现对Map升序排序
        Map<String, Double> sorted = budget
                .entrySet()
                .stream()
                .sorted(Map.Entry.comparingByValue(Comparator.naturalOrder()))
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e2, LinkedHashMap::new));
//sorted为{期权账户=500.0, 普通账户=1000.0, 信用账户=2000.0, 理财账户=4000.0}

List< Map>根据map里某一value排序

方式一:

List<Map<String, Object>> resultlist = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("id", "1");
        map1.put("name", "张三");
        map1.put("Score", 86.5);
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("id", "2");
        map2.put("name", "李四");
        map2.put("Score", 90.0);
        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("id", "3");
        map3.put("name", "王五");
        map3.put("Score", 70.5);

        resultlist.add(map1);
        resultlist.add(map2);
        resultlist.add(map3);

        Collections.sort(resultlist, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Double one = (Double) o1.get("Score");
                Double two = (Double) o2.get("Score");
                return one.compareTo(two);  //one.compareTo(two)为升序,two.compareTo(one)为降序
            }
        });

        System.out.println(resultlist);
//输出[{Score=70.5, name=王五, id=3}, {Score=86.5, name=张三, id=1}, {Score=90.0, name=李四, id=2}]

方式二:

List<Map<String, Object>> resultlist = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("id", "1");
        map1.put("name", "张三");
        map1.put("Score", 86.5);
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("id", "2");
        map2.put("name", "李四");
        map2.put("Score", 90.0);
        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("id", "3");
        map3.put("name", "王五");
        map3.put("Score", 70.5);

        resultlist.add(map1);
        resultlist.add(map2);
        resultlist.add(map3);
        Collections.sort(resultlist, (o1, o2) -> {
            Double one =  Double.valueOf(o1.get("Score").toString()) ;
            Double two =  Double.valueOf(o2.get("Score").toString()) ;
            return one.compareTo(two);  //one.compareTo(two)为升序,two.compareTo(one)为降序
        });
        System.out.println(resultlist);
//输出[{Score=70.5, name=王五, id=3}, {Score=86.5, name=张三, id=1}, {Score=90.0, name=李四, id=2}]

List< Object>根据Object的某一属性对List进行排序

升序:

List<User> userList = new ArrayList<>(Arrays.asList(
                new User(1L,new BigDecimal("10"),18),
                new User(2L,new BigDecimal("20"),19),
                new User(3L,new BigDecimal("100"),17)));
        System.out.println("排序前"+userList);

        userList = userList.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());

        System.out.println("排序后"+userList);

输出结果

排序前[User{id=1, money=10, age=18}, User{id=2, money=20, age=19}, User{id=3, money=100, age=17}]
排序后[User{id=3, money=100, age=17}, User{id=1, money=10, age=18}, User{id=2, money=20, age=19}]

倒序:

List<User> userList = new ArrayList<>(Arrays.asList(
                new User(1L,new BigDecimal("10"),18),
                new User(2L,new BigDecimal("20"),19),
                new User(3L,new BigDecimal("100"),17)));
        System.out.println("排序前"+userList);

        userList = userList.stream().sorted(Comparator.comparing(User::getAge).reversed()).collect(Collectors.toList());

        System.out.println("排序后"+userList);

输出结果

排序前[User{id=1, money=10, age=18}, User{id=2, money=20, age=19}, User{id=3, money=100, age=17}]
排序后[User{id=2, money=20, age=19}, User{id=1, money=10, age=18}, User{id=3, money=100, age=17}]

多条件排序:

List<User> userList = new ArrayList<>(Arrays.asList(
                new User(1L,new BigDecimal("10"),18),
                new User(2L,new BigDecimal("20"),19),
                new User(3L,new BigDecimal("100"),18)));
        System.out.println("排序前"+userList);

        userList = userList.stream().sorted(Comparator.comparing(User::getAge).thenComparing(User::getId)).collect(Collectors.toList());

        System.out.println("排序后"+userList);

输出结果

排序前[User{id=1, money=10, age=18}, User{id=2, money=20, age=19}, User{id=3, money=100, age=18}]
排序后[User{id=1, money=10, age=18}, User{id=3, money=100, age=18}, User{id=2, money=20, age=19}]

复杂List<Map<String, List<Map<String, Object>>>>排序实例

要求:不仅内部要根据Score排序,整个List也要根据Score排序

List<Map<String, List<Map<String, Object>>>> tagsList = new ArrayList<>();

        List<Map<String, Object>> resultlist1 = new ArrayList<>();
        Map<String, List<Map<String, Object>>> listMap1 = new HashMap<>();
        Map<String, Object> map1 = new HashMap<String, Object>();
        map1.put("id", "1");
        map1.put("name", "张一");
        map1.put("Score", 7L);
        Map<String, Object> map2 = new HashMap<String, Object>();
        map2.put("id", "2");
        map2.put("name", "李一");
        map2.put("Score", 100L);
        Map<String, Object> map3 = new HashMap<String, Object>();
        map3.put("id", "3");
        map3.put("name", "王一");
        map3.put("Score", 86L);
        Map<String, Object> map4 = new HashMap<String, Object>();
        map4.put("id", "4");
        map4.put("name", "赵一");
        map4.put("Score", 0L);
        resultlist1.add(map1);
        resultlist1.add(map2);
        resultlist1.add(map3);
        resultlist1.add(map4);
        listMap1.put("1", resultlist1);
        tagsList.add(listMap1);

        List<Map<String, Object>> resultlist2 = new ArrayList<>();
        Map<String, List<Map<String, Object>>> listMap2 = new HashMap<>();
        Map<String, Object> map2_1 = new HashMap<String, Object>();
        map2_1.put("id", "1");
        map2_1.put("name", "张二");
        map2_1.put("Score", 709L);
        Map<String, Object> map2_2 = new HashMap<String, Object>();
        map2_2.put("id", "2");
        map2_2.put("name", "李二");
        map2_2.put("Score", 0L);
        Map<String, Object> map2_3 = new HashMap<String, Object>();
        map2_3.put("id", "3");
        map2_3.put("name", "王二");
        map2_3.put("Score", 456L);
        Map<String, Object> map3_4 = new HashMap<String, Object>();
        map3_4.put("id", "4");
        map3_4.put("name", "赵二");
        map3_4.put("Score", 0L);
        resultlist2.add(map2_1);
        resultlist2.add(map2_1);
        resultlist2.add(map2_3);
        resultlist2.add(map3_4);
        listMap2.put("2", resultlist2);
        tagsList.add(listMap2);

        List<Map<String, Object>> resultlist3 = new ArrayList<>();
        Map<String, List<Map<String, Object>>> listMap3 = new HashMap<>();
        Map<String, Object> map3_1 = new HashMap<String, Object>();
        map3_1.put("id", "1");
        map3_1.put("name", "张三");
        map3_1.put("Score", 9L);
        Map<String, Object> map3_2 = new HashMap<String, Object>();
        map3_2.put("id", "2");
        map3_2.put("name", "李三");
        map3_2.put("Score", 0L);
        Map<String, Object> map3_3 = new HashMap<String, Object>();
        map3_3.put("id", "3");
        map3_3.put("name", "王三");
        map3_3.put("Score", 86L);
        Map<String, Object> map2_4 = new HashMap<String, Object>();
        map2_4.put("id", "4");
        map2_4.put("name", "赵三");
        map2_4.put("Score", 920L);
        resultlist3.add(map3_1);
        resultlist3.add(map3_2);
        resultlist3.add(map3_3);
        resultlist3.add(map2_4);
        listMap3.put("3", resultlist3);
        tagsList.add(listMap3);

        System.out.println("未排序时的tagsList:" + tagsList);

        //先对每一个里的Score降序排序,排序后tagsList里的每一个元素中的List<Map<String, Object>>都是有序的
        for (Map<String, List<Map<String, Object>>> ins : tagsList) {
            for (List<Map<String, Object>> v : ins.values()) {
                Collections.sort(v, (o1, o2) -> {
                    Long one = Long.valueOf(o1.get("Score").toString());
                    Long two = Long.valueOf(o2.get("Score").toString());
                    return two.compareTo(one);
                });
            }
        }
        System.out.println("对每一个Score内部降序排序后的tagsList:" + tagsList);

        Collections.sort(tagsList, (o1, o2) -> {
            Long one = 0L;
            Long two = 0L;
            for (List<Map<String, Object>> v : o1.values()) {
                one = Long.valueOf(v.get(0).get("Score").toString());
                break;
            }
            for (List<Map<String, Object>> v : o2.values()) {
                two = Long.valueOf(v.get(0).get("Score").toString());
                break;
            }
            return two.compareTo(one);
        });
        System.out.println("根据最内层的最大值排序后的tagsList:" + tagsList);

输出结果

未排序时的tagsList:
[{1=[{Score=7, name=张一, id=1}, {Score=100, name=李一, id=2}, {Score=86, name=王一, id=3}, {Score=0, name=赵一, id=4}]}, 
{2=[{Score=709, name=张二, id=1}, {Score=709, name=张二, id=1}, {Score=456, name=王二, id=3}, {Score=0, name=赵二, id=4}]}, 
{3=[{Score=9, name=张三, id=1}, {Score=0, name=李三, id=2}, {Score=86, name=王三, id=3}, {Score=920, name=赵三, id=4}]}]
    
对每一个Score内部降序排序后的tagsList:
[{1=[{Score=100, name=李一, id=2}, {Score=86, name=王一, id=3}, {Score=7, name=张一, id=1}, {Score=0, name=赵一, id=4}]}, 
{2=[{Score=709, name=张二, id=1}, {Score=709, name=张二, id=1}, {Score=456, name=王二, id=3}, {Score=0, name=赵二, id=4}]}, 
{3=[{Score=920, name=赵三, id=4}, {Score=86, name=王三, id=3}, {Score=9, name=张三, id=1}, {Score=0, name=李三, id=2}]}]
    
根据最内层的最大值排序后的tagsList:
[{3=[{Score=920, name=赵三, id=4}, {Score=86, name=王三, id=3}, {Score=9, name=张三, id=1}, {Score=0, name=李三, id=2}]}, 
{2=[{Score=709, name=张二, id=1}, {Score=709, name=张二, id=1}, {Score=456, name=王二, id=3}, {Score=0, name=赵二, id=4}]}, 
{1=[{Score=100, name=李一, id=2}, {Score=86, name=王一, id=3}, {Score=7, name=张一, id=1}, {Score=0, name=赵一, id=4}]}]

相关文章