1.关键代码
代码类Stream12SpecificationsAndCollect
import com.netease.streamlearningbyjava.bean.Employee2;
import org.junit.Test;
import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
/**
* 创建日期:2021/11/8 10:58
*
* @author tony.sun
* 类说明:规约与收集
*/
public class Stream12SpecificationsAndCollect {
List<Employee2> employee2s = Arrays.asList(
new Employee2("张三", 18, 9999.99, Employee2.Status.FREE),
new Employee2("李四", 58, 5555.55, Employee2.Status.BUSY),
new Employee2("王五", 26, 3333.33, Employee2.Status.VOCATION),
new Employee2("赵六", 36, 6666.66, Employee2.Status.FREE),
new Employee2("赵六", 36, 6666.66, Employee2.Status.FREE),
new Employee2("田七", 12, 8888.88, Employee2.Status.BUSY)
);
/**
* 规约
* reduce(T identity,BinaryOperator)/reduce(BinaryOperator)--可以将流中的元素反复结合起来,得到一个值。
*/
@Test
public void test3(){
List<Integer> list= Arrays.asList(1,2,3,4,5,6,7,8,9,10);
Integer sum = list.stream()
.reduce(0, (x, y) -> x + y);//这个0是起始值,第一个值,y是上面的1,2,3,4,5,6,7,8,9,10,每次都加一次,加了10次
System.out.println(sum);//55
System.out.println("----------------------------------");
Optional<Double> optional = employee2s.stream()
.map(Employee2::getSalary)
.reduce(Double::sum);
System.out.println(optional);
}
/**
* 收集
* collect--将流转化为其他形式,接收一个Collector接口实现,用于给Stream中元素做汇总的方法
*/
@Test
public void test4(){
//提取老的list里面的值,变成一个新的list(只有name),然后打印
List<String> list = employee2s.stream()
.map(Employee2::getName)
.collect(Collectors.toList());
list.forEach(System.out::println);
//结果:
//张三
//李四
//王五
//赵六
//赵六
//田七
//去重
System.out.println("--------------------------------");
Set<String> set = employee2s.stream()
.map(Employee2::getName)
.collect(Collectors.toSet());
set.forEach(System.out::println);
//结果:
//李四
//张三
//王五
//赵六
//田七
/**
* 其他格式
*/
HashSet<String> hashSet = employee2s.stream()
.map(Employee2::getName)
.collect(Collectors.toCollection(HashSet::new));
hashSet.forEach(System.out::println);
}
@Test
public void test5(){
/**
* 总数
*/
Long count = employee2s.stream()
.collect(Collectors.counting());
System.out.println(count);
/**
* 平均值
*/
Double avg = employee2s.stream()
.collect(Collectors.averagingDouble(Employee2::getSalary));
System.out.println(avg);
/**
* 总和
*/
DoubleSummaryStatistics sum = employee2s.stream()
.collect(Collectors.summarizingDouble(Employee2::getSalary));
System.out.println(sum);
/**
* 最大值
*/
Optional<Employee2> max = employee2s.stream()
.collect(Collectors.maxBy((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())));
System.out.println(max);
/**
* 最小值
*/
Optional<Double> min = employee2s.stream()
.map(Employee2::getSalary)
.collect(Collectors.minBy(Double::compare));
System.out.println(min.get());
/**
* 分组
*/
Map<Employee2.Status, List<Employee2>> group = employee2s.stream()
.collect(Collectors.groupingBy(Employee2::getStatus));
System.out.println(group);
}
}
bean类Employee2
import java.util.Objects;
/**
* 创建日期:2021/10/29 14:01
*
* @author tony.sun
* 类说明:
*/
public class Employee2 {
private String name;
private Integer age;
private Double salary;
private Status Status;
public Employee2(String name, Integer age, Double salary, Employee2.Status status) {
this.name = name;
this.age = age;
this.salary = salary;
Status = status;
}
/**
* 闲着,忙碌,休假
*/
public enum Status{
FREE,
BUSY,
VOCATION;
}
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;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public Employee2.Status getStatus() {
return Status;
}
public void setStatus(Employee2.Status status) {
Status = status;
}
@Override
public String toString() {
return "Employee2{" +
"name='" + name + '\'' +
", age=" + age +
", salary=" + salary +
", Status=" + Status +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee2 employee2 = (Employee2) o;
return Objects.equals(name, employee2.name) &&
Objects.equals(age, employee2.age) &&
Objects.equals(salary, employee2.salary) &&
Status == employee2.Status;
}
@Override
public int hashCode() {
return Objects.hash(name, age, salary, Status);
}
}
2.步骤与讲解
1.reduce收汁浓缩,这里起到一个合并值得左右,既1+2+3……+10;
List<Integer> list= Arrays.asList(1,2,3,4,5,6,7,8,9,10);
Integer sum = list.stream()
.reduce(0, (x, y) -> x + y);//这个0是起始值,x是第一个值(计算后的值),y是上面的1,2,3,4,5,6,7,8,9,10,每次都加一次,加了10次
System.out.println(sum);//55
//下面map表示要计算某个值,这里是Salary
System.out.println("----------------------------------");
Optional<Double> optional = employee2s.stream()
.map(Employee2::getSalary)
.reduce(Double::sum);
System.out.println(optional);
2.收集
1.获取list里面的值,然后提取成一个新的list
提取老的list里面的值,变成一个新的list(只有name),然后打印
//提取老的list里面的值,变成一个新的list(只有name),然后打印
List<String> list = employee2s.stream()
.map(Employee2::getName)
.collect(Collectors.toList());
list.forEach(System.out::println);
//结果:
//张三
//李四
//王五
//赵六
//赵六
//田七
2.去重
set是一个不包含重复元素的 collection。确切地说,set 不包含满足 e1.equals(e2) 的元素对 e1 和 e2,并且最多包含一个 null 元素
System.out.println("--------------------------------");
Set<String> set = employee2s.stream()
.map(Employee2::getName)
.collect(Collectors.toSet());
set.forEach(System.out::println);
//结果:
//李四
//张三
//王五
//赵六
//田七
3.总数,平均值,总和,最大值,最小值
/**
* 总数
*/
Long count = employee2s.stream()
.collect(Collectors.counting());
System.out.println(count);
/**
* 平均值
*/
Double avg = employee2s.stream()
.collect(Collectors.averagingDouble(Employee2::getSalary));
System.out.println(avg);
/**
* 总和
*/
DoubleSummaryStatistics sum = employee2s.stream()
.collect(Collectors.summarizingDouble(Employee2::getSalary));
System.out.println(sum);
/**
* 最大值
*/
Optional<Employee2> max = employee2s.stream()
.collect(Collectors.maxBy((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary())));
System.out.println(max);
/**
* 最小值
*/
Optional<Double> min = employee2s.stream()
.map(Employee2::getSalary)
.collect(Collectors.minBy(Double::compare));
System.out.println(min.get());
4.分組groupingBy
/**
* 分组
*/
Map<Employee2.Status, List<Employee2>> group = employee2s.stream()
.collect(Collectors.groupingBy(Employee2::getStatus));
System.out.println(group);