private final static String YELLOW = "1";
private final static String WHITE = "2";
private final static String BLACK = "3";
@Data
@AllArgsConstructor
class Person{
private String name;
private Integer age;
private String color;
}
@Test
public void streamTestMethod(){
List<Person> personList = new ArrayList<>();
personList.add(new Person("周星驰",55,YELLOW));
personList.add(new Person("维塔斯",48,WHITE));
personList.add(new Person("路德金",97,BLACK));
personList.add(new Person("陈奕迅",65,YELLOW));
personList.add(new Person("伍佰",56,YELLOW));
List<Person> orderByAge = personList.stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList());
List<Person> orderByAgeSecond = personList.stream().sorted(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getAge() - o2.getAge();
}
}).collect(Collectors.toList());
Map<String, List<Person>> colorMap = personList.stream().collect(Collectors.groupingBy(Person::getColor));
colorMap.forEach(
(col,colList) -> {
log.info("颜色{}对应person列表为{}",col,colList);
}
);
List<Person> gtList = personList.stream().filter(person -> person.getAge() > 65).collect(Collectors.toList());
List<String> nameList = personList.stream().map(person -> person.getName()).collect(Collectors.toList());
List<Person> collect = personList.stream().distinct().collect(Collectors.toList());
Integer current = 1;
Integer size = 10;
List<Person> pageList = personList.stream().skip((current - 1) * size).limit(size).collect(Collectors.toList());
Map<String, String> nameColorMap = personList.stream().collect(Collectors.toMap(Person::getName, Person::getColor));
}