package com.example.jsch_smb;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class Java8StreamDemo {
//并行流 parallelStream forEachOrdered
private void parallelStream() {
List nums = CollUtil.newArrayList(1, 2, 3, 4, 5);
//顺序打印
nums.stream().forEach(System.out::print);
nums.stream().forEachOrdered(System.out::print);
//并行流
nums.parallelStream().forEach(System.out::print);
nums.parallelStream().forEachOrdered(System.out::print);
}
//流连接 flatMap
private void flatMap() {
List list = Arrays.asList("a,b,c", "1,2,3");
List newList = list.stream().flatMap(s -> {
//将每个元素转换成一个stream
String[] split = s.split(",");
Stream s2 = Arrays.stream(split);
return s2;
}).collect(Collectors.toList());
newList.forEach(System.out::println); // a b c 1 2 3
}
//分组排序 Comparator.comparing Comparator.naturalOrder Collectors.partitioningBy
private void groupAndSort() {
Student s1 =new Student("aa", 10);
Student s2 =new Student("bb", 20);
Student s3 =new Student("aa",30);
Student s4 =new Student("dd", 40);
List students = CollUtil.newArrayList(s1, s2, s3, s4);
//二次排序
students.stream().sorted(
Comparator.comparing(Student::getName,Comparator.naturalOrder())
.thenComparing(Student::getAge,Comparator.reverseOrder())).collect(Collectors.toList());
//一次分组 分成两部分,一部分大于10岁,一部分小于等于10岁
Map>> collect2 = students.stream().collect(
Collectors.partitioningBy(e -> e.getAge() >10, Collectors.groupingBy(Student::getName)));
//二次分组 先第一个提交 在第二个条件
Map>> collect3 = students.stream().collect(
Collectors.partitioningBy(e -> e.getAge() >10, Collectors.groupingBy(k -> {
if (k.getAge() ==30) {
return "equal";
}else {
return "unequal";
}
})));
//分组统计
LinkedHashMap collect = students.stream().collect(
Collectors.groupingBy(Student::getName, LinkedHashMap::new, Collectors.counting()));
//分组取最大值
Map collect1 = students.stream().collect(
Collectors.groupingBy(Student::getName, Collectors.collectingAndThen(
Collectors.maxBy(Comparator.comparingInt(Student::getAge)), Optional::get)));
}
//匹配,聚合
private void match() {
List list = Arrays.asList(1, 2, 3, 4, 5);
boolean allMatch = list.stream().allMatch(e -> e >10); //false
boolean noneMatch = list.stream().noneMatch(e -> e >10); //true
boolean anyMatch = list.stream().anyMatch(e -> e >4); //true
Integer findFirst = list.stream().findFirst().get(); //1
Integer findAny = list.stream().findAny().get(); //1
long count = list.stream().count(); //5
Integer max = list.stream().max(Integer::compareTo).get(); //5
Integer min = list.stream().min(Integer::compareTo).get(); //1
}
//积累
private void experience() {
Student s1 =new Student("aa", 10);
Student s2 =new Student("bb", 20);
Student s3 =new Student("aa", 30);
Student s4 =new Student("dd", 40);
List students = CollUtil.newArrayList(s1, s2, s3, s4);
// 1. 取max 值
//List
IntSummaryStatistics collect = CollUtil.newArrayList(1, 2, 3, 4, 5).stream().collect(Collectors.summarizingInt(Integer::intValue));
//IntSummaryStatistics{count=5, sum=15, min=1, average=3.000000, max=5}
// List
students.stream().collect(Collectors.summarizingInt(Student::getAge)).getMax();
// List
List maps = CollUtil.newArrayList();
students.parallelStream().forEach(e -> {
maps.add(MapUtil.of(e.getName(), e.getAge()));
});
maps.stream().mapToInt(
m -> m.get("age") !=null ? Integer.parseInt(m.get("age").toString()) :0
).max().getAsInt();
// 2. 取总值 等聚合值 sum average ...
// List<对象>
students.stream().mapToInt(n -> n.getAge()).summaryStatistics().getSum();
// List
int sum = maps.stream().mapToInt(
m -> m.get("age") !=null ? Integer.parseInt(m.get("age").toString()) :0
).sum();
}
public static void main(String[] args) {
Java8StreamDemo demo =new Java8StreamDemo();
demo.groupAndSort();
}
static class Student {
private Stringname;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public StringgetName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
}