java8 stream().forEach、stream().map、stream().filter、stream().sorted的区别和用法
List< Student> list = new ArrayList < > ( ) ;
list. add ( Student. builder ( ) . id ( 2 ) . name ( "小明" ) . build ( ) ) ;
list. add ( Student. builder ( ) . id ( 1 ) . name ( "小华" ) . build ( ) ) ;
list. add ( Student. builder ( ) . id ( 3 ) . name ( "小红" ) . build ( ) ) ;
list. stream ( ) . forEach ( Student - > {
if ( "小红" . equals ( Student. getName ( ) ) ) {
Address address = Address. builder ( ) . address ( "合肥" ) . build ( ) ;
Student. setAddress ( address) ;
}
} ) ;
System. out. println ( "结果:" + list) ;
List< Student> l1= list. stream ( ) . map ( Student - > {
if ( "小红" . equals ( Student. getName ( ) ) ) {
Address address = Address. builder ( ) . address ( "合肥" ) . build ( ) ;
Student. setAddress ( address) ;
}
return Student;
} ) . collect ( Collectors. toList ( ) ) ;
System. out. println ( "改变:" + l1) ;
List< Student> l2= list. stream ( ) . filter (
Student - > AllTypeUtils. isNotEmptyAndNotNull ( Student. getAddress ( )
) ) . collect ( Collectors. toList ( ) ) ;
System. out. println ( "改变:" + l2) ;
List< Student> l3 = list. stream ( ) . sorted ( Comparator. comparing ( Student: : getid)
) .