Stream
作者:互联网
元素流在管道中经过中间操作(intermediate operation)的处理,最后由最终操作(terminal operation)得到前面处理的结果,筛选,求和, 排序,聚合
- 实体类
public class Employee {
/** 热词ID(主键ID) */
private Long id;
/** 姓名 */
private String name;
/** 工资 */
private Integer salary;
/** 部门 */
private Integer deptId;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSalary() {
return salary;
}
public void setSalary(Integer salary) {
this.salary = salary;
}
public Integer getDeptId() {
return deptId;
}
public void setDeptId(Integer deptId) {
this.deptId = deptId;
}
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", salary=" + salary +
", deptId=" + deptId +
'}';
}
public Employee(Long id, String name, Integer salary, Integer deptId) {
this.id = id;
this.name = name;
this.salary = salary;
this.deptId = deptId;
}
}
Employee e1 = new Employee(7369L, "jason", 100, 20);
Employee e2 = new Employee(7499L, "冯小兴", 200, 30);
Employee e3 = new Employee(7521L, "冯帅帅", 300, 30);
Employee e4 = new Employee(7782L, "冯佳兴", 400, 20);
Employee e5 = new Employee(7876L, "温小哲", 500, 20);
// 添加到集合中
List<Employee> employeeList = Arrays.asList(e1, e2, e3, e4, e5);
foreach 用法
// 打印每个每个对象所属的部门
employeeList.stream().forEach(Employee-> Employee.getDeptId());
// 打印每个对象
employeeList.stream().forEach(Employee-> System.out.println(Employee));
map方法
// 获取员工所有的名字
List<String> collect = employeeList.stream().map(employee -> employee.getName()).collect(Collectors.toList());
collect.stream().forEach(System.out::println);
mapToInt/mapToLong/mapToDouble方法,这几个方法主要用来对stream流中的元素产生单个的统计结果
int sum = employeeList.stream().mapToInt(employee -> employee.getSalary()).sum();
System.out.println("薪水总和"+ sum);
filter 筛选方法 :根据设置的条件对stream流中的数据做过滤操作
List<Employee> salaryCollection= employeeList.stream()
.filter(employee -> employee.getSalary() > 100)
.collect(Collectors.toList());
sorted 排序
/**按薪资排序 ,降序 reversed */
List<Employee> collect1 = employeeList.stream()
.sorted(Comparator.comparing(Employee::getSalary).reversed())
.collect(Collectors.toList());
System.out.println("先按薪资排序,在按部门排序");
List<Employee> collect2 = employeeList.stream()
.sorted(Comparator.comparing(Employee::getSalary).reversed().thenComparing(Employee::getDeptId))
.collect(Collectors.toList());
Collectors类,Collectors 类实现了很多归约操作,例如将流转换成集合和聚合元素。Collectors 可用于返回列表或字符串。
//按员工所属部门号进行分类,分组操作
Map<Integer, List<Employee>> map = employeeList.stream()
.collect(Collectors.groupingBy(employee -> employee.getDeptId()));
System.out.println("按员工所属部门号进行分类");
for(Map.Entry<Integer, List<Employee>> entry : map.entrySet()) {
System.out.println("key: " + entry.getKey() + " value:" + entry.getValue());
}
/**
* List -> Map
* 需要注意的是:
* toMap 如果集合对象有重复的key,会报错Duplicate key ....
* user1,user2的id都为1。
* 可以用 (k1,k2)->k1 来设置,如果有重复的key,则保留key1,舍弃key2
*
* list转map的时候有时候会将date类型作为key,实际情况中使用string的多,我们可以将某个字段转成string
*/
Map<Long, Employee> userMap = employeeList.stream().collect(Collectors.toMap(Employee::getId, a -> a,(k1,k2)->k1));
System.out.println(userMap);
min 和 max函数
// 最少的工资
Integer minSlary = employeeList.stream().map(Employee::getSalary).min(Integer::compareTo).get();
// 最多工资
Integer maxSlary = employeeList.stream().map(Employee::getSalary).max(Integer::compareTo).get();
去重,distinct
List<Long> idList = new ArrayList<Long>();
idList.add(1L);
idList.add(1L);
idList.add(2L);
List<Long> distinctIdList = idList.stream().distinct().collect(Collectors.toList());
- anyMatch表示,判断的条件里,任意一个元素成功,返回true
- allMatch表示,判断条件里的元素,所有的都是,返回true
- noneMatch跟allMatch相反,判断条件里的元素,所有的都不是,返回true
String name="冯佳兴";
boolean a = employeeList.stream().anyMatch(employee -> employee.getName().equals(name));// true
boolean b = employeeList.stream().allMatch(employee -> employee.getName().equals(name));//false
boolean c = employeeList.stream().noneMatch(employee -> employee.getName().equals(name));//false
待完善!
标签:Stream,stream,employee,employeeList,Employee,Integer,name 来源: https://blog.csdn.net/fjxcsdn/article/details/110007667