Supplier簡(jiǎn)單來(lái)說(shuō)就是一個(gè)容器 ? 對(duì)于其方法get解釋更為簡(jiǎn)單 ,獲得結(jié)果。
① Supplier 接口可以理解為一個(gè)容器,用于裝數(shù)據(jù)的。
② Supplier 接口有一個(gè) get 方法,可以返回值。
public static void main(String[] args) {//1.使用Supplier接口實(shí)現(xiàn)方法,只有一個(gè)get方法,無(wú)參數(shù),返回一個(gè)值Supplier<Integer> supplier = new Supplier<Integer>() {@Overridepublic Integer get() {return new Random().nextInt();}};System.out.println(supplier.get());
?
? ? ? ? //2.使用lambda
? ? ? ? supplier = () -> new Random().nextInt();
? ? ? ? System.out.println(supplier.get());
?
? ? ? ? //3.使用方法引用
? ? ? ? Supplier<Double> supplier2 = Math::random;
? ? ? ? System.out.println(supplier2.get());
?
? ? ? ? //或許體會(huì)不到:: ? 然后看這個(gè)。 ? 它的意思也就是類(lèi)名:方法名
? ? ? ? User user = new User(1,"李孟冬","123456");
? ? ? ? Supplier<String> supplier1 = user::getName;
? ? ? ? System.out.println(supplier1.get());
?
? ? }
public static void main(String[] args) {Stream<Integer> stream = Stream.of(1,2,3,4,5,6,7,8,9,10);//返回一個(gè)optional對(duì)象//filter(Predicate<? super T> predicate)//返回由與此給定謂詞匹配的此流的元素組成的流。// findFirst()//返回描述此流的第一個(gè)元素的Optional如果流為空,則返回一個(gè)空的Optional 。Optional<Integer> optional = stream.filter(i -> i >5 ).findFirst();//可以在這里多次換數(shù)進(jìn)行比較// get()//如果 Optional中有一個(gè)值,返回值,否則拋出 NoSuchElementException 。System.out.println(optional.get());
?
?//orElse(T other)//返回值如果存在,否則返回 other 。System.out.println(optional.orElse(5));System.out.println(optional.orElse(7));//即如果optional為空則返回other如果不為空則返回optional中的值
?//orElseGet(Supplier<? extends T> other)//返回值(如果存在),否則調(diào)用 other并返回該調(diào)用的結(jié)果。System.out.println(optional.orElseGet(() -> new Random().nextInt()));
?
?//即orElse:如果first中存在數(shù),就返回這個(gè)數(shù),如果不存在,就放回傳入的數(shù)//orElseGet:如果first中存在數(shù),就返回這個(gè)數(shù),如果不存在,就返回supplier返回的值
}
斷言式接口
Predicate謂詞測(cè)試,謂詞其實(shí)就是一個(gè)判斷的作用類(lèi)似bool的作用
① Predicate 是一個(gè)謂詞型接口,其實(shí)只是起到一個(gè)判斷作用。
② Predicate 通過(guò)實(shí)現(xiàn)一個(gè) test 方法做判斷。
public static void main(String[] args) {//使用Predicate接口實(shí)現(xiàn)方法,只有一個(gè)test方法,傳入一個(gè)參數(shù),返回一個(gè)bool值//不使用lambdaPredicate<Integer> predicate = new Predicate<Integer>() {@Overridepublic boolean test(Integer integer) {if (integer > 5){return true;}return false;}};System.out.println(predicate.test(6));//true
?predicate = (t) -> t>5;System.out.println(predicate.test(1));//false}
public static void main(String[] args) {Predicate<Integer> predicate = (t) -> t>5 ;// of(T... values)//返回其元素是指定值的順序排序流。Stream<Integer> stream = Stream.of(1,323,65,4,8,6232,20);//filter(Predicate<? super T> predicate)//返回由與此給定謂詞匹配的此流的元素組成的流。// collect(Collector<? super T,A,R> collector)//使用 Collector對(duì)此流的元素執(zhí)行 mutable reduction Collector 。List<Integer> list = stream.filter(predicate).collect(Collectors.toList());list.forEach(System.out::println);//嗯,我查了1.8的api并沒(méi)有顯示forEach方法//根據(jù)詢問(wèn),嗯forEach方法來(lái)自于Collection//不過(guò)從方法中我們看出,forEach(方法引用)
?}
函數(shù)式接口
Function 接口是一個(gè)功能型接口,它的一個(gè)作用就是轉(zhuǎn)換作用,將輸入數(shù)據(jù)轉(zhuǎn)換成另一種形式的輸出數(shù)據(jù)。
① Function 接口是一個(gè)功能型接口,是一個(gè)轉(zhuǎn)換數(shù)據(jù)的作用。
② Function 接口實(shí)現(xiàn) apply 方法來(lái)做轉(zhuǎn)換。
public static void main(String[] args) {//① 使用map方法,泛型的第一個(gè)參數(shù)是轉(zhuǎn)換前的類(lèi)型,第二個(gè)是轉(zhuǎn)化后的類(lèi)型Function<String,Integer> function = new Function<String, Integer>() {@Overridepublic Integer apply(String s) {return s.length();}};Stream<String> stream = Stream.of("aaaa","dddd","fff","gggg","hhhh");//map(Function<? super T,? extends R> mapper)//返回由給定函數(shù)應(yīng)用于此流的元素的結(jié)果組成的流。Stream<Integer> stream1 = stream.map(function);//forEach(Consumer<? super T> action)//對(duì)此流的每個(gè)元素執(zhí)行操作。stream1.forEach(System.out::println);
?}