1.什么是stream流
2.stream操作步骤
3.流的生成方式
//集合流
List<String> list = new ArrayList<>();
Stream<String> stream1 = list.stream();
Stream<String> list1=list.stream();
//数组流
String [] arr=new String[10];
Stream<String> arr1 =Arrays.stream(arr);
//Stream 静态方法
Stream<Integer> stream3 = Stream.of(1, 2, 3);
//无线流
Stream<Integer> Stream4=Stream.iterate(0, (x) -> x++);
//生成
Stream<String> double1=Stream.generate( () -> "sout" );
4.流的中间操作
- filter:接收 Lambda ,从流中排除某些元素
- limit:截断流,使其元素不超过给定数量
- skip(n):跳过元素,返回一个舍弃了前n个元素的流;若流中元素不足n个,则返回一个空流;与 limit(n) 互补
- distinct:筛选,通过流所生成的 hashCode() 与 equals() 取除重复元素
public class Employee {
private Integer id;
private String name;
private Integer age;
private Double salary;
}
@Test
public void text3(){
List<Employee> emps = Arrays.asList(
new Employee(101, "Z3", 19, 9999.99),
new Employee(102, "L4", 20, 7777.77),
new Employee(103, "W5", 35, 6666.66),
new Employee(104, "Tom", 44, 1111.11),
new Employee(105, "Jerry", 60, 4444.44),
new Employee(105, "Jerry", 60, 4444.44),
new Employee(105, "Jerry", 60, 4444.44)
);
Stream<Employee> asdsasa = emps.stream().filter((x) -> {
if (x.getAge() > 35) {
return true;
}
;
return false;
}).limit(10).distinct().skip(1);
asdsasa.forEach(System.out::println);
}
5.映射
- map:接收 Lambda ,将元素转换为其他形式或提取信息;接受一个函数作为参数,该函数会被应用到每个元素上,并将其映射成一个新的元素
- flatMap:接收一个函数作为参数,将流中每一个值都换成另一个流,然后把所有流重新连接成一个流
map
@Test
public void test02(){
List<String> list = Arrays.asList("a", "b", "c");
list.stream()
.map((str) -> str.toUpperCase())
.forEach(System.out::println);
emps.stream().map(Employee::getSalary).forEach(System.out::println);
}
@Test
public void test03(){
List<String> list = Arrays.asList("a", "b", "c");
test01 test02 = new test01();
//{{a},{b}}得到这样的流
Stream<Stream<Character>> list4=list.stream().map(test02::filterCharacter);
list4.forEach((x) -> {
x.forEach(System.out::println);
});
//{{a},{b}}直接把这样的流变成{a,b}
Stream<Character> list5=list.stream()
.flatMap(test02::filterCharacter);
list5.forEach(System.out::println);
}
public Stream<Character> filterCharacter(String str){
List<Character> list = new ArrayList<>();
for (char c : str.toCharArray()) {
list.add(c);
}
return list.stream();
}
6排序
@Test
public void test04(){
//如果年龄一样按名字排
emps.stream().sorted((x,y) -> {
if(x.getAge().equals(y.getAge())){
return x.getName().compareTo(y.getName());
}else{
return x.getAge().compareTo(y.getAge());
}
}).forEach(System.out::println);
}
结果
Employee{id=101, name='Z3', age=19, salary=9999.99}
Employee{id=102, name='L4', age=20, salary=7777.77}
Employee{id=103, name='W5', age=35, salary=6666.66}
Employee{id=104, name='Tom', age=44, salary=1111.11}
Employee{id=105, name='Jerry', age=60, salary=4444.44}
Employee{id=105, name='aerry', age=60, salary=4444.44}
Employee{id=105, name='aerry', age=60, salary=4444.44}
7.查找 / 匹配
终止操作:
- allMatch:检查是否匹配所有元素
- anyMatch:检查是否至少匹配一个元素
- noneMatch:检查是否没有匹配所有元素
- findFirst:返回第一个元素
- findAny:返回当前流中的任意元素
- count:返回流中元素的总个数
- max:返回流中最大值
- min:返回流中最小值
@Test
public void test04(){
//allMatch:检查是否匹配所有元素 下面代码是否名字全叫z2输出false
System.out.println(emps.stream().allMatch((t) -> "Z3".equals(t.getName())));
//anyMatch:检查是否至少匹配一个元素 true
System.out.println(emps.stream().anyMatch((t) -> "Z3".equals(t.getName())));
//noneMatch:检查是否没有匹配所有元素 没有xx 输出ture
System.out.println(emps.stream().noneMatch((t) -> "xx".equals(t.getName())));
//findFirst:返回第一个元素
Optional<Employee> first = emps.stream().findFirst();
System.out.println(first);
//findFirst:返回第一个元素 空直接赋值
Employee employee=emps.stream().findFirst().orElse(null);
System.out.println(employee);
//findAny:返回当前流中的任意元素
Employee employee1=emps.stream().findAny().orElse(null);
System.out.println(employee1);
//count:返回流中元素的总个数
System.out.println("个数"+emps.stream().count());
//max:返回流中最大值
Employee employee3=emps.stream().max((a,b) -> a.getAge().compareTo(b.getAge())).orElse(null);
System.out.println(employee3+"最大值");
//min:返回流中最小值
Employee employee4=emps.stream().min((a,b) -> a.getAge().compareTo(b.getAge())).orElse(null);
System.out.println(employee4+"最大值");
//min:返回流中最小值 =利用map
Double aDouble = emps.stream().map(Employee::getSalary).min(Double::compare).orElse(null);
System.out.println(aDouble);
}
8.归约 / 收集
- 归约:reduce(T identity, BinaryOperator) / reduce(BinaryOperator)
可以将流中的数据反复结合起来,得到一个值 - 收集:collect 将流转换成其他形式;接收一个 Collector 接口的实现,用于给流中元素做汇总的方法
@Test
public void test01(){
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
Integer integer = list.stream()
.reduce(0, (x, y) -> x + y);
System.out.println(integer);
}
//收集器
@Test
public void test05(){
//list set hash
Set<Employee> collect = emps.stream().collect(Collectors.toSet());
System.out.println(collect);
List<Employee> collect1 = emps.stream().collect(Collectors.toList());
System.out.println(collect1);
LinkedHashSet<Employee> collect2 = emps.stream().
collect(Collectors.toCollection(LinkedHashSet::new));
System.out.println(collect2);
}
//总数
@Test
public void test06(){
//总数
Long count = emps.stream()
.collect(Collectors.counting());
System.out.println(count);
//平均值
Double avg=emps.stream().collect(Collectors.averagingDouble(Employee::getSalary));
System.out.println(avg);
//总和
Double collect = emps.stream().collect(Collectors.summingDouble(Employee::getSalary));
System.out.println(collect);
//最大值
Optional<Employee> collect1 = emps.stream().collect(Collectors.maxBy((x, y) -> x.getSalary().compareTo(y.getSalary())));
System.out.println(collect1);
//最小值
Optional<Employee> collect2 = emps.stream().collect(Collectors.minBy((x, y) -> x.getSalary().compareTo(y.getSalary())));
System.out.println(collect2);
Optional<Double> collect3 = emps.stream().map(Employee::getSalary).collect(Collectors.minBy(Double::compare));
System.out.println(collect3);
}
//分组
@Test
public void test07() {
//分组
Map<Integer, List<Employee>> collect = emps.stream().collect(Collectors.groupingBy(Employee::getId));
System.out.println(collect);
//多级分组
Map<Integer, Map<Double, List<Employee>>> collect1 = emps.stream().collect(Collectors.groupingBy(Employee::getId, Collectors.groupingBy(Employee::getSalary)));
System.out.println(collect1);
Map<Integer, Map<String, List<Employee>>> collect2 = emps.stream().collect(Collectors.groupingBy(Employee::getId, Collectors.groupingBy((x) -> {
if (x.getAge() > 25) {
return "老";
} else {
return "亲少年";
}
})));
System.out.println(collect2);
//分区
Map<Boolean, List<Employee>> collect3 = emps.stream().collect(Collectors.partitioningBy((x) -> {
if (x.getAge() > 25) {
return false;
} else {
return true;
}
}));
System.out.println(collect3);
}
@Test
public void test09(){
//总结
DoubleSummaryStatistics collect = emps.stream().collect(Collectors.summarizingDouble(Employee::getSalary));
System.out.println(collect.getMax());
System.out.println(collect.getMin());
System.out.println(collect.getSum());
System.out.println(collect.getCount());
System.out.println(collect.getAverage());
//连接
String collect1 = emps.stream().map(Employee::getName).collect(Collectors.joining(","));
System.out.println(collect1);
}