JG-OJ記錄-第一次期末考-9:(20-21)晉級賽
阿新 • • 發佈:2021-01-15
函式式介面
1、概念:
使用功能性介面作為函式的引數,Lambda表示式協助實現函式功能。
2、功能性介面分類:
消費式介面,提供式介面,函式式介面,斷言式介面
3、消費式介面
import java.util.function.Consumer;
public class Demo4_消費式介面 {
public static void main(String[] args) {
method(x -> System.out.println("每月消費" + x + "元在生活上" ), 5000);
}
public static void method(Consumer<Integer> cons, Integer money) {
//消費式介面,提供了accept抽象方法,接受一個引數
cons.accept(money);
}
}
4、提供式介面
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.function.Supplier;
public class Demo5_ 提供式介面 {
public static void main(String[] args) {
//需求:生成n個隨機值(1~10)到集合中
List<Integer> method = method(() -> new Random().nextInt(10) + 1, 5);
System.out.println(method);//[3, 1, 9, 1, 2]
}
public static List<Integer> method(Supplier<Integer> sup, int num) {
List<Integer> list = new ArrayList<>();
for (int i = 1; i <= num; i++) {
Integer integer = sup.get();
list.add(integer);
}
return list;
}
}
5、函式式介面
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
public class Demo6_函式式介面 {
public static void main(String[] args) {
//需求:對字串去除左右空格
List<String> list = new ArrayList();
list.add("abc");
list.add(" bbc");
list.add("ccd ");
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
String method = method(x -> x.trim(), s);
list.set(i, method);
}
System.out.println(list);
}
public static String method(Function<String, String> fun, String s) {
String apply = fun.apply(s);
return apply;
}
}
6、斷言式介面
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
public class Demo7_斷言式介面 {
public static void main(String[] args) {
//使用集合儲存字串長度大於3的元素
List<String> list = new ArrayList<>();
Collections.addAll(list,"cba","wahaha","ujiuye","offcn");
List<String> method = method(x -> x.length() > 3, list);
System.out.println(method);//[wahaha, ujiuye, offcn]
}
public static List<String> method(Predicate<String> pred, List<String> list) {
List<String> arr = new ArrayList<>();
for (String s : list) {
if (pred.test(s)) {
arr.add(s);
}
}
return arr;
}
}
Stream類API
1、API介紹
2、獲取該介面實現類的方式
(1)通過Collection集合獲取
(2)通過Map集合獲取
(3)通過陣列/Stream類獲取
import com.sun.javafx.collections.MappingChange;
import java.util.*;
import java.util.stream.Stream;
public class Demo8_Stream {
public static void main(String[] args) {
//Collection獲取Stream物件
Collection<Integer> col = new ArrayList();
col.add(10);
col.add(20);
col.add(30);
//.stream()方法
Stream<Integer> stream = col.stream();
System.out.println(stream);//[email protected]
//Map
Map<String, String> map = new HashMap<>();
map.put("cn", "china");
map.put("us", "america");
//1、通過keySet獲取鍵一系列元素,再通過stream()獲取Stream物件
Set<String> keys = map.keySet();
Stream<String> s = keys.stream();
System.out.println(s);
//2、通過values獲取值一系列元素,再通過stream()獲取Stream物件
Collection<String> values = map.values();
Stream<String> ss = values.stream();
System.out.println(ss);
//3、通過valentrySet獲取Entry一系列元素,再通過stream()獲取Stream物件
Set<Map.Entry<String, String>> entries = map.entrySet();
Stream<Map.Entry<String, String>> sss = entries.stream();
System.out.println(sss);
//陣列,多個元素獲取
Integer[] arr = {1, 2, 3, 4, 5};
//Arrays.stream(陣列) 獲取Stream類物件
Stream<Integer> stream1 = Arrays.stream(arr);
System.out.println(stream1);
//Stream.of(T... t) 獲取Stream類物件
Stream<Integer> stream2 = Stream.of(arr);
System.out.println(stream2);
Stream<String> stream3 = Stream.of("abc", "bbb", "ccb");
System.out.println(stream3);
}
}
(4)常用終結功能方法
import com.sun.javafx.collections.MappingChange;
import java.util.*;
import java.util.stream.Stream;
public class Demo8_Stream {
public static void main(String[] args) {
//Stream終結方法:呼叫終結方法後,流被關閉,無法繼續使用。
Collection<Integer> col = new ArrayList();
col.add(10);
col.add(20);
col.add(30);
Stream<Integer> stream = col.stream();
//.forEach(x-> System.out.println(x)) 遍歷Stream中的每個元素,將元素輸出到控制檯。
//stream.forEach(x-> System.out.println(x));
//.count() 獲取Stream中的元素個數
System.out.println(stream.count());//java.lang.IllegalStateException: stream has already been operated upon or closed
}
}
(5)常用延遲功能方法
import com.sun.javafx.collections.MappingChange;
import java.util.*;
import java.util.stream.Stream;
public class Demo8_Stream {
public static void main(String[] args) {
//Stream中的延遲方法:Stream呼叫該方法後,流不會被關閉,還可以繼續使用。
Collection<Integer> col = new ArrayList();
col.add(10);
col.add(20);
col.add(30);
Stream<Integer> stream = col.stream();
//limit(long) 獲取Stream前n位元素
//stream.limit(2).forEach(x -> System.out.println(x));
//skip(long) 跳過Stream前n位元素
//Stream<Integer> skip = stream.skip(2);
//skip.forEach(x-> System.out.println(x));
//filter(Predicate) 根據條件過濾Stream的元素
stream.filter(x -> x >= 20).forEach(x -> System.out.println(x));
Collection<String> col2 = new ArrayList();
col2.add("abc");
col2.add(" bbc");
col2.add("ccd ");
Stream<String> stream2 = col2.stream();
//map() 對Stream類中元素執行相同方法
stream2.map(x->x.trim()).forEach(x-> System.out.println(x));
}
}