介面名稱 方法名
Function<T, R> R apply(T t)
Consumer<T> void accept(T t)
Supplier<T> T get()
Predicate<T> boolean test(T t)
Function的T為參數,R為回傳值,看方法名就可知道
Function:(功能),像客製化的產品,什麼都可以,所以可接收參數也可回傳結果
Consumer:(消費者),消費者只負責接收產品,所以只接收參數,不回傳結果
Supplier:(提供者),提供者只負責生產,所以不接收參數,只回傳結果
Predicate:(正則)表達式,和 Function 一樣,但回傳布林
其他:
UnaryOperaor<T>:一個參數,回傳的類型和傳參類型一樣
BinaryOperator<T, T>:和 UnaryOperaor 一樣,只不過有兩個參數
※只要有 Operator 就表示傳參類型和回傳值的類型是一樣的
BiXxx:Bi 開頭的就是兩個參數的意思
IntXxx:參數為 int,類似的還有 LongXxx、DoubleXxx
ToIntXxx:回傳 int 的意思,類似的還有 ToLongXxx、ToDoubleXxx
IntToXxx:上兩個的合體,參數為 int,回傳 Xxx,類似的還有 LongToXxx、DoubleToXxx
※package
// Function
Function<String, Integer> funa = (i) -> {
return Integer.parseInt(i) + 1;
};
System.out.println("Function:");
System.out.println(funa.apply("100"));
Function<String, Integer> funb = Integer::parseInt;
System.out.println(funb.apply("100") + 1);
// Consumer
Consumer<String> cona = (s) -> {
System.out.println(s);
};
System.out.println(System.getProperty("line.separator") + "Consumer:");
cona.accept("Hello World!");
Consumer<String> conb = System.out::println;
conb.accept("Hello World!");
// Supplier
Supplier<Properties> supa = () -> {
return System.getProperties();
};
System.out.println(System.getProperty("line.separator") + "Supplier:");
System.out.println(supa.get());
Supplier<Properties> supb = System::getProperties;
System.out.println(supb.get());
// Predicate
String str = "12345";
Predicate<String> prea = (regExp) -> {
return str.matches(regExp);
};
System.out.println(System.getProperty("line.separator") + "Predicate:");
System.out.println(prea.test("\\d{5}"));
Predicate<String> preb = str::matches;
System.out.println(preb.test("\\d{5}"));
System.out.println();
// 有更多參數時,就要到API去找,如Bi有二的意思,表示傳兩個參數
BiFunction<String, String, String> bfuna = (s1, s2) -> {
return "hello".replaceAll(s1, s2);
};
System.out.println(bfuna.apply("l", "_"));
BiFunction<String, String, String> bfunb = "hello"::replaceAll;
System.out.println(bfunb.apply("l", "_"));
※
※集合
※迭代
java8的Iterable<T>多一個default的forEach(Consumer<? super T> action)方法public class Java8Test {
static List<Integer> l = new ArrayList<>();
static {
l.add(1);
l.add(5);
l.add(3);
l.add(3);
l.add(3);
}
public static void main(String[] args) {
System.out.println("迭代");
for (Integer i : l)
System.out.println(i);
System.out.println();
l.forEach(i -> System.out.println(i));
System.out.println();
System.out.println("長度");
System.out.println(l.size());
Stream<Integer> stream = l.stream();
System.out.println(stream.count());
System.out.println();
System.out.println("List");
System.out.println(l);
Stream<List<Integer>> s = Stream.of(l);
s.forEach(System.out::println);
}
}
※
※包含
static List<String> animal = new ArrayList<>();
static {
animal.add("elephant");
animal.add("dog");
animal.add("pig");
animal.add("rat");
animal.add("tiger");
animal.add("cat");
}
public static void main(String[] args) {
Predicate<String> pred = (String str) -> {
return str.contains("a");
};
animal.forEach(xxx -> {
if (pred.test(xxx)) {
System.out.println("have a is:" + xxx);
}
});
}
※將集合抓出來然後判斷正則,還可以寫一個公用的方法,如下:
※
public static void main(String[] args) {
find(animal, (String str) -> {
return str.contains("a");
});
}
public static void find(List<String> data, Predicate<String> pred) {
data.forEach(xxx -> {
if (pred.test(xxx)) {
System.out.println("have a is:" + xxx);
}
});
// data.stream().filter(pred).forEach(System.out::println);
// data.stream().filter(pred).collect(Collectors.toList()).forEach(System.out::println);
}
※注解是利用Stream,可以有更精簡的寫法,下一章會說明
沒有留言:
張貼留言