当前位置:   article > 正文

Stream流的简单用法

Stream流的简单用法

filter

  1. //stream流中的filter
  2. //filter 方法用于通过设置的条件过滤出元素。以下代码片段使用 filter 方法过滤掉空字符串:
  3. List<String> filter = Arrays.asList("mz", "", "mz55", "m", "MZ");
  4. Stream<String> stringStream = filter.stream().filter(item -> !item.isEmpty());
  5. stringStream.forEach(item -> {
  6. stringBuilder1.append(item).append(",");
  7. });
  8. System.out.println(stringBuilder1);
  9. //mz,mz55,m,MZ

map

  1. //stream流中的map
  2. //map 方法用于映射每个元素到对应的结果,以下代码片段使用 map 输出了元素对应的平方数:
  3. List<Integer> map = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
  4. StringBuilder stringBuilder = new StringBuilder();
  5. Stream<Integer> integerStream = map.stream().map(i -> i * i);
  6. integerStream.forEach(item -> {
  7. stringBuilder.append(item).append(",");
  8. });
  9. System.out.println(stringBuilder);
  10. //9,4,4,9,49,9,25,

limit/skip

  1. //limit/skip
  2. //imit 返回 Stream 的前面n 个元素;skip 则是扔掉前n个元素。以下代码片段使用limit 方法保留4个元素:
  3. List<Integer> limit = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
  4. StringBuilder stringBuilder2 = new StringBuilder();
  5. Stream<Integer> limit1 = limit.stream().limit(4);
  6. limit1.forEach(item ->{
  7. stringBuilder2.append(item).append(",");
  8. });
  9. System.err.println(stringBuilder2);
  10. //3,2,2,3,

sorted

  1. //sorted
  2. //sorted 方法用于对流进行排序。以下代码片段使用 sorted 方法进行排序:
  3. List<Integer> sorted = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
  4. StringBuilder stringBuilder3 = new StringBuilder();
  5. Stream<Integer> sorted1 = sorted.stream().sorted();
  6. sorted1.forEach(item -> {
  7. stringBuilder3.append(item).append(",");
  8. });
  9. System.out.println(stringBuilder3);
  10. //2,2,3,3,3,5,7,

distinct

  1. //distinct
  2. //distinct主要用来去重,以下代码片段使用 distinct 对元素进行去重:
  3. List<Integer> distinct = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
  4. StringBuilder stringBuilder4 = new StringBuilder();
  5. Stream<Integer> distinct1 = distinct.stream().distinct();
  6. distinct1.forEach(item -> {
  7. stringBuilder4.append(item).append(",");
  8. });
  9. System.out.println(stringBuilder4);
  10. //3,2,7,5,

综合用法

  1. //接下来演示下,当一个Stream先后通过filter、map、sortlimit以及distinct处理
  2. //后会发生什么。
  3. List<String> list = Arrays.asList("mz", "", "mz55", "m", "MZ","Hello","HelloWord","mz");
  4. StringBuilder stringBuilder5 = new StringBuilder();
  5. Stream<Integer> distinct2 = list.stream().filter(ite -> ite.length() <= 6)
  6. .map(String::length).sorted()
  7. .limit(3)
  8. .distinct();
  9. distinct2.forEach(item -> {
  10. stringBuilder5.append(item).append(",");
  11. });
  12. System.out.println(stringBuilder5);
  13. //0,1,2,

forEach

  1. //forEach
  2. //Stream 提供了方法 “forEach’来迭代流中的每个数据。以下代码片段使用 forEach 输出了10个随机数:
  3. Random random = new Random();
  4. random.ints().limit(10).forEach(System.err::println);

count

  1. //count
  2. //count用来统计流中的元素个数。
  3. List<String> count = Arrays.asList("mz", "", "mz55", "m", "MZ","Hello","HelloWord","mz");
  4. long count1 = count.stream().count();
  5. System.err.println(count1);
  6. //8

collect

  1. //collect
  2. //collect就是一个归约操作,可以接受各种做法作为参数,将流中的元素累积成一个汇总结果:
  3. List<String> collect = Arrays.asList("mz", "", "mz55", "m", "MZ","Hello","HelloWord","mz");
  4. StringBuilder stringBuilder6 = new StringBuilder();
  5. List<String> mz = collect.stream().filter(item -> item.startsWith("m")).collect(Collectors.toList());
  6. mz.forEach(itme -> {
  7. stringBuilder6.append(itme).append(",");
  8. });
  9. System.err.println(stringBuilder6.toString());
  10. //mz,mz55,m,mz,

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/821807
推荐阅读
相关标签
  

闽ICP备14008679号