0 创建流
public void test1(){ List<String> list = new ArrayList<>(); Stream<String> stream = list.stream(); Integer[] nums = new Integer[10]; Stream<Integer> stream1 = Arrays.stream(nums); Stream<Integer> stream2 = Stream.of(1,2,3,4,5,6); Stream<Integer> stream3 = Stream.iterate(0, (x) -> x + 2).limit(10); stream3.forEach(System.out::println); Stream<Double> stream4 = Stream.generate(Math::random).limit(2); stream4.forEach(System.out::println); }
1 合并多个流
//合并多个流 private void merge() { List<String> list = new ArrayList<String>(); list.add("I am a boy"); list.add("I love the girl"); list.add("But the girl loves another girl"); List<String> result = list.stream() //得到流 .map(line->line.split(" ")) //将每个元素转成String[] .flatMap(Arrays::stream) //将每个String[] 转成流 ,并合并为一个大流 .distinct() //对元素去重 .collect(Collectors.toList()); //终止流得到集合 result.forEach(System.out::println); //打印 }
2 是否匹配任一元素
//是否匹配任一元素 private void anyMatch() { List<Person> persons = Arrays.asList(new Person("zhangsan", 18),new Person("lisi", 19)); boolean isHas = persons.stream().anyMatch(e -> e.getAge() == 18); System.out.println("isHas : "+isHas); }
3 是否所有元素都匹配
//是否所有元素都匹配 private void allMatch() { List<Person> persons = Arrays.asList(new Person("zhangsan", 18),new Person("lisi", 19)); boolean bool = persons.stream().allMatch(e -> e.getAge() == 18); System.out.println("isHas : "+bool); }
4 是否所有元素都不匹配
//是否所有元素都不匹配 private void noneMatch() { List<Person> persons = Arrays.asList(new Person("zhangsan", 18),new Person("lisi", 19)); boolean bool = persons.stream().noneMatch(e -> e.getAge() == 20); System.out.println("isHas : "+bool); }
5 新增容器Optional ,可以避免空指针异常
private void orElse() { Optional<Integer> optional2 = Optional.ofNullable(null); System.out.println( optional2.orElse(23)); }
6 归并 reduce
//归并 private void testReduce() { List<Person> persons = Arrays.asList(new Person("zhangsan", 18),new Person("lisi", 19)); //reduce 不设置初始值 Optional<Integer> optional = persons.stream().map(e -> e.getAge()).reduce((a,b) -> a + b); System.out.println(optional.get()); //reduce 设置初始值 Integer ageTotal = persons.stream().map(e -> e.getAge()).reduce(0,(a,b) -> a + b); System.out.println(ageTotal); }
7 map filter等操作
private void commonTest() { List<Integer> numbers = Arrays.asList(3, 2, 2, 3, 7, 3, 5); //forEach 遍历 numbers.stream().forEach(System.out::println); //map 映射每个元素到对应的结果 List<Integer> squaresList = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList()); squaresList.stream().forEach(System.out::println); //filter 过滤 List<Integer> filetrList = numbers.stream().filter(x -> x>6).collect(Collectors.toList()); filetrList.stream().forEach(System.out::println); //limit 截取多少个 numbers.stream().limit(3).forEach(System.out::println); //sorted 排序 numbers.stream().sorted().forEach(System.out::println); //summaryStatistics 统计 IntSummaryStatistics statistics = numbers.stream().mapToInt(x -> x).summaryStatistics(); System.out.println(statistics.getMax()); System.out.println(statistics.getMin()); }
8 收集器
//使用收集器 private void collector() { List<Person> persons = Arrays.asList(new Person("zhangsan", 18), new Person("lisi", 19),new Person("wangwu", 23), new Person("zhaoliu", 25),new Person("qianqi", 17)); //统计个数 Long count = persons.stream().collect(Collectors.counting()); System.out.println(count); //计算最大值 Optional<Person> optional = persons.stream().collect(Collectors.maxBy(Comparator.comparingInt(Person::getAge))); System.out.println(optional.get().getName()); //求和 Integer sum = persons.stream().collect(Collectors.summingInt(Person::getAge)); System.out.println(sum); //求平均值 Double average = persons.stream().collect(Collectors.averagingInt(Person::getAge)); System.out.println(average); //统计 最大值 , 最小值 , 平均值 , 和 , 个数 IntSummaryStatistics summary = persons.stream().collect(Collectors.summarizingInt(Person::getAge)); System.out.println(summary.getMax()); //连接字符串 String names = persons.stream().map(x -> x.getName()).collect(Collectors.joining(" | ")); System.out.println(names); //一般归约 Integer sumAge = persons.stream().collect(Collectors.reducing(0, Person::getAge, (x , y) -> x + y )); System.out.println(sumAge); //分组 Map<String, List<Person>> map = persons.stream().collect(Collectors.groupingBy((person) -> { if(person.getAge() < 15 ) { return "small"; }else if(person.getAge() < 20) { return "middle"; }else { return "larger"; } })); map.get("middle").forEach( x -> System.out.println(x.getName())); //分组并统计 Map<String, Long> map2 = persons.stream().collect(Collectors.groupingBy((person) -> { if(person.getAge() < 15 ) { return "small"; }else if(person.getAge() < 20) { return "middle"; }else { return "larger"; } }, Collectors.counting() )); map2.values().stream().forEach(System.out::println); //分区是分组的特殊情况 Map<Boolean, List<Person>> map3 = persons.stream().collect(Collectors.partitioningBy(x -> x.getAge() > 18)); map3.get(false).forEach( x -> System.out.println(x.getName())); }
9 map 的使用,转换类型
//map 可以将一个类型的集合转变为其他类型的集合 public void testMap() { List<Integer> foo = Arrays.asList(3,4,5,7,6); List<Score> scores = foo.stream().map(e -> new Score(e)) .collect(Collectors.toList()); } class Score { int value ; Score(int value){ this.value = value; } }
10 对流的操作
skip
filter 从流中排除某些元素。
map
flatMap
limit 截断流,使其元素不超过给定数量。
sort 排序
distinct 筛选,通过流所生成元素的 hashCode() 和 equals() 去除重复元素
allMatch——检查是否匹配所有元素
anyMatch——检查是否至少匹配一个元素
noneMatch——都不匹配或者stream为空时候,返回true
findFirst——返回第一个元素
findAny——返回当前流中的任意元素
count——返回流中元素的总个数
max——返回流中最大值
min——返回流中最小值