如何使用Java8对List进行排序

x33g5p2x  于2020-09-03 发布在 Java  
字(3.4k)|赞(0)|评价(0)|浏览(4122)

工作中,经常会对集合内的元素进行排序。利用Java8可以很方便的对List内的元素进行排序操作,下面介绍三种情况下的排序方法。

一、基础类型List排序

List中是基础类型,比如Integer, Long 等,排序方法很简单。

//对数字进行排序
List<Integer> nums = Arrays.asList(3,1,5,2,9,8,4,10,6,7);
nums.sort(Comparator.reverseOrder()); //reverseOrder倒序
System.err.println("倒序:"+nums);

nums.sort(Comparator.naturalOrder()); //naturalOrder自然排序即:正序
System.err.println("正序:"+nums);

执行结果:

倒序:[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
正序:[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

二、对象List单属性排序

定义一个动物Animal类,针对List<Animal>中的年龄age进行排序,Animal类定义如下:

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Animal {
    private String name;
    private Integer age;//年龄
	private Integer weight;//体重
}

1.方式一

public static void main(String[] args) {
    List<Animal> dataList = JSON.parseArray("[{\"name\":\"兔子\",\"age\":8,\"weight\":2},{\"name\":\"小狗\",\"age\":3,\"weight\":18},{\"name\":\"猫咪\",\"age\":3,\"weight\":3}]", Animal.class);
    System.out.println("排序前:" + JSON.toJSONString(dataList));
    Collections.sort(dataList, new Comparator<Animal>() {
        @Override
        public int compare(Animal o1, Animal o2) {
            return o1.getAge().compareTo(o2.getAge());
        }
    });
    System.out.println("排序后:" +JSON.toJSONString(dataList));
}

2.方式二

JAVA8的写法,List 接口支持直接使用 sort 该方法,不再需要使用 Collections.sort了

dataList.sort(dataList, new Comparator<Animal>() {
   @Override
   public int compare(Animal o1, Animal o2) {
       return o1.getAge().compareTo(o2.getAge();
   }
});

3.方式三

Lambda写法,JAVA8的写法

dataList.sort((Animal o1, Animal o2)->o1.getAge().compareTo(o2.getAge()));

4.方式四

Lambda写法,JAVA8的写法

dataList.sort((o1, o2)->o1.getAge().compareTo(o2.getAge()));

5.方式五

Lambda写法,JAVA8的写法

dataList.sort(Comparator.comparing(Animal::getAge));

6.方式六

Lambda写法,JAVA8的写法

Comparator<Animal> ageComparator = (o1, o2)->o1.getAge().compareTo(o2.getAge());
dataList.sort(ageComparator);       //按上面配置的顺序取值
dataList.sort(ageComparator.reversed());    //按上面配置的顺序反向取值

执行结果:

排序前:[{"age":8,"name":"兔子","weight":2},{"age":3,"name":"小狗","weight":18},{"age":3,"name":"猫咪","weight":3}]
排序后:[{"age":3,"name":"小狗","weight":18},{"age":3,"name":"猫咪","weight":3},{"age":8,"name":"兔子","weight":2}]

三、对象List多属性排序

需求:多条件排序,先按年龄age排序,再根据体重weight排序

1.方式一

Comparator<Animal> comparator = (o1, o2) -> {
      if (o1.getName().equals(o2.getName())) {
          return o1.getWeight().compareTo(o2.getWeight());
      }
      return o2.getAge().compareTo(o1.getAge());
};
dataList.sort(comparator.reversed());

2.方式二

Comparator<Animal> ageComparator = (o1, o2)->o1.getAge().compareTo(o2.getAge());
Comparator<Animal> weightComparator = (o1, o2)->o1.getWeight().compareTo(o2.getWeight());
dataList.sort(ageComparator.thenComparing(weightComparator));

3.方式三

dataList.sort(Comparator.comparing(Animal::getAge).thenComparing(Animal::getWeight));

执行结果:

排序前:[{"age":8,"name":"兔子","weight":2},{"age":3,"name":"小狗","weight":18},{"age":3,"name":"猫咪","weight":3}]
排序后:[{"age":3,"name":"猫咪","weight":3},{"age":3,"name":"小狗","weight":18},{"age":8,"name":"兔子","weight":2}]

相关文章