java8使用Stream API方法总结

在本篇文章里小编给大家分享了关于java8使用Stream API方法相关知识点,需要的朋友们学习下。

Stream是java8中处理集合的关键抽象概念,它可以指定您希望对集合进行的操作,可以执行非常复杂的查找、过滤和映射数据等操作。使用Stream API对集合数据进行操作,就类似于使用SQL执行的数据库查询。

Stream 的三个操作步骤

1、创建Stream.

得到Stream流的第一种方式:

可以通过Collection系列集合提供提供的Stream()或parallelStream

 @Test public void test1() { //可以通过Collection系列集合提供提供的Stream()或parallelStream List list = new ArrayList<>(); Stream stream = list.stream(); } 

通过Arrays中的静态方法stream()方法得到数组流

 //通过Arrays中的静态方法stream()方法得到数组流

 Dept[] depts = new Dept[10]; Stream deptStream = Arrays.stream(depts); 

通过Stream类中的静态方法of()Stream.of("aa","bb","cc");

创建无限流 //迭代 Stream integerStream = Stream.iterate(0,(x) -> x+2);

2、中间操作

//创建无限流 //迭代 Stream integerStream = Stream.iterate(0,(x) -> x+2); //中间操作 integerStream.limit(10).forEach(System.out::println);

6、

查看运行结果

3、终止操作

 //创建无限流 //迭代 Stream integerStream = Stream.iterate(0,(x) -> x+2); //终止操作 integerStream.forEach(System.out::println); 

查看运行结果

以上就是java8使用Stream API方法总结的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java