1.计算流中所有数之和
static void test3 ( ) {
List < Integer > list = new ArrayList < > ( ) ;
list. add ( 12 ) ;
list. add ( 2 ) ;
int sum = list. stream ( ) . mapToInt ( f -> f) . sum ( ) ;
System . out. println ( sum) ;
}
2.filte map 练习
static void test1 ( ) {
List < HashMap > list = new ArrayList ( ) ;
HashMap map1 = new HashMap ( ) ;
map1. put ( "nationality" , "中国" ) ;
map1. put ( "name" , "小志1" ) ;
map1. put ( "age" , 21 ) ;
HashMap map2 = new HashMap ( ) ;
map2. put ( "nationality" , "俄罗斯" ) ;
map2. put ( "name" , "小志2" ) ;
map2. put ( "age" , 19 ) ;
HashMap map3 = new HashMap ( ) ;
map3. put ( "nationality" , "巴基斯坦" ) ;
map3. put ( "name" , "小志3" ) ;
map3. put ( "age" , 23 ) ;
list. add ( map1) ;
list. add ( map2) ;
list. add ( map3) ;
List < String > collect = list. stream ( ) .
filter ( f -> Integer . parseInt ( f. get ( "age" ) . toString ( ) ) > 20 ) .
map ( f -> f. get ( "nationality" ) . toString ( ) ) .
collect ( Collectors . toList ( ) ) ;
System . out. println ( collect) ;
}
3.重构嵌套for循环
static void test2 ( ) {
List < HashMap > list = new ArrayList < > ( ) ;
HashMap map1 = new HashMap ( ) ;
map1. put ( "name" , "小志1" ) ;
List < HashMap > song1 = new ArrayList ( ) ;
HashMap songMap1 = new HashMap ( ) ;
songMap1. put ( "name" , "歌曲1" ) ;
songMap1. put ( "length" , "20" ) ;
HashMap songMap2 = new HashMap ( ) ;
songMap2. put ( "name" , "歌曲2" ) ;
songMap2. put ( "length" , "70" ) ;
map1. put ( "song" , song1) ;
song1. add ( songMap1) ;
song1. add ( songMap2) ;
HashMap map2 = new HashMap ( ) ;
map2. put ( "name" , "小志2" ) ;
List < HashMap > song2 = new ArrayList ( ) ;
HashMap songMap3 = new HashMap ( ) ;
songMap3. put ( "name" , "歌曲3" ) ;
songMap3. put ( "length" , "50" ) ;
HashMap songMap4 = new HashMap ( ) ;
songMap4. put ( "name" , "歌曲4" ) ;
songMap4. put ( "length" , "80" ) ;
map2. put ( "song" , song2) ;
song2. add ( songMap3) ;
song2. add ( songMap4) ;
list. add ( map1) ;
list. add ( map2) ;
List nameList1 = new ArrayList ( ) ;
for ( HashMap map : list) {
for ( HashMap song : ( List < HashMap > ) map. get ( "song" ) ) {
if ( Integer . parseInt ( song. get ( "length" ) . toString ( ) ) >= 60 ) {
nameList1. add ( song. get ( "name" ) ) ;
}
}
}
List < String > nameList2 = list. stream ( )
. flatMap ( f -> ( ( List < HashMap > ) f. get ( "song" ) ) . stream ( ) )
. filter ( f -> Integer . parseInt ( f. get ( "length" ) . toString ( ) ) >= 60 )
. map ( f -> f. get ( "name" ) . toString ( ) )
. collect ( Collectors . toList ( ) ) ;
System . out. println ( nameList2) ;
}
4.集合中map中有age sex name ,返回map只带age name的集合
static void test4 ( ) {
List < HashMap > list = new ArrayList ( ) ;
HashMap map1 = new HashMap ( ) ;
map1. put ( "age" , 12 ) ;
map1. put ( "sex" , "男" ) ;
map1. put ( "name" , "小志1" ) ;
HashMap map2 = new HashMap ( ) ;
map2. put ( "age" , 18 ) ;
map2. put ( "sex" , "女" ) ;
map2. put ( "name" , "小志2" ) ;
list. add ( map1) ;
list. add ( map2) ;
List < HashMap > collect = list. stream ( ) . map ( f -> {
HashMap map = new HashMap ( ) ;
map. put ( "age" , f. get ( "age" ) ) ;
map. put ( "name" , f. get ( "name" ) ) ;
return map;
} ) . collect ( Collectors . toList ( ) ) ;
System . out. println ( collect) ;
}
5. 计算一个字符串中小写字符的个数(filte)
static void test5 ( ) {
String s = "Abcdef" ;
long count = s. chars ( ) . boxed ( ) . filter ( f -> Character . isLowerCase ( f) ) . count ( ) ;
System . out. println ( count) ;
}
6. 计算一个字符串中小写字符的个数(reduce)
static void test6 ( ) {
String s = "Abcdef" ;
Integer reduce = s. chars ( ) . boxed ( ) . reduce ( 0 , ( a, b) -> {
if ( Character . isLowerCase ( b. intValue ( ) ) ) {
a++ ;
}
return a;
} ) ;
System . out. println ( reduce) ;
}
7.找出length最大的map
static void test7 ( ) {
HashMap map1 = new HashMap ( ) ;
map1. put ( "length" , 20 ) ;
HashMap map2 = new HashMap ( ) ;
map2. put ( "length" , 40 ) ;
HashMap map3 = new HashMap ( ) ;
map3. put ( "length" , 30 ) ;
List < HashMap > list = Arrays . asList ( map1, map2, map3) ;
Optional < HashMap > optional = list. stream ( ) . collect ( Collectors . maxBy ( Comparator . comparing ( f -> f. get ( "length" ) . toString ( ) ) ) ) ;
System . out. println ( optional. get ( ) ) ;
}
8.计算平均值
static void test8 ( ) {
HashMap map1 = new HashMap ( ) ;
map1. put ( "length" , 20 ) ;
HashMap map2 = new HashMap ( ) ;
map2. put ( "length" , 40 ) ;
HashMap map3 = new HashMap ( ) ;
map3. put ( "length" , 30 ) ;
List < HashMap > list = Arrays . asList ( map1, map2, map3) ;
Double length = list. stream ( ) . collect ( Collectors . averagingInt ( f -> Integer . parseInt ( f. get ( "length" ) . toString ( ) ) ) ) ;
System . out. println ( length) ;
}
9.数据分块
static void test9 ( ) {
HashMap map1 = new HashMap ( ) ;
map1. put ( "name" , "张三" ) ;
map1. put ( "sex" , "男" ) ;
HashMap map2 = new HashMap ( ) ;
map2. put ( "name" , "李四" ) ;
map2. put ( "sex" , "男" ) ;
HashMap map3 = new HashMap ( ) ;
map3. put ( "name" , "王五" ) ;
map3. put ( "sex" , "不明" ) ;
HashMap map4 = new HashMap ( ) ;
map4. put ( "name" , "老六" ) ;
map4. put ( "sex" , "女" ) ;
List < HashMap > list = Arrays . asList ( map1, map2, map3, map4) ;
Map < Boolean , List < HashMap > > sex = list. stream ( ) . collect ( Collectors . partitioningBy ( f -> "男" . equals ( f. get ( "sex" ) ) ) ) ;
System . out. println ( sex) ;
}
10.数据分组
static void test10 ( ) {
HashMap map1 = new HashMap ( ) ;
map1. put ( "name" , "张三" ) ;
map1. put ( "sex" , "男" ) ;
HashMap map2 = new HashMap ( ) ;
map2. put ( "name" , "李四" ) ;
map2. put ( "sex" , "男" ) ;
HashMap map3 = new HashMap ( ) ;
map3. put ( "name" , "王五" ) ;
map3. put ( "sex" , "不明" ) ;
HashMap map4 = new HashMap ( ) ;
map4. put ( "name" , "老六" ) ;
map4. put ( "sex" , "女" ) ;
List < HashMap > list = Arrays . asList ( map1, map2, map3, map4) ;
Map < String , List < HashMap > > sex = list. stream ( ) . collect ( Collectors . groupingBy ( f -> f. get ( "sex" ) . toString ( ) ) ) ;
System . out. println ( sex) ;
}
11.拼接字符串
static void test11 ( ) {
List < String > list= Arrays . asList ( "j" , "a" , "v" , "a" , "8" ) ;
String s = list. stream ( ) . collect ( Collectors . joining ( "," , "[" , "]" ) ) ;
System . out. println ( s) ;
}
12.组合收集器(Collectors的groupingBy 和 counting 统计分组后的个数)
static void test12 ( ) {
HashMap map1 = new HashMap ( ) ;
map1. put ( "name" , "张三" ) ;
map1. put ( "sex" , "男" ) ;
HashMap map2 = new HashMap ( ) ;
map2. put ( "name" , "李四" ) ;
map2. put ( "sex" , "男" ) ;
HashMap map3 = new HashMap ( ) ;
map3. put ( "name" , "王五" ) ;
map3. put ( "sex" , "不明" ) ;
HashMap map4 = new HashMap ( ) ;
map4. put ( "name" , "老六" ) ;
map4. put ( "sex" , "女" ) ;
List < HashMap > list = Arrays . asList ( map1, map2, map3, map4) ;
Map < String , Long > sex = list. stream ( ) . collect ( Collectors . groupingBy ( f -> f. get ( "sex" ) . toString ( ) , Collectors . counting ( ) ) ) ;
System . out. println ( sex) ;
}
13.组合收集器(Collectors的groupingBy 和 mapping 根据性别分组获取性别对应的名字)
static void test13 ( ) {
HashMap map1 = new HashMap ( ) ;
map1. put ( "name" , "张三" ) ;
map1. put ( "sex" , "男" ) ;
HashMap map2 = new HashMap ( ) ;
map2. put ( "name" , "李四" ) ;
map2. put ( "sex" , "男" ) ;
HashMap map3 = new HashMap ( ) ;
map3. put ( "name" , "王五" ) ;
map3. put ( "sex" , "不明" ) ;
HashMap map4 = new HashMap ( ) ;
map4. put ( "name" , "老六" ) ;
map4. put ( "sex" , "女" ) ;
List < HashMap > list = Arrays . asList ( map1, map2, map3, map4) ;
Map < String , List < Object > > collect = list. stream ( ) .
collect ( Collectors .
groupingBy ( f -> f. get ( "sex" ) . toString ( ) , Collectors . mapping ( f -> f. get ( "name" ) , Collectors . toList ( ) ) ) ) ;
System . out. println ( collect) ;
}