1, 操作list对象中的属性
List < Person > personList = new ArrayList < Person > ( ) ;
personList. add ( new Person ( "Tom" , 8900 , 23 , "male" , "New York" ) ) ;
personList. add ( new Person ( "Jack" , 7000 , 25 , "male" , "Washington" ) ) ;
personList. add ( new Person ( "Lily" , 7800 , 21 , "female" , "Washington" ) ) ;
personList. add ( new Person ( "Anni" , 8200 , 24 , "female" , "New York" ) ) ;
personList. add ( new Person ( "Owen" , 9500 , 25 , "male" , "New York" ) ) ;
personList. add ( new Person ( "Alisa" , 7900 , 26 , "female" , "New York" ) ) ;
List < Person > personListNew = personList. stream ( ) . map ( person -> {
Person personNew = new Person ( person. getName ( ) , 0 , 0 , null , null ) ;
personNew. setSalary ( person. getSalary ( ) + 10000 ) ;
return personNew;
} ) . collect ( Collectors . toList ( ) ) ;
System . out. println ( "一次改动前:" + personList. get ( 0 ) . getName ( ) + "-->" + personList. get ( 0 ) . getSalary ( ) ) ;
System . out. println ( "一次改动后:" + personListNew. get ( 0 ) . getName ( ) + "-->" + personListNew. get ( 0 ) . getSalary ( ) ) ;
List < Person > personListNew2 = personList. stream ( ) . map ( person -> {
person. setSalary ( person. getSalary ( ) + 10000 ) ;
return person;
} ) . collect ( Collectors . toList ( ) ) ;
System . out. println ( "二次改动前:" + personList. get ( 0 ) . getName ( ) + "-->" + personListNew. get ( 0 ) . getSalary ( ) ) ;
System . out. println ( "二次改动后:" + personListNew2. get ( 0 ) . getName ( ) + "-->" + personListNew. get ( 0 ) . getSalary ( ) ) ;
2,分组
List < Person > personList = new ArrayList < Person > ( ) ;
personList. add ( new Person ( "Tom" , 8900 , "male" , "New York" ) ) ;
personList. add ( new Person ( "Jack" , 7000 , "male" , "Washington" ) ) ;
personList. add ( new Person ( "Lily" , 7800 , "female" , "Washington" ) ) ;
personList. add ( new Person ( "Anni" , 8200 , "female" , "New York" ) ) ;
personList. add ( new Person ( "Owen" , 9500 , "male" , "New York" ) ) ;
personList. add ( new Person ( "Alisa" , 7900 , "female" , "New York" ) ) ;
Map < Boolean , List < Person > > part = personList. stream ( ) . collect ( Collectors . partitioningBy ( x -> x. getSalary ( ) > 8000 ) ) ;
Map < String , List < Person > > group = personList. stream ( ) . collect ( Collectors . groupingBy ( Person :: getSex ) ) ;
Map < String , Map < String , List < Person > > > group2 = personList. stream ( ) . collect ( Collectors . groupingBy ( Person :: getSex , Collectors . groupingBy ( Person :: getArea ) ) ) ;
System . out. println ( "员工按薪资是否大于8000分组情况:" + part) ;
System . out. println ( "员工按性别分组情况:" + group) ;
System . out. println ( "员工按性别、地区:" + group2) ;
3,合并两个流
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 > newList = Stream . concat ( stream1, stream2) . distinct ( ) . collect ( Collectors . toList ( ) ) ;
List < Integer > collect = Stream . iterate ( 1 , x -> x + 2 ) . limit ( 10 ) . collect ( Collectors . toList ( ) ) ;
List < Integer > collect2 = Stream . iterate ( 1 , x -> x + 2 ) . skip ( 1 ) . limit ( 5 ) . collect ( Collectors . toList ( ) ) ;
System . out. println ( "流合并:" + newList) ;
System . out. println ( "limit:" + collect) ;
System . out. println ( "skip:" + collect2) ;
4,获取Integer集合中的最大值
List < Integer > list = Arrays . asList ( 7 , 6 , 9 , 4 , 11 , 6 ) ;
Optional < Integer > max = list. stream ( ) . max ( Integer :: compareTo ) ;
Optional < Integer > max2 = list. stream ( ) . max ( new Comparator < Integer > ( ) {
@Override
public int compare ( Integer o1, Integer o2) {
return o1. compareTo ( o2) ;
}
} ) ;
System . out. println ( "自然排序的最大值:" + max. get ( ) ) ;
System . out. println ( "自定义排序的最大值:" + max2. get ( ) ) ;
5,获取String集合中最长的元素
List < String > list = Arrays . asList ( "adnm" , "admmt" , "pot" , "xbangd" , "weoujgsd" ) ;
Optional < String > max = list. stream ( ) . max ( Comparator . comparing ( String :: length ) ) ;
System . out. println ( "最长的字符串:" + max. get ( ) ) ;
6,求list中对象莫个属性的最大值或者属性的总和
List < Person > personList = new ArrayList < Person > ( ) ;
personList. add ( new Person ( "Tom" , 8900 , 23 , "male" , "New York" ) ) ;
personList. add ( new Person ( "Jack" , 7000 , 25 , "male" , "Washington" ) ) ;
personList. add ( new Person ( "Lily" , 7800 , 21 , "female" , "Washington" ) ) ;
personList. add ( new Person ( "Anni" , 8200 , 24 , "female" , "New York" ) ) ;
personList. add ( new Person ( "Owen" , 9500 , 25 , "male" , "New York" ) ) ;
personList. add ( new Person ( "Alisa" , 7900 , 26 , "female" , "New York" ) ) ;
Optional < Integer > sumSalary = personList. stream ( ) . map ( Person :: getSalary ) . reduce ( Integer :: sum ) ;
Integer sumSalary2 = personList. stream ( ) . reduce ( 0 , ( sum, p) -> sum += p. getSalary ( ) ,
( sum1, sum2) -> sum1 + sum2) ;
Integer sumSalary3 = personList. stream ( ) . reduce ( 0 , ( sum, p) -> sum += p. getSalary ( ) , Integer :: sum ) ;
Integer maxSalary = personList. stream ( ) . reduce ( 0 , ( max, p) -> max > p. getSalary ( ) ? max : p. getSalary ( ) ,
Integer :: max ) ;
Integer maxSalary2 = personList. stream ( ) . reduce ( 0 , ( max, p) -> max > p. getSalary ( ) ? max : p. getSalary ( ) ,
( max1, max2) -> max1 > max2 ? max1 : max2) ;
System . out. println ( "工资之和:" + sumSalary. get ( ) + "," + sumSalary2 + "," + sumSalary3) ;
System . out. println ( "最高工资:" + maxSalary + "," + maxSalary2) ;
7, 取出list中符合条件的对象中的一个属性
List < Person > personList = new ArrayList < > ( ) ;
personList. add ( new Person ( "Tom" , 8900 , 23 , "male" , "New York" ) ) ;
personList. add ( new Person ( "Jack" , 7000 , 25 , "male" , "Washington" ) ) ;
personList. add ( new Person ( "Lily" , 7800 , 21 , "female" , "Washington" ) ) ;
personList. add ( new Person ( "Anni" , 8200 , 24 , "female" , "New York" ) ) ;
personList. add ( new Person ( "Owen" , 9500 , 25 , "male" , "New York" ) ) ;
personList. add ( new Person ( "Alisa" , 7900 , 26 , "female" , "New York" ) ) ;
List < String > fiterList = personList. stream ( ) . filter ( x -> x. getSalary ( ) > 8000 ) . map ( Person :: getName )
. collect ( Collectors . toList ( ) ) ;
System . out. print ( "高于8000的员工姓名:" + fiterList) ;
8,取出对象中莫个最大的值
List < Person > personList = new ArrayList < Person > ( ) ;
personList. add ( new Person ( "Tom" , 8900 , 23 , "male" , "New York" ) ) ;
personList. add ( new Person ( "Jack" , 7000 , 25 , "male" , "Washington" ) ) ;
personList. add ( new Person ( "Lily" , 7800 , 21 , "female" , "Washington" ) ) ;
personList. add ( new Person ( "Anni" , 8200 , 24 , "female" , "New York" ) ) ;
personList. add ( new Person ( "Owen" , 9500 , 25 , "male" , "New York" ) ) ;
personList. add ( new Person ( "Alisa" , 7900 , 26 , "female" , "New York" ) ) ;
Optional < Person > max = personList. stream ( ) . max ( Comparator . comparingInt ( Person :: getSalary ) ) ;
System . out. println ( "员工工资最大值:" + max. get ( ) . getSalary ( ) ) ;
9,是否包涵符合特定条件的元素
List < Integer > list = Arrays . asList ( 7 , 6 , 9 , 3 , 8 , 2 , 1 ) ;
boolean anyMatch = list. stream ( ) . anyMatch ( x -> x > 6 ) ;
10,统计对象中的信息
List < Person > personList = new ArrayList < Person > ( ) ;
personList. add ( new Person ( "Tom" , 8900 , 23 , "male" , "New York" ) ) ;
personList. add ( new Person ( "Jack" , 7000 , 25 , "male" , "Washington" ) ) ;
personList. add ( new Person ( "Lily" , 7800 , 21 , "female" , "Washington" ) ) ;
Long count = personList. stream ( ) . collect ( Collectors . counting ( ) ) ;
Double average = personList. stream ( ) . collect ( Collectors . averagingDouble ( Person :: getSalary ) ) ;
Optional < Integer > max = personList. stream ( ) . map ( Person :: getSalary ) . collect ( Collectors . maxBy ( Integer :: compare ) ) ;
Integer sum = personList. stream ( ) . collect ( Collectors . summingInt ( Person :: getSalary ) ) ;
DoubleSummaryStatistics collect = personList. stream ( ) . collect ( Collectors . summarizingDouble ( Person :: getSalary ) ) ;
System . out. println ( "员工总数:" + count) ;
System . out. println ( "员工平均工资:" + average) ;
System . out. println ( "员工工资总和:" + sum) ;
System . out. println ( "员工工资所有统计:" + collect) ;
11,一个List中的部分字段赋值给另一个List
demoList. stream ( ) . map ( result -> new DemoListVO ( result. getKeyNo ( ) , result. getName ( ) ) ) . collect ( Collectors . toList ( ) ) ;