生活随笔
收集整理的這篇文章主要介紹了
Java基础day23
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java基礎day23 Java基礎day23-函數接口&stream流 1.函數式接口 1.1函數式接口概述 1.2函數式接口作為方法的參數 1.3函數式接口作為方法的返回值 1.4常用函數式接口之Supplier 1.5Supplier接口練習之獲取最大值 1.6常用函數式接口之Consumer 1.7Consumer接口練習之按要求打印信息 1.8常用函數式接口之Predicate 1.9Predicate接口練習之篩選滿足條件數據 1.10常用函數式接口之Function 1.11Function接口練習之按照指定要求操作數據 2.Strem流 2.2Stream流的常見生成方式 2.6Stream流的收集操作
Java基礎day23-函數接口&stream流
1.函數式接口
1.1函數式接口概述
概念 有且僅有一個抽象方法的接口 如何檢測一個接口是不是函數式接口 @FunctionalInterface 放在接口定義的上方:如果接口是函數式接口,編譯通過;如果不是,編譯失敗 注意事項 我們自己定義函數式接口的時候,@FunctionalInterface是可選的,就算我不寫這個注解,只要保證滿足函數 式接口定義的條件,也照樣是函數式接口。但是,建議加上該注解
1.2函數式接口作為方法的參數
需求描述 定義一個類(RunnableDemo),在類中提供兩個方法 一個方法是:startThread(Runnable r) 方法參數Runnable是一個函數式接口 一個方法是主方法,在主方法中調用startThread方法 代碼演示
public class RunnableDemo { public static void main ( String
[ ] args
) { startThread ( new Runnable ( ) { @Override public void run ( ) { System
. out
. println ( Thread
. currentThread ( ) . getName ( ) + "線程啟動" ) ; } } ) ; startThread ( ( ) - > System
. out
. println ( Thread
. currentThread ( ) . getName ( ) + "線程啟動" ) ) ; } private static void startThread ( Runnable r
) { new Thread ( r
) . start ( ) ; }
}
1.3函數式接口作為方法的返回值
需求描述 定義一個類(ComparatorDemo),在類中提供兩個方法 一個方法是:Comparator getComparator() 方法返回值Comparator是一個函數式接口 一個方法是主方法,在主方法中調用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常用函數式接口之Supplier
Supplier接口 Supplier接口也被稱為生產型接口,如果我們指定了接口的泛型是什么類型,那么接口中的get方法就會生產 什么類型的數據供我們使用。 常用方法 只有一個無參的方法
方法名說明 T get() 按照某種實現邏輯(由Lambda表達式實現)返回一個數據
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接口練習之獲取最大值
案例需求 定義一個類(SupplierTest),在類中提供兩個方法 一個方法是:int getMax(Supplier sup) 用于返回一個int數組中的最大值 一個方法是主方法,在主方法中調用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常用函數式接口之Consumer
Consumer接口 Consumer接口也被稱為消費型接口,它消費的數據的數據類型由泛型指定 常用方法 Consumer:包含兩個方法
方法名說明 void accept(T t) 對給定的參數執行此操作 default Consumer andThen(Consumer after) 返回一個組合的Consumer,依次執行此操作,然后執行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接口練習之按要求打印信息
案例需求 String[] strArray = {“林青霞,30”, “張曼玉,35”, “王祖賢,33”}; 字符串數組中有多條信息,請按照格式:“姓名:XX,年齡:XX"的格式將信息打印出來 要求: 把打印姓名的動作作為第一個Consumer接口的Lambda實例 把打印年齡的動作作為第二個Consumer接口的Lambda實例 將兩個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常用函數式接口之Predicate
Predicate接口 Predicate接口通常用于判斷參數是否滿足指定的條件 常用方法
方法名說明 boolean test(T t) 對給定的參數進行判斷(判斷邏輯由Lambda表達式實現),返回一個布爾值 default Predicate negate() 返回一個邏輯的否定,對應邏輯非 default Predicate and(Predicate other) 返回一個組合判斷,對應短路與 default Predicate or(Predicate other) 返回一個組合判斷,對應短路或
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接口練習之篩選滿足條件數據
練習描述 String[] strArray = {“林青霞,30”, “柳巖,34”, “張曼玉,35”, “貂蟬,31”, “王祖賢,33”}; 字符串數組中有多條信息,請通過Predicate接口的拼裝將符合要求的字符串篩選到集合ArrayList中,并 遍歷ArrayList集合 同時滿足如下要求:姓名長度大于2;年齡大于33 分析 有兩個判斷條件,所以需要使用兩個Predicate接口,對條件進行判斷 必須同時滿足兩個條件,所以可以使用and方法連接兩個判斷條件 示例代碼
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常用函數式接口之Function
Function接口 Function<T,R>接口通常用于對參數進行處理,轉換(處理邏輯由Lambda表達式實現),然后返回一個新的值 常用方法
方法名說明 R apply(T t) 將此函數應用于給定的參數 default Function andThen(Function after) 返回一個組合函數,首先將該函數應用于輸入,然后將after函數應用于結果
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接口練習之按照指定要求操作數據
練習描述 String s = “林青霞,30”; 請按照我指定的要求進行操作: 1:將字符串截取得到數字年齡部分 2:將上一步的年齡字符串轉換成為int類型的數據 3:將上一步的int數據加70,得到一個int結果,在控制臺輸出 請通過Function接口來實現函數拼接 示例代碼
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體驗Stream流
案例需求 按照下面的要求完成集合的創建和遍歷 ?創建一個集合,存儲多個字符串元素 ?把集合中所有以"張"開頭的元素存儲到一個新的集合 ?把"張"開頭的集合中的長度為3的元素存儲到一個新的集合 ?遍歷上一步得到的集合 原始方式示例代碼
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流的好處 直接閱讀代碼的字面意思即可完美展示無關邏輯方式的語義:獲取流、過濾姓張、過濾長度為3、逐一打印 Stream流把真正的函數式編程風格引入到Java中
2.2Stream流的常見生成方式
Stream流的思想 生成Stream流的方式 Collection體系集合 使用默認方法stream()生成流, default Stream stream() Map體系集合 把Map轉成Set集合,間接的生成流 數組 通過Stream接口的靜態方法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流中間操作方法
概念 中間操作的意思是,執行完此方法之后,Stream流依然可以繼續執行其他操作。 常見方法
方法名說明 Stream filter(Predicate predicate) 用于對流中的數據進行過濾 Stream limit(long maxSize) 返回此流中的元素組成的流,截取前指定參數個數的數據 Stream skip(long n) 跳過指定參數個數的數據,返回由該流的剩余元素組成的流 static Stream concat(Stream a, Stream b) 合并a和b兩個流為一個流 Stream distinct() 返回由該流的不同元素(根據Object.equals(Object) )組成的流 Stream sorted() 返回由此流的元素組成的流,根據自然順序排序 Stream sorted(Comparator comparator) 返回由該流的元素組成的流,根據提供的Comparator進行排序 Stream map(Function mapper) 返回由給定函數應用于此流的元素的結果組成的流 IntStream mapToInt(ToIntFunction mapper) 返回一個IntStream其中包含將給定函數應用于此流的元素的結果
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流終結操作方法
概念 終結操作的意思是,執行完此方法之后,Stream流將不能再執行其他操作。 常見方法
方法名說明 void forEach(Consumer action) 對此流的每個元素執行操作 long count() 返回此流中的元素數
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流綜合練習
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 ( "周潤發" ) ; 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流的收集操作
概念 對數據使用Stream流的方式操作完畢后,可以把流中的數據收集到集合中。 常用方法
方法名說明 R collect(Collector collector) 把結果收集到集合中
方法名說明 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位技術專家面對面 20年技術見證,附贈技術全景圖
總結
以上是生活随笔 為你收集整理的Java基础day23 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。