Stream流
1、认识 2、Stream流使用步骤 3、如何获取Stream流 4.Stream流的中间方法 5、 Stream流终结方法
1、认识
2、Stream流使用步骤
3、如何获取Stream流
List < String > list= new ArrayList < > ( ) ;
Collections . addAll ( list, "崔十一" , "张天年" , "张家" , "张天爱" ) ;
List < String > l = list. stream ( ) . filter ( s -> s. startsWith ( "张" ) && s. length ( ) == 3 ) . collect ( Collectors . toList ( ) ) ;
System . out. println ( l) ;
Set < String > set= new HashSet < > ( ) ;
Collections . addAll ( list, "崔十一" , "张天年" , "张家" , "张天爱" ) ;
Set < String > l1 = list. stream ( ) . filter ( s -> s. startsWith ( "张" ) && s. length ( ) == 3 ) . collect ( Collectors . toSet ( ) ) ;
System . out. println ( l1) ;
Map < String , Integer > map= new HashMap < > ( ) ;
map. put ( "java" , 100 ) ;
map. put ( "java1" , 200 ) ;
map. put ( "java2" , 300 ) ;
map. keySet ( ) . stream ( ) . filter ( s-> s. contains ( "1" ) ) . forEach ( s -> System . out. println ( s) ) ;
map. values ( ) . stream ( ) . filter ( s-> s>= 200 ) . forEach ( System . out:: println ) ;
Set < Map. Entry < String , Integer > > entries = map. entrySet ( ) ;
entries. stream ( ) . forEach ( kvs-> System . out. println ( kvs. getKey ( ) + "-->" + kvs. getValue ( ) ) ) ;
int [ ] a= new int [ ] { 1 , 2 , 3 } ;
Arrays . stream ( a) . filter ( s-> s>= 2 ) . forEach ( System . out:: println ) ;
Stream . of ( a) . forEach ( System . out:: println ) ;
4.Stream流的中间方法
5、 Stream流终结方法