最近用到 Java8 新特性时候,发现流函数非常好用,特地总结了一下常用的方法和应用范围,下面通过一个例子来展示
1、实体类代码
package com.test;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Author:LSR
* Date:2020/4/30
* DESCRIPTION:
*/
public class Employee {
private String name;
private int salary;
private String office;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getSalary() {
return salary;
}
public void setSalary(int salary) {
this.salary = salary;
}
public String getOffice() {
return office;
}
public void setOffice(String office) {
this.office = office;
}
public Employee(String name, int salary, String office) {
this.name = name;
this.salary = salary;
this.office = office;
}
@Override
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", salary=" + salary +
", office='" + office + '\'' +
'}';
}
}
2、Java8 流函数各种方法应用测试代码
package com.test;
import java.util.*;
import java.util.stream.Collectors;
public class Java8Test2 {
private static List<Employee> employeeList = new ArrayList<>();
static{
employeeList.add(new Employee("Matt",5000,"New York"));
employeeList.add(new Employee("Steve",6000,"London"));
employeeList.add(new Employee("Carrie",20000,"New York"));
employeeList.add(new Employee("Peter",7000,"New York"));
employeeList.add(new Employee("Pat",8000,"London"));
employeeList.add(new Employee("Tammy",29000,"Shanghai"));
/*employeeList.add(Employee.builder().name("Matt").salary(5000).office("New York").build());
employeeList.add(Employee.builder().name("Steve").salary(6000).office("London").build());
employeeList.add(Employee.builder().name("Carrie").salary(20000).office("New York").build());
employeeList.add(Employee.builder().name("Peter").salary(7000).office("New York").build());
employeeList.add(Employee.builder().name("Pat").salary(8000).office("London").build());
employeeList.add(Employee.builder().name("Tammy").salary(29000).office("Shanghai").build());*/
}
public static void main(String[] args) {
//anyMatch
boolean isMatch = employeeList.stream().anyMatch(employee -> employee.getOffice().equals("London"));
System.out.println(isMatch);
//返回所有salary大于6000
boolean matched = employeeList.stream().allMatch(employee -> employee.getSalary()>4000);
System.out.println("返回所有salary大于6000:"+matched);
//找出工资最高
Optional<Employee> hightestSalary = employeeList.stream().max((e1, e2)->Integer.compare(e1.getSalary(),e2.getSalary()));
System.out.println("找出工资最高:"+hightestSalary);
//返回姓名列表
List<String> names = employeeList.stream().map(employee -> employee.getName()).collect(Collectors.toList());
System.out.println("返回姓名列表:"+names);
//List转换成Map 其中 (k1, k2)->k2 表示最后的值覆盖前面的值可以达到去重目的
Map<String,Employee> employeeMap = employeeList.stream().collect(Collectors.toMap((key->key.getName()),(value->value),(k1, k2)->k2));
employeeMap.forEach((key,value)-> System.out.println(key + "=" + value.toString()));
//统计办公室是New York的个数
long officeCount = employeeList.stream().filter(employee -> employee.getOffice().equals("Shanghai")).count();
System.out.println("统计办公室是New York的个数:"+officeCount);
//List转换为Set
Set<String> officeSet = employeeList.stream().map(employee -> employee.getOffice()).distinct().collect(Collectors.toSet());
System.out.println("List转换为Set:"+officeSet.toString());
//查找办公室地点是New York的员工
Optional<Employee> allMatchedEmployees = employeeList.stream().filter(employee -> employee.getOffice().equals("New York")).findAny();
System.out.println("查找办公室地点是New York的员工:"+allMatchedEmployees.toString());
//按照工资的降序来列出员工信息
List<Employee> sortEmployeeList = employeeList.stream().sorted((e1,e2)->Integer.compare(e2.getSalary(),e1.getSalary())).collect(Collectors.toList());
//按照名字的升序列出员工信息
List<Employee> sortEmployeeByName = employeeList.stream().sorted((e1,e2)->e1.getName().compareTo(e2.getName())).collect(Collectors.toList());
System.out.println("按照工资的降序来列出员工信息:" +sortEmployeeList);
System.out.println("按照名字的升序列出员工信息:" + sortEmployeeByName.toString());
//获取工资最高的前2条员工信息
List<Employee> top2EmployeeList= employeeList.stream()
.sorted((e1,e2)->Integer.compare(e2.getSalary(),e1.getSalary()))
.limit(2)
.collect(Collectors.toList());
System.out.println("获取工资最高的前2条员工信息:"+top2EmployeeList.toString());
//获取平均工资
OptionalDouble averageSalary = employeeList.stream().mapToInt(employee->employee.getSalary()).average();
System.out.println("平均工资:" + averageSalary);
//查找New York
OptionalDouble averageSalaryByOffice = employeeList.stream().filter(employee -> employee.getOffice()
.equals("New York"))
.mapToInt(employee->employee.getSalary())
.average();
System.out.println("New York办公室平均工资:" + averageSalaryByOffice);
}
}