赞
踩
- //stream流中的filter
- //filter 方法用于通过设置的条件过滤出元素。以下代码片段使用 filter 方法过滤掉空字符串:
- List<String> filter = Arrays.asList("mz", "", "mz55", "m", "MZ");
- Stream<String> stringStream = filter.stream().filter(item -> !item.isEmpty());
- stringStream.forEach(item -> {
- stringBuilder1.append(item).append(",");
- });
- System.out.println(stringBuilder1);
- //mz,mz55,m,MZ
- //stream流中的map
- //map 方法用于映射每个元素到对应的结果,以下代码片段使用 map 输出了元素对应的平方数:
- List<Integer> map = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
- StringBuilder stringBuilder = new StringBuilder();
- Stream<Integer> integerStream = map.stream().map(i -> i * i);
- integerStream.forEach(item -> {
- stringBuilder.append(item).append(",");
- });
- System.out.println(stringBuilder);
- //9,4,4,9,49,9,25,
- //limit/skip
- //imit 返回 Stream 的前面n 个元素;skip 则是扔掉前n个元素。以下代码片段使用limit 方法保留4个元素:
- List<Integer> limit = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
- StringBuilder stringBuilder2 = new StringBuilder();
- Stream<Integer> limit1 = limit.stream().limit(4);
- limit1.forEach(item ->{
- stringBuilder2.append(item).append(",");
- });
- System.err.println(stringBuilder2);
- //3,2,2,3,
- //sorted
- //sorted 方法用于对流进行排序。以下代码片段使用 sorted 方法进行排序:
- List<Integer> sorted = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
- StringBuilder stringBuilder3 = new StringBuilder();
- Stream<Integer> sorted1 = sorted.stream().sorted();
- sorted1.forEach(item -> {
- stringBuilder3.append(item).append(",");
- });
- System.out.println(stringBuilder3);
- //2,2,3,3,3,5,7,
- //distinct
- //distinct主要用来去重,以下代码片段使用 distinct 对元素进行去重:
- List<Integer> distinct = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
- StringBuilder stringBuilder4 = new StringBuilder();
- Stream<Integer> distinct1 = distinct.stream().distinct();
- distinct1.forEach(item -> {
- stringBuilder4.append(item).append(",");
- });
- System.out.println(stringBuilder4);
- //3,2,7,5,
- //接下来演示下,当一个Stream先后通过filter、map、sort、limit以及distinct处理
- //后会发生什么。
- List<String> list = Arrays.asList("mz", "", "mz55", "m", "MZ","Hello","HelloWord","mz");
- StringBuilder stringBuilder5 = new StringBuilder();
- Stream<Integer> distinct2 = list.stream().filter(ite -> ite.length() <= 6)
- .map(String::length).sorted()
- .limit(3)
- .distinct();
- distinct2.forEach(item -> {
- stringBuilder5.append(item).append(",");
- });
- System.out.println(stringBuilder5);
- //0,1,2,
- //forEach
- //Stream 提供了方法 “forEach’来迭代流中的每个数据。以下代码片段使用 forEach 输出了10个随机数:
- Random random = new Random();
- random.ints().limit(10).forEach(System.err::println);
- //count
- //count用来统计流中的元素个数。
- List<String> count = Arrays.asList("mz", "", "mz55", "m", "MZ","Hello","HelloWord","mz");
- long count1 = count.stream().count();
- System.err.println(count1);
- //8
- //collect
- //collect就是一个归约操作,可以接受各种做法作为参数,将流中的元素累积成一个汇总结果:
- List<String> collect = Arrays.asList("mz", "", "mz55", "m", "MZ","Hello","HelloWord","mz");
- StringBuilder stringBuilder6 = new StringBuilder();
- List<String> mz = collect.stream().filter(item -> item.startsWith("m")).collect(Collectors.toList());
- mz.forEach(itme -> {
- stringBuilder6.append(itme).append(",");
- });
- System.err.println(stringBuilder6.toString());
- //mz,mz55,m,mz,
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。