package day0304;
import java. util. ArrayList;
import java. util. Date;
import java. util. List;
public class Stream {
public static void main ( String[ ] args) {
Date da = new Date ( ) ;
System. out. println ( da) ;
List< String> list = new ArrayList < > ( ) ;
list. add ( "张无忌" ) ;
list. add ( "周芷若" ) ;
list. add ( "赵敏" ) ;
list. add ( "张强" ) ;
list. add ( "张三丰" ) ;
show1 ( list) ;
show2 ( list) ;
}
A.常规方法
static void show1 ( List< String> list) {
List< String> listA = new ArrayList < String> ( ) ;
for ( String s : list) {
if ( s. startsWith ( "张" ) ) {
listA. add ( s) ;
}
}
List< String> listB = new ArrayList < String> ( ) ;
for ( String s : listA) {
if ( s. length ( ) == 3 ) {
listB. add ( s) ;
}
}
for ( String s : listB) {
System. out. println ( s) ;
}
}
B.利用Stream流
static void show2 ( List< String> list) {
list. stream ( )
. filter ( name- > name. startsWith ( "张" ) )
. filter ( name- > name. length ( ) == 3 )
. forEach ( name- > System. out. println ( name) ) ;
}
}