distinct():去掉流中重复的数据
list.stream().distinct().forEach(System.out::println);
filter():根据条件过滤流中的数据
list.stream().filter(x -> x.length() > 4).forEach(System.out::println);
sort():对流中的数据排序
list.stream().sorted(String::compareTo).forEach(System.out::println);
limt():对流中的数据截取操作
list.stream().limit(5).forEach(System.out::println);
skip():跳过流中开头几个元素
list.stream().skip(3).forEach(System.out::println);
map映射
map映射转换元素,map方法接受一个函数,把这个函数应用于每个元素上,并将它映射为一个新的元素
list.stream().map(String::toUpperCase).forEach(System.out::println);
allMatch():判断流中的元素是否都匹配给定的条件
boolean allMatch = list.stream().allMatch(s -> s.length() > 3);
anyMathc():判断流中的元素是否有匹配给定的条件
boolean anyMatch = list.stream().anyMatch(s -> s.length() > 3);
noneMathc():判断流中的元素是否都不匹配给定的条件
boolean noneMatch = list.stream().noneMatch(s -> s.length() < 2);
count统计
long count = list.stream().count();
reduce归纳合并
List<Integer> list2 = Arrays.asList(6,21,87,34,1,78,54);
//求和,指定初始值
Integer reduce = list2.stream().reduce(0, Integer::sum);
System.out.println(reduce);
//求和,没有初始值
Optional<Integer> reduce1 = list2.stream().reduce((x,y) -> x + y);
System.out.println(reduce1.get());
//求最大值
Integer max = (list2.stream().reduce(Math::max)).get();
System.out.println(max);
//求最小值
Integer min = (list2.stream().reduce(Math::min)).get();
System.out.println(min);
collect归约
//把stream流转换为集合
Set<String> set = list.stream().collect(Collectors.toSet());
System.out.println(set);
System.out.println("-------------");
//把stream流转换为数组
Object[] objects = list.stream().toArray();
System.out.println(Arrays.toString(objects));
System.out.println("-------------");
//将stream流转换为字符串
String collect = list.stream().collect(Collectors.joining("-"));
System.out.println(collect);
System.out.println("-------------");
package com.wkcto.stream;
import java.util.*;
import java.util.stream.Collectors;
public class Test02 {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Collections.addAll(list,"bjpowernode","wkcto","hello","jj","xx","jj","jj","good","xx","good");
//流的筛选与切片
//distinct():去掉流中重复的数据
list.stream().distinct().forEach(System.out::println);
System.out.println("-------------");
//filter():根据条件过滤流中的数据
list.stream().filter(x -> x.length() > 4).forEach(System.out::println);
System.out.println("-------------");
//sort():对流中的数据排序
list.stream().sorted(String::compareTo).forEach(System.out::println);
System.out.println("-------------");
//limt():对流中的数据截取操作
list.stream().limit(5).forEach(System.out::println);
System.out.println("-------------");
//skip():跳过流中开头几个元素
list.stream().skip(3).forEach(System.out::println);
System.out.println("-------------");
//map映射
//map映射转换元素,map方法接受一个函数,把这个函数应用于每个元素上,并将它映射为一个新的元素
list.stream().map(String::toUpperCase).forEach(System.out::println);
System.out.println("-------------");
list.stream()
.map(String::length)
.map(length -> length + "")
.forEach(System.out::println);
System.out.println("-------------");
//allMatch():判断流中的元素是否都匹配给定的条件
boolean allMatch = list.stream().allMatch(s -> s.length() > 3);
System.out.println(allMatch);
System.out.println("-------------");
//anyMathc():判断流中的元素是否有匹配给定的条件
boolean anyMatch = list.stream().anyMatch(s -> s.length() > 3);
System.out.println(anyMatch);
System.out.println("-------------");
//noneMathc():判断流中的元素是否都不匹配给定的条件
boolean noneMatch = list.stream().noneMatch(s -> s.length() < 2);
System.out.println(noneMatch);
System.out.println("-------------");
//count统计
long count = list.stream().count();
System.out.println(count);
System.out.println("-------------");
//reduce归纳合并
List<Integer> list2 = Arrays.asList(6,21,87,34,1,78,54);
//求和,指定初始值
Integer reduce = list2.stream().reduce(0, Integer::sum);
System.out.println(reduce);
//求和,没有初始值
Optional<Integer> reduce1 = list2.stream().reduce((x,y) -> x + y);
System.out.println(reduce1.get());
//求最大值
Integer max = (list2.stream().reduce(Math::max)).get();
System.out.println(max);
//求最小值
Integer min = (list2.stream().reduce(Math::min)).get();
System.out.println(min);
System.out.println("-------------");
//collect归约
//把stream流转换为集合
Set<String> set = list.stream().collect(Collectors.toSet());
System.out.println(set);
System.out.println("-------------");
//把stream流转换为数组
Object[] objects = list.stream().toArray();
System.out.println(Arrays.toString(objects));
System.out.println("-------------");
//将stream流转换为字符串
String collect = list.stream().collect(Collectors.joining("-"));
System.out.println(collect);
System.out.println("-------------");
}
}