Java List 排序sort 和sorted方法说明

Java List 排序Sort 和Sorted

1、sort:  list.sort 方法是list方法 对原有list 元素顺序位置进行更改排序

  如:

    listP.sort((x1,x2)->x1.getName().compareTo(x2.name));

2、sorted: sorted 方法是对list转换成stream流的方法,不对有有list元素排序,而是返回一个排序后的新list:

    如:

List<Fruit> listP2 = listP.stream().sorted(Comparator.comparing(Fruit::getName).reversed().thenComparing(Fruit::getWeight)).collect(Collectors.toList());

如果想第二个排序字段倒序排可以如下写法
List<Fruit> listP2 = listP.stream().sorted(Comparator.comparing(Fruit::getName).reversed()
                        .thenComparing(Comparator.comparing(Fruit::getWeight).reversed())).collect(Collectors.toList());



下面用多种方式实例进行说明
   try {
            Fruit p1 = new Fruit();
            p1.setName("apple");
            p1.setWeight(8);

            Fruit p2 = new Fruit();
            p2.setName("apple");
            p2.setWeight(13);
            Fruit p3 = new Fruit();
            p3.setName("pear");
            p3.setWeight(10);

            Fruit p4 = new Fruit();
            p4.setName("pear");
            p4.setWeight(16);
            Fruit p5 = new Fruit();
            p5.setName("wang");
            p5.setWeight(10);

            Fruit p6 = new Fruit();
            p6.setName("banana");
            p6.setWeight(2);

            List<Fruit> listP = Arrays.asList(p1, p2, p3, p4, p5, p6);
           // list.sort 对原有list 元素顺序位置进行更改排序
            // lamda表达式实现 Comparator  简要主法
            listP.sort((x1,x2)->x1.getName().compareTo(x2.name));
            // lamda 表达式实现自定义方法
            listP.sort((x1,x2)-> {
              if(x1.getWeight()>x2.getWeight()){
                  return 1;
              } else if(x1.getWeight()<x2.getWeight()){
                  return -1;
              }else{
                  return 0;
              }
            });

            //lamda 表达式 显示实现 comparator方法
            listP.sort(new Comparator<Fruit>() {
                @Override
                public int compare(Fruit o1, Fruit o2) {
                    return o1.getName().compareTo(o2.getName());
                }
            });

            // list sort 实现多字段排序
            Comparator<Fruit> nameComparator=Comparator.comparing(x-> x.getName());
            Comparator<Fruit> weightComparator=Comparator.comparing(x-> x.getWeight());
            listP.sort(nameComparator.thenComparing(weightComparator));


            // list-stream()->sorted 进行排序反回排序后新list,原list元素顺序不变
            List<Fruit> listP2 = listP.stream().sorted(Comparator.comparing(Fruit::getName).reversed().thenComparing(Fruit::getWeight)).collect(Collectors.toList());

        } catch (Exception e) {
            e.printStackTrace();
        }


    }

  3、List<String> list 排序  下面代码实例

  String[] strC = new String[]{"账单优惠", "现金", "账单减免", "扫码支付", "团购", "会员卡", "银行卡"};
        List<String> listCategory = new ArrayList<>();
        listCategory = Arrays.asList(strC);
        List<String> list2 = listCategory.stream().sorted(String::compareTo).collect(Collectors.toList());
        List<String> list3 = listCategory.stream().sorted((x1, x2) ->
        {
            return x1.compareTo(x2);
        }).collect(Collectors.toList());

        Collections.sort(listCategory);
        Collections.reverse(listCategory);

 

posted @ 2022-01-06 09:24  xiaoBai1001  阅读(13609)  评论(0编辑  收藏  举报