prepare
public class PersonDO {
private String name;
private Integer age;
public PersonDO(String name, Integer age) {
this.name = name;
this.age = age;
}
public PersonDO() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "PersonDO{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
private Random r = new Random();
personList = getPersonList(5);
public List<PersonDO> getPersonList(int size) {
List<PersonDO> personList = new ArrayList<>();
for (int i = 0; i < size; i++) {
PersonDO p = genPerson();
personList.add(p);
}
return personList;
}
public PersonDO genPerson() {
String name = genRandomStr(5);
int age = r.nextInt(10) + 15;
return new PersonDO(name, age);
}
extract
List<String> names = personList.stream().map(PersonDO::getName).collect(Collectors.toList());
distinct
List<String> distinctName = personList.stream()
.map(PersonDO::getName)
.distinct()
.collect(Collectors.toList());
grouping
Map<Integer, List<PersonDO>> ageMap = personList.stream().collect(Collectors.groupingBy(PersonDO::getAge));
Map<String, Integer> nameGroupSumAge = personList.stream()
.collect(Collectors.groupingBy(PersonDO::getName, Collectors.summingInt(PersonDO::getAge)));
Map<String, Double> nameGroupAvgAge = personList.stream()
.collect(Collectors.groupingBy(PersonDO::getName, Collectors.averagingInt(PersonDO::getAge)));
Map<String, PersonDO> nameGroupSmallestOne = personList.stream()
.collect(
Collectors.groupingBy(PersonDO::getName,
Collectors.collectingAndThen(
Collectors.minBy(Comparator.comparing(PersonDO::getAge)), Optional::get)
)
);
Map<String, PersonDO> nameGroupBiggestOne = personList.stream()
.collect(
Collectors.groupingBy(PersonDO::getName,
Collectors.collectingAndThen(
Collectors.maxBy(Comparator.comparing(PersonDO::getAge)), Optional::get)
)
);
Map<String, List<PersonDO>> nameGroupSortList = personList.stream()
.collect(
Collectors.groupingBy(PersonDO::getName,
Collectors.collectingAndThen(
Collectors.toList(), groupPersonList -> {
groupPersonList.sort(Comparator.comparing(PersonDO::getAge));
return groupPersonList;
}
)
)
);
sort
List<PersonDO> afterSort = personList.stream().sorted(Comparator.comparing(PersonDO::getAge).thenComparing(PersonDO::getName)).collect(Collectors.toList());
List<PersonDO> afterDescSort = personList.stream().sorted(Comparator.comparing(PersonDO::getAge, (o1,o2)-> o2-o1).thenComparing(PersonDO::getName)).collect(Collectors.toList());
List<Integer> afterSortExtract = personList.stream().sorted(Comparator.comparing(PersonDO::getAge).thenComparing(PersonDO::getName)).map(PersonDO::getAge).collect(Collectors.toList());
filter
List<PersonDO> afterFilter = personList.stream().filter((p) -> (p.getAge() > 18)).collect(Collectors.toList());
List<PersonDO> afterFilter = personList.stream().filter((p) -> (p.getAge() > 18)).limit(2).collect(Collectors.toList());
List<String> afterFilter = personList.stream().filter((p) -> (p.getAge() > 18)).map(PersonDO::getName).collect(Collectors.toList());
transform
Map<String, PersonDO> nameMap = personList.stream().collect(Collectors.toMap(PersonDO::getName, Function.identity(), (k1, k2) -> k2));
Map<Integer, String> ageMap = personList.stream().collect(Collectors.toMap(PersonDO::getAge, PersonDO::getName, (k1, k2) -> k1));