Lambda表示式內建函式式介面
阿新 • • 發佈:2018-12-30
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.junit.Test;
/**
* java8 內建四大函式式核心介面
*
* Consumer<T> 消費型介面
* void accept(T t);
*
* Supplier<T> 供給型介面
* T get();
*
* Function<T,R> 函式型介面
* R apple<T t>;
*
* Predicate<T> 判斷型介面
* boolean test<T t>;
* */
public class LambdaInterfaceDemo {
@Test
public void test1(){
consumer(100.00, x -> System.out.println(x));
}
//消費性介面
public void consumer(double money,Consumer<Double> con){
con.accept(money);
}
@Test
public void test2(){
String s = supplier(() -> "fasfsdf");
System.out.println(s);
}
//供給型介面
public String supplier(Supplier<String> sup){
String str=sup.get().toString().toUpperCase();
return str;
}
@Test
public void test3(){
String s =function("adfasdfsdfsd", (e1) ->e1.substring(0, 5));
System.out.println(s);
}
//函式型介面
public String function(String num,Function<String, String> fun){
return fun.apply(num).toString().toUpperCase();
}
@Test
public void test4(){
List<Employees> list = Arrays.asList(
new Employees(20, "張三", 1234.11),
new Employees(70, "李四", 9985.01),
new Employees(58, "王五", 3421.11),
new Employees(10, "趙六", 6785.11),
new Employees(40, "田七", 3362.11)
);
boolean b = predicate("safsdfsdfdsf", (x) -> x.startsWith("a"));
System.out.println(b);
}
//判斷型介面
public boolean predicate(String str ,Predicate<String> pre){
return pre.test(str);
}
}
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import org.junit.Test;
/**
* java8 內建四大函式式核心介面
*
* Consumer<T> 消費型介面
* void accept(T t);
*
* Supplier<T> 供給型介面
* T get();
*
* Function<T,R> 函式型介面
* R apple<T t>;
*
* Predicate<T> 判斷型介面
* boolean test<T t>;
* */
public class LambdaInterfaceDemo {
@Test
public void test1(){
consumer(100.00, x -> System.out.println(x));
}
//消費性介面
public void consumer(double money,Consumer<Double> con){
con.accept(money);
}
@Test
public void test2(){
String s = supplier(() -> "fasfsdf");
System.out.println(s);
}
//供給型介面
public String supplier(Supplier<String> sup){
String str=sup.get().toString().toUpperCase();
return str;
}
@Test
public void test3(){
String s =function("adfasdfsdfsd", (e1) ->e1.substring(0, 5));
System.out.println(s);
}
//函式型介面
public String function(String num,Function<String, String> fun){
return fun.apply(num).toString().toUpperCase();
}
@Test
public void test4(){
List<Employees> list = Arrays.asList(
new Employees(20, "張三", 1234.11),
new Employees(70, "李四", 9985.01),
new Employees(58, "王五", 3421.11),
new Employees(10, "趙六", 6785.11),
new Employees(40, "田七", 3362.11)
);
boolean b = predicate("safsdfsdfdsf", (x) -> x.startsWith("a"));
System.out.println(b);
}
//判斷型介面
public boolean predicate(String str ,Predicate<String> pre){
return pre.test(str);
}
}