Stream的排序

public static void main(String[] args) {
    List list = new ArrayList<>();
    list.add(99);
    list.add(99);
    list.add(66);
    list.add(100);
    list.add(55);
    list.add(55);
    list.add(88);
    list.add(88);
    List list1 = list.stream().sorted().collect(Collectors.toList());
    System.out.println("正序" + list1);
    List list2 = list.stream().distinct().sorted().collect(Collectors.toList());
    System.out.println("去重正序" + list2);
    List list3 = list.stream().distinct().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
    System.out.println("去重倒序" + list3);
}

打印结果为:

正序[55, 55, 66, 88, 88, 99, 99, 100]
去重正序[55, 66, 88, 99, 100]
去重倒序[100, 99, 88, 66, 55]

你可能感兴趣的:(【Java】)