package com.jeethink.system.domain;
public class Employee {
public Integer age;
public String name;
public Integer getAge() {
return this.age;
}
public void setAge(final Integer age) {
this.age = age;
}
public String getName() {
return this.name;
}
public void setName(final String name) {
this.name = name;
}
public Employee(final Integer age, final String name) {
this.age = age;
this.name = name;
}
@Override
public String toString() {
return "Employee{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
1.排序
1.1
List<Employee> employees = Arrays.asList(new Employee(1,"小强")
,new Employee(2,"小唐")
,new Employee(3,"小李")
,new Employee(40,"小心")
,new Employee(1,"比尔鸭"));
Collections.sort(employees,(x, y)->{
if (x.getAge()==y.getAge()) {
return x.getName().compareTo(y.getName());
}else {
return x.getAge().compareTo(y.getAge());
}
});
employees.stream().sorted((x,y)->{
return x.getAge().compareTo(y.getAge());
}).collect(Collectors.toList());
1.2 取某个字段的集合
List<String> collect = employees.stream().map(employee -> employee.getName()).collect(Collectors.toList());
1.3 根据条件过滤
employees.stream().filter(employee -> employee.getAge()>1).map(employee -> employee.getName()).forEach(System.out::println);
1.4 根据一个字段分组
Map<Integer, List<Employee>> collect1 = employees.stream().collect(Collectors.groupingBy(employee -> employee.getAge()));
1.5对一个集合中的一个字段进行求和
int ageSum = employees.stream().collect(Collectors.summingInt(Employee::getAge));
Map<String,String> userMap1 = userList.stream().collect(Collectors.toMap(User::getId,User::getName));
在这里插入代码片