Stream:java1.8新特性

来源:互联网 发布:linux cdn搭建 编辑:程序博客网 时间:2024/05/21 10:41

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");

2.数组转换为一个流:

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

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

List<String> list = Arrays.asList(strArray);    stream = list.stream();

二、流的操作:

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

1.遍历操作(map):

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

1.1.遍历转换为大写:
List<String> output = wordList.stream().     map(String::toUpperCase).           collect(Collectors.toList());
1.2.平方数:
List<Integer> nums = Arrays.asList(1, 2, 3, 4);   List<Integer> squareNums = nums.stream().         map(n -> n * n).       collect(Collectors.toList());
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());
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));
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());

注意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());
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);
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());

其中的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"));
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());

则返回的为false

原创粉丝点击