Stream:java1.8新特性

Stream:java1.8新特性

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ke_zhang_123/article/details/76549410

Stream:java1.8新特性

what?(它是什么)

stream流,Stream 就如同一个迭代器(Iterator),单向,不可往复,数据只能遍历一次,遍历过一次后即用尽了,就好比流水从面前流过,一去不复返.
Stream 可以并行化操作,使用并行去遍历时,数据会被分成多个段,其中每一个都在不同的线程中处理,然后将结果一起输出。Stream的并行操作依赖于 Java7 中引入的 Fork/Join 框架(JSR166y)来拆分任务和加速处理过程。

why?(为什么使用它)

但在当今这个数据大爆炸的时代,在数据来源多样化、数据海量化的今天,很多时候不得不脱离 RDBMS,或者以底层返回的数据为基础进行更上层的数据统计
更多的时候是程序员需要用 Iterator 来遍历集合,完成相关的聚合应用逻辑.但是效率很低
而使用stream使用并发模式,程序执行速度更快.

how?(怎么做)

一、流的初始化与转换:

Java中的Stream的所有操作都是针对流的,所以,使用Stream必须要得到Stream对象:
1.初始化一个流:

Stream stream = Stream.of("a", "b", "c");
  • 1

2.数组转换为一个流:

String [] strArray = new String[] {"a", "b", "c"};
    stream = Stream.of(strArray);
  • 1
  • 2
或者
  • 1
stream = Arrays.stream(strArray);
  • 1

3.集合对象转换为一个流(Collections):

List<String> list = Arrays.asList(strArray);
    stream = list.stream();
  • 1
  • 2
二、流的操作:

流的操作可以归结为几种:

1.遍历操作(map):

使用map操作可以遍历集合中的每个对象,并对其进行操作,map之后,用.collect(Collectors.toList())会得到操作后的集合。

1.1.遍历转换为大写:
List<String> output = wordList.stream().
     map(String::toUpperCase).
           collect(Collectors.toList());
  • 1
  • 2
  • 3
1.2.平方数:
List<Integer> nums = Arrays.asList(1, 2, 3, 4);
   List<Integer> squareNums = nums.stream().
         map(n -> n * n).
       collect(Collectors.toList());
  • 1
  • 2
  • 3
  • 4
2.过滤操作(filter):

使用filter可以对象Stream中进行过滤,通过测试的元素将会留下来生成一个新的Stream。

2.1.得到其中不为空的String
List<String> filterLists = new ArrayList<>();
filterLists.add("");
filterLists.add("a");
filterLists.add("b");
List afterFilterLists = filterLists.stream()
       .filter(s -> !s.isEmpty())
        .collect(Collectors.toList());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
3.循环操作(forEach):

如果只是想对流中的每个对象进行一些自定义的操作,可以使用forEach:

List<String> forEachLists = new ArrayList<>();
forEachLists.add("a");
forEachLists.add("b");
forEachLists.add("c");
forEachLists.stream().forEach(s-> System.out.println(s));
  • 1
  • 2
  • 3
  • 4
  • 5
4.返回特定的结果集合(limit/skip):

limit 返回 Stream 的前面 n 个元素;skip 则是扔掉前 n 个元素:

List<String> forEachLists = new ArrayList<>();
forEachLists.add("a");
forEachLists.add("b");
forEachLists.add("c");
forEachLists.add("d");
forEachLists.add("e");
forEachLists.add("f");
List<String> limitLists = forEachLists.stream().skip(2).limit(3).collect(Collectors.toList());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

注意skip与limit是有顺序关系的,比如使用skip(2)会跳过集合的前两个,返回的为c、d、e、f,然后调用limit(3)会返回前3个,所以最后返回的c,d,e

5.排序(sort/min/max/distinct):

sort可以对集合中的所有元素进行排序。max,min可以寻找出流中最大或者最小的元素,而distinct可以寻找出不重复的元素:

5.1.对一个集合进行排序:
List<Integer> sortLists = new ArrayList<>();
sortLists.add(1);
sortLists.add(4);
sortLists.add(6);
sortLists.add(3);
sortLists.add(2);
List<Integer> afterSortLists = sortLists.stream().sorted((In1,In2)->
       In1-In2).collect(Collectors.toList());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
5.2、得到其中长度最大的元素:
List<String> maxLists = new ArrayList<>();
maxLists.add("a");
maxLists.add("b");
maxLists.add("c");
maxLists.add("d");
maxLists.add("e");
maxLists.add("f");
maxLists.add("hahaha");
int maxLength = maxLists.stream().mapToInt(s->s.length()).max().getAsInt();
System.out.println("字符串长度最长的长度为"+maxLength);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
5.3、对一个集合进行查重:
List<String> distinctList = new ArrayList<>();
distinctList.add("a");
distinctList.add("a");
distinctList.add("c");
distinctList.add("d");
List<String> afterDistinctList = distinctList.stream().distinct().collect(Collectors.toList());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

其中的distinct()方法能找出stream中元素equal(),即相同的元素,并将相同的去除,上述返回即为a,c,d。

6、匹配(Match方法):

有的时候,我们只需要判断集合中是否全部满足条件,或者判断集合中是否有满足条件的元素,这时候就可以使用match方法:
allMatch:Stream 中全部元素符合传入的 predicate,返回 true
anyMatch:Stream 中只要有一个元素符合传入的 predicate,返回 true
noneMatch:Stream 中没有一个元素符合传入的 predicate,返回 true

6.1、判断集合中没有有为‘c’的元素:
List<String> matchList = new ArrayList<>();
matchList.add("a");
matchList.add("a");
matchList.add("c");
matchList.add("d"); 
boolean isExits = matchList.stream().anyMatch(s -> s.equals("c"));
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
6.2、判断集合中是否全不为空:
List<String> matchList = new ArrayList<>();
matchList.add("a");
matchList.add("");
matchList.add("a");
matchList.add("c");
matchList.add("d");
boolean isNotEmpty = matchList.stream().noneMatch(s -> s.isEmpty());
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

则返回的为false

转载于:https://www.cnblogs.com/jobs-lgy/p/9881296.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值