常用API 演示
package com.test;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
public class TestStreamaAPI3 {
List<Employee> emps = Arrays.asList(
new Employee(102, "李四", 59, 6666.66, Employee.Status.FREE),
new Employee(101, "张三", 18, 9999.99, Employee.Status.FREE),
new Employee(103, "王五", 28, 3333.33, Employee.Status.FREE),
new Employee(104, "赵六", 8, 7777.77, Employee.Status.FREE),
new Employee(104, "赵六", 8, 7777.77, Employee.Status.FREE),
new Employee(104, "赵六", 8, 7777.77, Employee.Status.FREE),
new Employee(105, "田七", 38, 5555.55, Employee.Status.FREE)
);
@Test
public void tets1() {
//检查是否匹配所有元素 ---allMatch
boolean b1 = emps.stream().allMatch((item) -> {
return item.getStatus().equals(Employee.Status.FREE);
});
System.out.println(b1);
//检查是否匹配其中的一个元素 ---- anyMatch
boolean b2 = emps.stream().anyMatch((item) -> {
return item.getStatus().equals(Employee.Status.FREE);
});
System.out.println(b2);
//检查是否没有匹配元素 ---- noneMatch
boolean b3 = emps.stream().noneMatch((item) -> {
return item.getStatus().equals(Employee.Status.FREE);
});
System.out.println(b3);
//返回第一个元素 ---- findFirst
Optional<Employee> op = emps.stream()
//根据薪资进行排序
.sorted((e1, e2) -> -Double.compare(e1.getSalary(), e2.getSalary()))
//获取打一个元素
.findFirst();
System.out.println(op.get());
//返回当前流中的任意元素 使用并行流进行数据据的处理 ---- parallelStream
Optional<Employee> any = emps.parallelStream().filter((item) -> item.getStatus().equals(Employee.Status.FREE)).findAny();
System.out.println(any);
//返回流中的元素个数
long count = emps.stream().count();
System.out.println(count);
//获取流中元素的最大值
Optional<Employee> max = emps.stream().max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
System.out.println(max);
//获取流中数据的最小值
Optional<Employee> min = emps.stream().min((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
System.out.println(min);
}
//map -- reduce 模式
@Test
public void test02() {
List<Integer> list = Arrays.asList(12, 3, 4, 5, 6, 78, 9, 1, 10);
Integer sum = list.stream().reduce(0, (e1, e2) -> e1 + e2);
System.out.println(sum);
Optional<Double> reduce = emps.stream().map(Employee::getSalary).reduce(Double::sum);
System.out.println(reduce);
}
@Test
public void test03() {
//按照状态进行分组
Map<Employee.Status, List<Employee>> map = emps.stream().collect(Collectors.groupingBy(Employee::getStatus));
System.out.println(map);
}
@Test
public void test04() {
//多级分组 先按照状态分组 , 然后再年龄段分组
Map<Employee.Status, Map<String, List<Employee>>> collect = emps.stream().collect(Collectors.groupingBy(Employee::getStatus, Collectors.groupingBy((e) -> {
if (((Employee) e).getAge() <= 35) {
return "青年";
} else if (((Employee) e).getAge() <= 55) {
return "中年";
} else {
return "老年";
}
})));
System.out.println(collect);
}
@Test
public void test05() {
//分区 分成两个去 一个true 一个flase
Map<Boolean, List<Employee>> booleanListMap = emps.stream().collect(Collectors.partitioningBy(employee -> employee.getSalary() > 8000));
System.out.println(booleanListMap);
}
@Test
public void test06() {
Optional<Employee> optional = Optional.ofNullable(new Employee());
if (optional.isPresent()) {
System.out.println(optional.get());
}
Employee employee = optional.orElse(new Employee(20, "扎根三三", 23, 999.99, Employee.Status.FREE));
System.out.println(employee);
}
}
涉及的实体类
package com.test;
public class Employee {
private int id;
private String name;
private int age;
private double salary;
private Status status;
public Employee() {
}
public Employee(String name) {
this.name = name;
}
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public Employee(int id, String name, int age, double salary) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
}
public Employee(int id, String name, int age, double salary, Status status) {
this.id = id;
this.name = name;
this.age = age;
this.salary = salary;
this.status = status;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public String show() {
return "测试方法引用!";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + id;
result = prime * result + ((name == null) ? 0 : name.hashCode());
long temp;
temp = Double.doubleToLongBits(salary);
result = prime * result + (int) (temp ^ (temp >>> 32));
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (age != other.age)
return false;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
if (Double.doubleToLongBits(salary) != Double.doubleToLongBits(other.salary))
return false;
return true;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", salary=" + salary + ", status=" + status
+ "]";
}
public enum Status {
FREE, BUSY, VOCATION;
}
}