1,提取
List < Long > projectIds = projectList. stream ( ) . map ( ProjectEntity :: getProjectId ) . distinct ( ) . collect ( Collectors . toList ( ) ) ;
Set < Long > projectIds = projectList. stream ( ) . map ( ProjectEntity :: getProjectId ) . collect ( Collectors . toSet ( ) ) ;
2,类型转换list–map
Map < Long , String > projectMap = projectList. stream ( ) . collect ( Collectors . toMap ( ProjectEntity :: getId , ProjectEntity :: getName ) ) ;
Map < String , String > projectMap = personList. stream ( ) . collect ( Collectors . toMap ( Person :: getCode , Person :: getName , ( o, n) -> o) ) ;
Map < String , Person > projectMap = personList. stream ( ) . collect ( Collectors . toMap ( Person :: getCode , v-> v, ( o, n) -> o) ) ;
3,分组
Map < Long , ProjectEntity > map = projectList. stream ( ) . collect ( Collectors . toMap ( k -> k. getId ( ) , v -> v) ) ;
Map < Boolean , List < Person > > part = personList. stream ( ) . collect ( Collectors . partitioningBy ( x -> x. getState ( ) > 1 ) ) ;
Map < String , List < Person > > group = personList. stream ( ) . collect ( Collectors . groupingBy ( Person :: getCode ) ) ;
Map < String , Map < Integer , List < Person > > > group2 = personList. stream ( ) . collect ( Collectors . groupingBy ( Person :: getCode , Collectors . groupingBy ( Person :: getState ) ) ) ;
Map < String , Long > collect = list. stream ( ) . collect ( Collectors . groupingBy ( Student :: getGrade , Collectors . counting ( ) ) ) ;
4,连接合并
String names = personList. stream ( ) . map ( x-> x. getName ( ) ) . collect ( Collectors . joining ( "和" ) ) ;
String [ ] arr1 = { "a" , "b" , "c" , "d" } ;
String [ ] arr2 = { "d" , "e" , "f" , "g" } ;
Stream < String > stream1 = Stream . of ( arr1) ;
Stream < String > stream2 = Stream . of ( arr2) ;
List < String > contents = new ArrayList < > ( ) ;
String content = contents. stream ( ) . collect ( Collectors . joining ( ";" ) ) ;
List < String > newList = Stream . concat ( stream1, stream2) . distinct ( ) . collect ( Collectors . toList ( ) ) ;
5,排序
List < String > newList = personList. stream ( ) . sorted ( Comparator . comparing ( Person :: getSalary ) ) . map ( Person :: getName )
. collect ( Collectors . toList ( ) ) ;
List < String > newList2 = personList. stream ( ) . sorted ( Comparator . comparing ( Person :: getSalary ) . reversed ( ) )
. map ( Person :: getName ) . collect ( Collectors . toList ( ) ) ;
List < String > newList3 = personList. stream ( )
. sorted ( Comparator . comparing ( Person :: getSalary ) . thenComparing ( Person :: getState ) ) . map ( Person :: getName )
. collect ( Collectors . toList ( ) ) ;
6,聚合函数
Optional < Integer > sum = personList. stream ( ) . map ( Person :: getState ) . reduce ( Integer :: sum ) ;
Optional < Integer > max = personList. stream ( ) . map ( Person :: getState ) . reduce ( Integer :: max ) ;
System . out. println ( sum. get ( ) ) ;
System . out. println ( max. get ( ) ) ;
List < BigDecimal > list = Arrays . asList ( BigDecimal . ONE , BigDecimal . ONE , BigDecimal . TEN ) ;
BigDecimal reduce = list. stream ( ) . reduce ( BigDecimal . ZERO , BigDecimal :: add ) ;
List < Integer > list = Arrays . asList ( 1 , 2 , 3 , 4 ) ;
Optional < Integer > reduce = list. stream ( ) . reduce ( ( x, y) -> x+ y) ;
7,比较
List < String > list = Arrays . asList ( "zhangsan" , "lisi" , "wangwu" , "sunliu" ) ;
Optional < String > max = list. stream ( ) . max ( String :: compareTo ) ;
System . out. println ( max. get ( ) ) ;
List < Integer > list = Arrays . asList ( 1 , 17 , 27 , 7 ) ;
Optional < Integer > max = list. stream ( ) . max ( Integer :: compareTo ) ;
System . out. println ( max. get ( ) ) ;
List < String > list = Arrays . asList ( "zhangsan" , "lisi" , "wangwu" , "sunliu" ) ;
Comparator < ? super String > comparator = Comparator . comparing ( String :: length ) ;
Optional < String > max = list. stream ( ) . max ( comparator) ;
System . out. println ( max. get ( ) ) ;
8,匹配过滤
List < Integer > list = Arrays . asList ( 7 , 6 , 9 , 3 , 8 , 2 , 1 ) ;
List < Integer > list1 = list. stream ( ) . filter ( x -> x > 6 ) . collect ( Collectors . toList ( ) ) ;
Optional < Integer > findFirst = list. stream ( ) . filter ( x -> x > 6 ) . findFirst ( ) ;
Optional < Integer > findAny = list. parallelStream ( ) . filter ( x -> x > 6 ) . findAny ( ) ;
boolean anyMatch = list. stream ( ) . anyMatch ( x -> x < 6 ) ;
boolean allMatchCheck = list. stream ( ) . allMatch ( e -> ( CheckStatusEnum . CHECK_PASS . equals ( e. getCheckStatus ( ) ) || StatusEnum . PASS . equals ( e. getStatus ( ) ) ) ) ;
Optional < Person > findFirst = personList. stream ( ) . filter ( x -> x. getSalary ( ) > 3000 ) . findFirst ( ) ;
System . out. println ( findFirst. get ( ) ) ;
Optional < Person > findAny = personList. parallelStream ( ) . filter ( x -> x. getSalary ( ) > 3000 ) . findAny ( ) ;
System . out. println ( findAny. get ( ) ) ;
boolean anyMatch = personList. stream ( ) . anyMatch ( x -> x. getSalary ( ) > 3000 ) ;
System . out. println ( anyMatch) ;
List < Integer > findFirst = personList. stream ( ) . filter ( x -> x. getSalary ( ) > 3000 ) . map ( Person :: getSalary ) . collect ( Collectors . toList ( ) ) ;
9,其他应用
Map < Long , SysLogDTO > logMap = logList. parallelStream ( ) . filter ( Objects :: nonNull ) .
collect ( Collectors . groupingBy ( a -> a. getOperatorId ( ) ,
Collectors . collectingAndThen ( Collectors . reducing ( ( c1, c2) -> ( c1. getOperateTime ( ) . getTime ( ) > c2. getOperateTime ( ) . getTime ( ) ) ? c1 : c2) , Optional :: get ) ) ) ;
List < Person > wList = new ArrayList < Person > ( ) ;
Person projectChart1 = new Person ( ) ;
projectChart1. setName ( "li" ) ;
projectChart1. setCode ( "X0001" ) ;
projectChart1. setState ( 2 ) ;
projectChart1. setSalary ( 3000 ) ;
wList. add ( projectChart1) ;
List < Person > common = personList. stream ( ) . filter ( item -> wList. contains ( item) ) . collect ( Collectors . toList ( ) ) ;
System . out. println ( common) ;
if ( ToolUtil . isEmpty ( matchList) || matchList. size ( ) == 0 ) {
personList. remove ( taskEntity) ;
}
List < TaskEntity > listB = new LinkedList < > ( ) ;
listA. stream ( ) . forEach (
taskEntity -> {
TaskEntity task = new TaskEntity ( ) ;
BeanUtils . copyProperties ( taskEntity, task) ;
listB. add ( task) ;
}
) ;
10,参考连接
https://blog.csdn.net/qq_45228323/article/details/121328790
Java 8 stream的详细用法
Java 8 Stream常用方法学习