函數(shù)式接口:只有一個方法的接口
比如:Runnable接口,forEach接口…….
@FunctionalInterface
public interface Runnable {
/**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run()
*/
public abstract void run();
}
四大原生函數(shù)式接口
Function:范型<T,R> T會作為參數(shù)羔巢,R則是返回值
@FunctionalInterface
public interface Function<T, R> {
/**
* Applies this function to the given argument.
*
* @param t the function argument
* @return the function result
*/
R apply(T t);
舉個例子
public static void main(String[] args)
{
//函數(shù)式接口
Function function = new Function<String,Integer>()
{
@Override
public Integer apply(String o)
{
return 1;
}
};
System.out.println(function.apply("Y1"));
}
運行結(jié)果:
1
還可以用 Lambda 這么寫
Function functionLambda = (Y1)->{return 1;};
System.out.println(functionLambda.apply("Y1"));
Function functionLambda = Y1->{return 1;};
System.out.println(functionLambda.apply("Y1"));
Predicate(斷定型接口):范型<T> T會作為參數(shù)有咨,根據(jù)邏輯返回布爾值
@FunctionalInterface
public interface Predicate<T> {
/**
* Evaluates this predicate on the given argument.
*
* @param t the input argument
* @return {@code true} if the input argument matches the predicate,
* otherwise {@code false}
*/
boolean test(T t);
舉個例子
public static void main(String[] args)
{
Predicate predicate = new Predicate<String>()
{
//判斷字符串長度是否大于3
@Override
public boolean test(String o)
{
return o.length()>3;
}
};
System.out.println(predicate.test("Y1"));
}
運行結(jié)果:
false
當(dāng)然也可以用Lambda
Predicate predicate = Y1->{
if (Y1 instanceof String){
String o = (String) Y1;
return o.length()>3;
}else {
return false;
}
};
System.out.println(predicate.test("Y111"));
運行結(jié)果:
true
或者更加簡化
Predicate<String> predicate = Y1->{return Y1.length()>3;};
System.out.println(predicate.test("Y111"));
Consumer(消費型接口) 范型<T> T會作為參數(shù)乖订,沒有返回值
@FunctionalInterface
public interface Consumer<T> {
/**
* Performs this operation on the given argument.
*
* @param t the input argument
*/
void accept(T t);
舉個例子
public static void main(String[] args)
{
Consumer<String> consumer = new Consumer<String>()
{
@Override
public void accept(String o)
{
System.out.println(o);
}
};
consumer.accept("Y1");
}
運行結(jié)果:
Y1
Lambda
Consumer<String> consumer = Y1->{ System.out.println(Y1); };
consumer.accept("Y1”);
運行結(jié)果:
Y1
Supplier(供給型接口) 沒有參數(shù)只有返回值
@FunctionalInterface
public interface Supplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get();
}
舉個例子
public static void main(String[] args)
{
Supplier<String> supplier = new Supplier<String>()
{
@Override
public String get()
{
return "Y1";
}
};
System.out.println(supplier.get());
}
運行結(jié)果:
Y1
Lambda
Supplier<String> supplier = ()->{return "Y1";};
System.out.println(supplier.get());
運行結(jié)果:
Y1
函數(shù)式接口簡化類編程模型酥馍,框架底層應(yīng)用比較多,下面的流式計算會用到很多函數(shù)式接口
Stream流式計算
看一個例子:
public class StreamDemo
{
public static void main(String[] args)
{
/*
要求用一行代碼實現(xiàn)
現(xiàn)在有6個用戶
篩選:
ID 必須是偶數(shù)
年齡必須大于3歲
用戶名轉(zhuǎn)為大寫字母
用戶名字母倒著排序
只輸出一個用戶的名字
*/
User user1 = new User(1,"a",1);
User user2 = new User(2,"b",2);
User user3 = new User(3,"c",3);
User user4 = new User(4,"d",4);
User user5 = new User(5,"e",5);
User user6 = new User(6,"f",6);
}
}
這時候就要用到鏈式編程坦喘,lambda表達式,函數(shù)式接口饺饭,還有Stream流式計算
//集合負責(zé)存儲
List<User> users = Arrays.asList(user1, user2, user3, user4, user5,user6);
//計算交給stream流
users.stream()
.filter(user -> {return user.getId()%2==0;}) // id偶數(shù)
.filter(user -> {return user.getAge()>3;}) //年齡大于三
.map(user -> {return user.getName().toUpperCase();}) //名字變成大大寫
.sorted((u1,u2) -> {return u2.compareTo(u1);}) //倒排序
.limit(1) //輸出一個用戶
.forEach(System.out::print);
運行結(jié)果
F