生活随笔
收集整理的這篇文章主要介紹了
Java基础day23
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
Java基礎(chǔ)day23 Java基礎(chǔ)day23-函數(shù)接口&stream流 1.函數(shù)式接口 1.1函數(shù)式接口概述 1.2函數(shù)式接口作為方法的參數(shù) 1.3函數(shù)式接口作為方法的返回值 1.4常用函數(shù)式接口之Supplier 1.5Supplier接口練習(xí)之獲取最大值 1.6常用函數(shù)式接口之Consumer 1.7Consumer接口練習(xí)之按要求打印信息 1.8常用函數(shù)式接口之Predicate 1.9Predicate接口練習(xí)之篩選滿足條件數(shù)據(jù) 1.10常用函數(shù)式接口之Function 1.11Function接口練習(xí)之按照指定要求操作數(shù)據(jù) 2.Strem流 2.2Stream流的常見生成方式 2.6Stream流的收集操作
Java基礎(chǔ)day23-函數(shù)接口&stream流
1.函數(shù)式接口
1.1函數(shù)式接口概述
概念 有且僅有一個(gè)抽象方法的接口 如何檢測(cè)一個(gè)接口是不是函數(shù)式接口 @FunctionalInterface 放在接口定義的上方:如果接口是函數(shù)式接口,編譯通過;如果不是,編譯失敗 注意事項(xiàng) 我們自己定義函數(shù)式接口的時(shí)候,@FunctionalInterface是可選的,就算我不寫這個(gè)注解,只要保證滿足函數(shù) 式接口定義的條件,也照樣是函數(shù)式接口。但是,建議加上該注解
1.2函數(shù)式接口作為方法的參數(shù)
需求描述 定義一個(gè)類(RunnableDemo),在類中提供兩個(gè)方法 一個(gè)方法是:startThread(Runnable r) 方法參數(shù)Runnable是一個(gè)函數(shù)式接口 一個(gè)方法是主方法,在主方法中調(diào)用startThread方法 代碼演示
public class RunnableDemo { public static void main ( String
[ ] args
) { startThread ( new Runnable ( ) { @Override public void run ( ) { System
. out
. println ( Thread
. currentThread ( ) . getName ( ) + "線程啟動(dòng)" ) ; } } ) ; startThread ( ( ) - > System
. out
. println ( Thread
. currentThread ( ) . getName ( ) + "線程啟動(dòng)" ) ) ; } private static void startThread ( Runnable r
) { new Thread ( r
) . start ( ) ; }
}
1.3函數(shù)式接口作為方法的返回值
需求描述 定義一個(gè)類(ComparatorDemo),在類中提供兩個(gè)方法 一個(gè)方法是:Comparator getComparator() 方法返回值Comparator是一個(gè)函數(shù)式接口 一個(gè)方法是主方法,在主方法中調(diào)用getComparator方法 代碼演示
import java
. util
. ArrayList
;
import java
. util
. Collections
;
import java
. util
. Comparator
; public class ComparatorDemo { public static void main ( String
[ ] args
) { ArrayList
< String> array
= new ArrayList < String> ( ) ; array
. add ( "cccc" ) ; array
. add ( "aa" ) ; array
. add ( "b" ) ; array
. add ( "ddd" ) ; System
. out
. println ( "排序前" + array
) ; Collections
. sort ( array
, getComparator ( ) ) ; System
. out
. println ( "排序后" + array
) ; } private static Comparator
< String> getComparator ( ) { return ( s1
, s2
) - > s1
. length ( ) - s2
. length ( ) ; }
}
1.4常用函數(shù)式接口之Supplier
Supplier接口 Supplier接口也被稱為生產(chǎn)型接口,如果我們指定了接口的泛型是什么類型,那么接口中的get方法就會(huì)生產(chǎn) 什么類型的數(shù)據(jù)供我們使用。 常用方法 只有一個(gè)無參的方法
方法名說明 T get() 按照某種實(shí)現(xiàn)邏輯(由Lambda表達(dá)式實(shí)現(xiàn))返回一個(gè)數(shù)據(jù)
import java
. util
. function
. Supplier
; public class SupplierDemo { public static void main ( String
[ ] args
) { String s
= getString ( ( ) - > "林青霞" ) ; System
. out
. println ( s
) ; Integer i
= getinteger ( ( ) - > 30 ) ; System
. out
. println ( i
) ; } private static Integer
getinteger ( Supplier
< Integer> sup
) { return sup
. get ( ) ; } private static String
getString ( Supplier
< String> sup
) { return sup
. get ( ) ; }
}
1.5Supplier接口練習(xí)之獲取最大值
案例需求 定義一個(gè)類(SupplierTest),在類中提供兩個(gè)方法 一個(gè)方法是:int getMax(Supplier sup) 用于返回一個(gè)int數(shù)組中的最大值 一個(gè)方法是主方法,在主方法中調(diào)用getMax方法 示例代碼
import java
. util
. function
. Supplier
; public class SupplierTest { public static void main ( String
[ ] args
) { int [ ] arr
= { 19 , 50 , 28 , 37 , 46 } ; int maxvalue
= getmax ( ( ) - > { int max
= arr
[ 0 ] ; for ( int i
= 1 ; i
< arr
. length
; i
++ ) { if ( arr
[ i
] > max
) { max
= arr
[ i
] ; } } return max
; } ) ; System
. out
. println ( maxvalue
) ; } private static int getmax ( Supplier
< Integer> sup
) { return sup
. get ( ) ; }
}
1.6常用函數(shù)式接口之Consumer
Consumer接口 Consumer接口也被稱為消費(fèi)型接口,它消費(fèi)的數(shù)據(jù)的數(shù)據(jù)類型由泛型指定 常用方法 Consumer:包含兩個(gè)方法
方法名說明 void accept(T t) 對(duì)給定的參數(shù)執(zhí)行此操作 default Consumer andThen(Consumer after) 返回一個(gè)組合的Consumer,依次執(zhí)行此操作,然后執(zhí)行after操作
import java
. util
. function
. Consumer
; public class ConsumerDemo { public static void main ( String
[ ] args
) { operatorString ( "林青霞" , s
- > System
. out
. println ( s
) ) ; operatorString ( "林青霞" , s
- > System
. out
. println ( new StringBuilder ( s
) . reverse ( ) . toString ( ) ) ) ; System
. out
. println ( "----" ) ; operatorString ( "林青霞" , s
- > System
. out
. println ( s
) , s
- > System
. out
. println ( new StringBuilder ( s
) . reverse ( ) . toString ( ) ) ) ; } private static void operatorString ( String name
, Consumer
< String> con1
, Consumer
< String> con2
) {
con1
. andThen ( con2
) . accept ( name
) ; } private static void operatorString ( String name
, Consumer
< String> con
) { con
. accept ( name
) ; }
}
1.7Consumer接口練習(xí)之按要求打印信息
案例需求 String[] strArray = {“林青霞,30”, “張曼玉,35”, “王祖賢,33”}; 字符串?dāng)?shù)組中有多條信息,請(qǐng)按照格式:“姓名:XX,年齡:XX"的格式將信息打印出來 要求: 把打印姓名的動(dòng)作作為第一個(gè)Consumer接口的Lambda實(shí)例 把打印年齡的動(dòng)作作為第二個(gè)Consumer接口的Lambda實(shí)例 將兩個(gè)Consumer接口按照順序組合到一起使用 示例代碼
import java
. util
. function
. Consumer
; public class ConsumerTest { public static void main ( String
[ ] args
) { String
[ ] strArray
= { "林青霞,30" , "張曼玉,35" , "王祖賢,33" } ; printInfo ( strArray
, str
- > System
. out
. print ( "姓名:" + str
. split ( "," ) [ 0 ] ) , str
- > System
. out
. println ( ",年齡:" + Integer
. parseInt ( str
. split ( "," ) [ 1 ] ) ) ) ; } private static void printInfo ( String
[ ] strArray
, Consumer
< String> con1
, Consumer
< String> con2
) { for ( String str
: strArray
) { con1
. andThen ( con2
) . accept ( str
) ; } }
}
1.8常用函數(shù)式接口之Predicate
Predicate接口 Predicate接口通常用于判斷參數(shù)是否滿足指定的條件 常用方法
方法名說明 boolean test(T t) 對(duì)給定的參數(shù)進(jìn)行判斷(判斷邏輯由Lambda表達(dá)式實(shí)現(xiàn)),返回一個(gè)布爾值 default Predicate negate() 返回一個(gè)邏輯的否定,對(duì)應(yīng)邏輯非 default Predicate and(Predicate other) 返回一個(gè)組合判斷,對(duì)應(yīng)短路與 default Predicate or(Predicate other) 返回一個(gè)組合判斷,對(duì)應(yīng)短路或
import java
. util
. function
. Predicate
; public class PredicateDemo02 { public static void main ( String
[ ] args
) { boolean b1
= checkString ( "hello" , s
- > s
. length ( ) > 8 ) ; System
. out
. println ( b1
) ; boolean b2
= checkString ( "helloworld" , s
- > s
. length ( ) > 8 ) ; System
. out
. println ( b2
) ; boolean b3
= checkString ( "hello" , s
- > s
. length ( ) > 8 , s
- > s
. length ( ) < 15 ) ; System
. out
. println ( b3
) ; boolean b4
= checkString ( "helloworld" , s
- > s
. length ( ) > 8 , s
- > s
. length ( ) < 15 ) ; System
. out
. println ( b4
) ; } private static boolean checkString ( String s
, Predicate
< String> pre
) { return pre
. negate ( ) . test ( s
) ; } private static boolean checkString ( String s
, Predicate
< String> pre1
, Predicate
< String> pre2
) { return pre1
. and ( pre2
) . test ( s
) ; }
}
1.9Predicate接口練習(xí)之篩選滿足條件數(shù)據(jù)
練習(xí)描述 String[] strArray = {“林青霞,30”, “柳巖,34”, “張曼玉,35”, “貂蟬,31”, “王祖賢,33”}; 字符串?dāng)?shù)組中有多條信息,請(qǐng)通過Predicate接口的拼裝將符合要求的字符串篩選到集合ArrayList中,并 遍歷ArrayList集合 同時(shí)滿足如下要求:姓名長度大于2;年齡大于33 分析 有兩個(gè)判斷條件,所以需要使用兩個(gè)Predicate接口,對(duì)條件進(jìn)行判斷 必須同時(shí)滿足兩個(gè)條件,所以可以使用and方法連接兩個(gè)判斷條件 示例代碼
import java
. util
. ArrayList
;
import java
. util
. function
. Predicate
; public class PredicateTest { public static void main ( String
[ ] args
) { String
[ ] strArray
= { "林青霞,30" , "柳巖,34" , "張曼玉,35" , "貂蟬,31" , "王祖 賢,33" } ; ArrayList
< String> array
= myFilter ( strArray
, s
- > s
. split ( "," ) [ 0 ] . length ( ) > 2 , s
- > Integer
. parseInt ( s
. split ( "," ) [ 1 ] ) > 33 ) ; for ( String str
: array
) { System
. out
. println ( str
) ; } } private static ArrayList
< String> myFilter ( String
[ ] strArray
, Predicate
< String> pre1
, Predicate
< String> pre2
) { ArrayList
< String> array
= new ArrayList < String> ( ) ; for ( String str
: strArray
) { if ( pre1
. and ( pre2
) . test ( str
) ) { array
. add ( str
) ; } } return array
; }
}
1.10常用函數(shù)式接口之Function
Function接口 Function<T,R>接口通常用于對(duì)參數(shù)進(jìn)行處理,轉(zhuǎn)換(處理邏輯由Lambda表達(dá)式實(shí)現(xiàn)),然后返回一個(gè)新的值 常用方法
方法名說明 R apply(T t) 將此函數(shù)應(yīng)用于給定的參數(shù) default Function andThen(Function after) 返回一個(gè)組合函數(shù),首先將該函數(shù)應(yīng)用于輸入,然后將after函數(shù)應(yīng)用于結(jié)果
import java
. util
. function
. Function
; public class FunctionDemo { public static void main ( String
[ ] args
) { convert ( "100" , s
- > Integer
. parseInt ( s
) ) ; convert ( 100 , i
- > String
. valueOf ( i
+ 566 ) ) ; convert ( "100" , s
- > Integer
. parseInt ( s
) , i
- > String
. valueOf ( i
+ 566 ) ) ; } private static void convert ( String s
, Function
< String, Integer> fun
) { int i
= fun
. apply ( s
) ; System
. out
. println ( i
) ; } private static void convert ( int i
, Function
< Integer, String> fun
) { String s
= fun
. apply ( i
) ; System
. out
. println ( s
) ; } private static void convert ( String s
, Function
< String, Integer> fun1
, Function
< Integer, String> fun2
) { String ss
= fun1
. andThen ( fun2
) . apply ( s
) ; System
. out
. println ( ss
) ; }
}
1.11Function接口練習(xí)之按照指定要求操作數(shù)據(jù)
練習(xí)描述 String s = “林青霞,30”; 請(qǐng)按照我指定的要求進(jìn)行操作: 1:將字符串截取得到數(shù)字年齡部分 2:將上一步的年齡字符串轉(zhuǎn)換成為int類型的數(shù)據(jù) 3:將上一步的int數(shù)據(jù)加70,得到一個(gè)int結(jié)果,在控制臺(tái)輸出 請(qǐng)通過Function接口來實(shí)現(xiàn)函數(shù)拼接 示例代碼
import java
. util
. function
. Function
; public class FunctionTest { public static void main ( String
[ ] args
) { String s
= "林青霞,30" ; convert ( s
, ss
- > ss
. split ( "," ) [ 1 ] , Integer
: : parseInt
, i
- > i
+ 70 ) ; } private static void convert ( String s
, Function
< String, String> fun1
, Function
< String, Integer> fun2
, Function
< Integer, Integer> fun3
) { int i
= fun1
. andThen ( fun2
) . andThen ( fun3
) . apply ( s
) ; System
. out
. println ( i
) ; }
}
2.Strem流
2.1體驗(yàn)Stream流
案例需求 按照下面的要求完成集合的創(chuàng)建和遍歷 ?創(chuàng)建一個(gè)集合,存儲(chǔ)多個(gè)字符串元素 ?把集合中所有以"張"開頭的元素存儲(chǔ)到一個(gè)新的集合 ?把"張"開頭的集合中的長度為3的元素存儲(chǔ)到一個(gè)新的集合 ?遍歷上一步得到的集合 原始方式示例代碼
import java
. util
. ArrayList
; public class StreamDemo { public static void main ( String
[ ] args
) { ArrayList
< String> list
= new ArrayList < String> ( ) ; list
. add ( "林青霞" ) ; list
. add ( "張曼玉" ) ; list
. add ( "王祖賢" ) ; list
. add ( "柳巖" ) ; list
. add ( "張敏" ) ; list
. add ( "張無忌" ) ; ArrayList
< String> zhangList
= new ArrayList < String> ( ) ; for ( String s
: list
) { if ( s
. startsWith ( "張" ) ) { zhangList
. add ( s
) ; } } System
. out
. println ( zhangList
) ; ArrayList
< String> threeList
= new ArrayList < String> ( ) ; for ( String s
: zhangList
) { if ( s
. length ( ) == 3 ) { threeList
. add ( s
) ; } } System
. out
. println ( threeList
) ; list
. stream ( ) . filter ( s
- > s
. startsWith ( "張" ) ) . filter ( s
- > s
. length ( ) == 3 ) . forEach ( System
. out
: : println
) ; }
}
import java
. util
. ArrayList
; public class Streamdemo2 { public static void main ( String
[ ] args
) { ArrayList
< String> list
= new ArrayList < String> ( ) ; list
. add ( "林青霞" ) ; list
. add ( "張曼玉" ) ; list
. add ( "王祖賢" ) ; list
. add ( "柳巖" ) ; list
. add ( "張敏" ) ; list
. add ( "張無忌" ) ; ArrayList
< String> zhangList
= new ArrayList < String> ( ) ; list
. stream ( ) . filter ( s
- > s
. startsWith ( "張" ) ) . filter ( s
- > s
. length ( ) == 3 ) . forEach ( System
. out
: : println
) ; }
}
Stream流的好處 直接閱讀代碼的字面意思即可完美展示無關(guān)邏輯方式的語義:獲取流、過濾姓張、過濾長度為3、逐一打印 Stream流把真正的函數(shù)式編程風(fēng)格引入到Java中
2.2Stream流的常見生成方式
Stream流的思想 生成Stream流的方式 Collection體系集合 使用默認(rèn)方法stream()生成流, default Stream stream() Map體系集合 把Map轉(zhuǎn)成Set集合,間接的生成流 數(shù)組 通過Stream接口的靜態(tài)方法of(T… values)生成流 代碼演示
import java
. util
. *
;
import java
. util
. stream
. Stream
; public class Streamdemo3 { public static void main ( String
[ ] args
) { List
< String> list
= new ArrayList < String> ( ) ; Stream
< String> listStream
= list
. stream ( ) ; Set
< String> set
= new HashSet < String> ( ) ; Stream
< String> setStream
= set
. stream ( ) ; Map
< String, Integer> map
= new HashMap < String, Integer> ( ) ; Stream
< String> keyStream
= map
. keySet ( ) . stream ( ) ; Stream
< Integer> valueStream
= map
. values ( ) . stream ( ) ; Stream
< Map
. Entry
< String, Integer> > entryStream
= map
. entrySet ( ) . stream ( ) ; String
[ ] strArray
= { "hello" , "world" , "java" } ; Stream
< String> strArrayStream
= Stream
. of ( strArray
) ; Stream
< String> strArrayStream2
= Stream
. of ( "hello" , "world" , "java" ) ; Stream
< Integer> intStream
= Stream
. of ( 10 , 20 , 30 ) ; }
}
2.3Stream流中間操作方法
概念 中間操作的意思是,執(zhí)行完此方法之后,Stream流依然可以繼續(xù)執(zhí)行其他操作。 常見方法
方法名說明 Stream filter(Predicate predicate) 用于對(duì)流中的數(shù)據(jù)進(jìn)行過濾 Stream limit(long maxSize) 返回此流中的元素組成的流,截取前指定參數(shù)個(gè)數(shù)的數(shù)據(jù) Stream skip(long n) 跳過指定參數(shù)個(gè)數(shù)的數(shù)據(jù),返回由該流的剩余元素組成的流 static Stream concat(Stream a, Stream b) 合并a和b兩個(gè)流為一個(gè)流 Stream distinct() 返回由該流的不同元素(根據(jù)Object.equals(Object) )組成的流 Stream sorted() 返回由此流的元素組成的流,根據(jù)自然順序排序 Stream sorted(Comparator comparator) 返回由該流的元素組成的流,根據(jù)提供的Comparator進(jìn)行排序 Stream map(Function mapper) 返回由給定函數(shù)應(yīng)用于此流的元素的結(jié)果組成的流 IntStream mapToInt(ToIntFunction mapper) 返回一個(gè)IntStream其中包含將給定函數(shù)應(yīng)用于此流的元素的結(jié)果
import java
. util
. ArrayList
; public class StreamDemo01 { public static void main ( String
[ ] args
) { ArrayList
< String> list
= new ArrayList < String> ( ) ; list
. add ( "林青霞" ) ; list
. add ( "張曼玉" ) ; list
. add ( "王祖賢" ) ; list
. add ( "柳巖" ) ; list
. add ( "張敏" ) ; list
. add ( "張無忌" ) ; list
. stream ( ) . filter ( s
- > s
. startsWith ( "張" ) ) . forEach ( System
. out
: : println
) ; System
. out
. println ( "--------" ) ; list
. stream ( ) . filter ( s
- > s
. length ( ) == 3 ) . forEach ( System
. out
: : println
) ; System
. out
. println ( "--------" ) ; list
. stream ( ) . filter ( s
- > s
. startsWith ( "張" ) ) . filter ( s
- > s
. length ( ) == 3 ) . forEach ( System
. out
: : println
) ; }
}
import java
. util
. ArrayList
; public class StreamDemo02 { public static void main ( String
[ ] args
) { ArrayList
< String> list
= new ArrayList < String> ( ) ; list
. add ( "林青霞" ) ; list
. add ( "張曼玉" ) ; list
. add ( "王祖賢" ) ; list
. add ( "柳巖" ) ; list
. add ( "張敏" ) ; list
. add ( "張無忌" ) ; list
. stream ( ) . limit ( 3 ) . forEach ( System
. out
: : println
) ; System
. out
. println ( "--------" ) ; list
. stream ( ) . skip ( 3 ) . forEach ( System
. out
: : println
) ; System
. out
. println ( "--------" ) ; list
. stream ( ) . skip ( 2 ) . limit ( 2 ) . forEach ( System
. out
: : println
) ; }
}
import java
. util
. ArrayList
;
import java
. util
. stream
. Stream
; public class StreamDemo03 { public static void main ( String
[ ] args
) { ArrayList
< String> list
= new ArrayList < String> ( ) ; list
. add ( "林青霞" ) ; list
. add ( "張曼玉" ) ; list
. add ( "王祖賢" ) ; list
. add ( "柳巖" ) ; list
. add ( "張敏" ) ; list
. add ( "張無忌" ) ; Stream
< String> s1
= list
. stream ( ) . limit ( 4 ) ; Stream
< String> s2
= list
. stream ( ) . skip ( 2 ) ; Stream
. concat ( s1
, s2
) . distinct ( ) . forEach ( System
. out
: : println
) ; }
}
import java
. util
. ArrayList
; public class StreamDemo04 { public static void main ( String
[ ] args
) { ArrayList
< String> list
= new ArrayList < String> ( ) ; list
. add ( "linqingxia" ) ; list
. add ( "zhangmanyu" ) ; list
. add ( "wangzuxian" ) ; list
. add ( "liuyan" ) ; list
. add ( "zhangmin" ) ; list
. add ( "zhangwuji" ) ; list
. stream ( ) . sorted ( ( s1
, s2
) - > { int num
= s1
. length ( ) - s2
. length ( ) ; int num2
= num
== 0 ? s1
. compareTo ( s2
) : num
; return num
; } ) . forEach ( System
. out
: : println
) ; }
}
import java
. util
. ArrayList
; public class StreamDemo05 { public static void main ( String
[ ] args
) { ArrayList
< String> list
= new ArrayList < String> ( ) ; list
. add ( "10" ) ; list
. add ( "20" ) ; list
. add ( "30" ) ; list
. add ( "40" ) ; list
. add ( "50" ) ;
list
. stream ( ) . mapToInt ( Integer
: : parseInt
) . forEach ( System
. out
: : println
) ; int sum
= list
. stream ( ) . mapToInt ( Integer
: : parseInt
) . sum ( ) ; System
. out
. println ( sum
) ; }
}
2.4Stream流終結(jié)操作方法
概念 終結(jié)操作的意思是,執(zhí)行完此方法之后,Stream流將不能再執(zhí)行其他操作。 常見方法
方法名說明 void forEach(Consumer action) 對(duì)此流的每個(gè)元素執(zhí)行操作 long count() 返回此流中的元素?cái)?shù)
import java
. util
. ArrayList
; public class StreamDemo4 { public static void main ( String
[ ] args
) { ArrayList
< String> list
= new ArrayList < String> ( ) ; list
. add ( "林青霞" ) ; list
. add ( "張曼玉" ) ; list
. add ( "王祖賢" ) ; list
. add ( "柳巖" ) ; list
. add ( "張敏" ) ; list
. add ( "張無忌" ) ; list
. stream ( ) . forEach ( System
. out
: : println
) ; long count
= list
. stream ( ) . filter ( s
- > s
. startsWith ( "張" ) ) . count ( ) ; System
. out
. println ( count
) ; }
}
2.5Stream流綜合練習(xí)
import java
. util
. ArrayList
;
import java
. util
. stream
. Stream
; public class Streamtest { public static void main ( String
[ ] args
) { ArrayList
< String> manList
= new ArrayList < String> ( ) ; manList
. add ( "周潤發(fā)" ) ; manList
. add ( "成龍" ) ; manList
. add ( "劉德華" ) ; manList
. add ( "吳京" ) ; manList
. add ( "周星馳" ) ; manList
. add ( "李連杰" ) ; ArrayList
< String> womanList
= new ArrayList < String> ( ) ; womanList
. add ( "林心如" ) ; womanList
. add ( "張曼玉" ) ; womanList
. add ( "林青霞" ) ; womanList
. add ( "柳巖" ) ; womanList
. add ( "林志玲" ) ; womanList
. add ( "王祖賢" ) ; Stream
. concat ( manList
. stream ( ) . filter ( s
- > s
. length ( ) == 3 ) . limit ( 3 ) , womanList
. stream ( ) . filter ( s
- > s
. startsWith ( "林" ) ) . skip ( 1 ) ) . map ( Actor
: : new ) . forEach ( p
- > System
. out
. println ( p
. getName ( ) ) ) ; }
}
2.6Stream流的收集操作
概念 對(duì)數(shù)據(jù)使用Stream流的方式操作完畢后,可以把流中的數(shù)據(jù)收集到集合中。 常用方法
方法名說明 R collect(Collector collector) 把結(jié)果收集到集合中
方法名說明 public static Collector toList() 把元素收集到List集合中 public static Collector toSet() 把元素收集到Set集合中 public static Collector toMap(Function keyMapper,Function valueMapper) 把元素收集到Map集合中
import java
. util
. *
;
import java
. util
. stream
. Collectors
;
import java
. util
. stream
. Stream
; public class Collectdemo { public static void main ( String
[ ] args
) { List
< String> list
= new ArrayList < String> ( ) ; list
. add ( "林青霞" ) ; list
. add ( "張曼玉" ) ; list
. add ( "王祖賢" ) ; list
. add ( "柳巖" ) ; Set
< Integer> set
= new HashSet < Integer> ( ) ; set
. add ( 10 ) ; set
. add ( 20 ) ; set
. add ( 30 ) ; set
. add ( 33 ) ; set
. add ( 35 ) ; Stream
< Integer> setStream
= set
. stream ( ) . filter ( age
- > age
> 25 ) ; Set
< Integer> ages
= setStream
. collect ( Collectors
. toSet ( ) ) ; for ( Integer age
: ages
) { System
. out
. println ( age
) ; } String
[ ] strArray
= { "林青霞,30" , "張曼玉,35" , "王祖賢,33" , "柳巖,25" } ; Stream
< String> arrayStream
= Stream
. of ( strArray
) . filter ( s
- > Integer
. parseInt ( s
. split ( "," ) [ 1 ] ) > 28 ) ; Map
< String, Integer> map
= arrayStream
. collect ( Collectors
. toMap ( s
- > s
. split ( "," ) [ 0 ] , s
- > Integer
. parseInt ( s
. split ( "," ) [ 1 ] ) ) ) ; Set
< String> keySet
= map
. keySet ( ) ; for ( String key
: keySet
) { Integer value
= map
. get ( key
) ; System
. out
. println ( key
+ "," + value
) ; } }
}
與50位技術(shù)專家面對(duì)面 20年技術(shù)見證,附贈(zèng)技術(shù)全景圖
總結(jié)
以上是生活随笔 為你收集整理的Java基础day23 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。