好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

java8使用Stream API方法总结

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

stream 的三个操作步骤

1、创建stream.

得到stream流的第一种方式:

可以通过collection系列集合提供提供的stream()或parallelstream

?

1

2

3

4

5

6

7

8

9

10

11

@test

 

public void test1() {

 

   //可以通过collection系列集合提供提供的stream()或parallelstream

 

   list<string> list = new arraylist<>();

 

   stream<string> stream = list.stream();

 

}

 

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

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

?

1

2

3

dept[] depts = new dept[ 10 ];

 

stream<dept> deptstream = arrays.stream(depts);

 

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

创建无限流 //迭代 stream<integer> integerstream = stream.iterate(0,(x) -> x+2);

2、中间操作

//创建无限流 //迭代 stream<integer> integerstream = stream.iterate(0,(x) -> x+2); //中间操作 integerstream.limit(10).foreach(system.out::println);

 

6、

查看运行结果

3、终止操作

?

1

2

3

4

5

6

7

8

9

//创建无限流

 

//迭代

 

stream<integer> integerstream = stream.iterate( 0 ,(x) -> x+ 2 );

 

//终止操作

 

integerstream.foreach(system.out::println);

 

查看运行结果

查看更多关于java8使用Stream API方法总结的详细内容...

  阅读:15次