Java stream 排序
数字排序List<Student> students = new ArrayList<>();Student student1 = new Student("六年级","Math","小张",1D);students.add(student1);Student student2 = new Student("三年级上","English","小钱",8D);students
·
数字排序
public static void main(String[] args) {
List<Person> userList = new ArrayList<>();
userList.add(new Person("1",25));
userList.add(new Person("2",41));
userList.add(new Person("33",30));
userList.add(new Person("11",100));
userList.add(new Person("55",30));
userList.add(new Person("22",35));
List<Person> collect =
userList.stream().sorted(Comparator.comparing(Person::getAge).reversed()).collect(Collectors.toList());
System.out.println(collect);
List<Person> collect2 =
userList.stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList());
System.out.println(collect2);
}
控制台输出:
[Person(name=11, age=100), Person(name=2, age=41), Person(name=22, age=35), Person(name=33, age=30), Person(name=55, age=30), Person(name=1, age=25)]
[Person(name=1, age=25), Person(name=33, age=30), Person(name=55, age=30), Person(name=22, age=35), Person(name=2, age=41), Person(name=11, age=100)]
以中文排序
public static void main(String[] args) {
List<Person> userList = new ArrayList<>();
userList.add(new Person("1",25));
userList.add(new Person("2",41));
userList.add(new Person("33",30));
userList.add(new Person("11",100));
userList.add(new Person("55",30));
userList.add(new Person("22",35));
List<Person> collect3 =
userList.stream().sorted(Comparator.comparing(Person::getName)).collect(Collectors.toList());
System.out.println(collect3);
List<Person> collect4 = userList.stream().sorted((e1, e2) -> {
return Integer.compare(Integer.parseInt(e1.getName()), Integer.parseInt(e2.getName()));
}).collect(Collectors.toList());
System.out.println(collect4);
}
控制台输出:
[Person(name=1, age=25), Person(name=11, age=100), Person(name=2, age=41), Person(name=22, age=35), Person(name=33, age=30), Person(name=55, age=30)]
[Person(name=1, age=25), Person(name=2, age=41), Person(name=11, age=100), Person(name=22, age=35), Person(name=33, age=30), Person(name=55, age=30)]
排序字段为null时
List<Student> students = new ArrayList<>();
Student student1 = new Student("六年级","Math","小张",1D);
students.add(student1);
Student student2 = new Student("三年级上","English","小钱",8D);
students.add(student2);
Student student3 = new Student(null,"Chinese","小孙",6D);
students.add(student3);
List<Student> collect = students.stream().sorted(Comparator.comparing(Student::getGrade).reversed()).collect(Collectors.toList());
控制台:
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.compareTo(String.java:1155)
at java.lang.String.compareTo(String.java:111)
at java.util.Comparator.lambda$comparing$77a9974f$1(Comparator.java:469)
at java.util.Collections$ReverseComparator2.compare(Collections.java:5185)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:360)
at java.util.TimSort.sort(TimSort.java:220)
at java.util.Arrays.sort(Arrays.java:1512)
at java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:348)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:483)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:472)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.hbis.ttie.mal.rest.Test2.main(Test2.java:29)
更多推荐
已为社区贡献1条内容
所有评论(0)