Java中函數式接口

1.Supplier接口

Supplier接口是一個供給類型的接口,僅包含一個無參的方法: T get() 。用來獲取一個泛型參數指定類型的對
象數據。

import java.util.function.Supplier;

public class SupplierDemo {
	private String getStr(Supplier<String> fun) {//參數是函數式接口
		return fun.get();
	}
	public static void main(String[] args) {
		String a = "hello";
		String b = "世界";
		String str = new SupplierDemo().getStr(()->a+b);//入參時可以傳入Lambda表達式
		System.out.println(str);
	}
}

例子:返回數組中的最大值

import java.util.function.Supplier;

public class SupplierDemo {
	private Integer getMax(Supplier<Integer> fun) {//參數是函數式接口
		return fun.get();
	}
	public static void main(String[] args) {
		int[] arr = {3,1,55,6,9};
		Integer max = new SupplierDemo().getMax(()->{
			int m = 0;
			for (int i : arr) {
				if(m < i)
					m = i;
			}
			return m;
		});//入參時可以傳入Lambda表達式
		System.out.println(max);
		//匿名內部類
		Integer max1 = new SupplierDemo().getMax(new Supplier<Integer>() {
			@Override
			public Integer get() {
				int m = 0;
				for (int i : arr) {
					if(m < i)
						m = i;
				}
				return m;
			}
		});
		System.out.println(max1);
	}
}

2.Consumer接口

Consumer接口爲消費類型接口,Consumer 接口中包含抽象方法void accept(T t) ,意爲消費一個指定泛型的數據。

import java.util.function.Consumer;

public class ConsumerDemo {
	private void consumerStr(String str, Consumer<String> con) {
		con.accept(str);
	}
	public static void main(String[] args) {
		new ConsumerDemo().consumerStr("hello",s->System.out.println(s));
	}
}

例子:list forEach傳入的是Consumer函數式接口。使用forEach遍歷List

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ConsumerDemo {
	public static void main(String[] args) {
		//list的forEach方法傳入的是Consumer函數式接口:forEach(Consumer<? super T> action)
		//使用forEach遍歷list
		List<String> list = new ArrayList<String>();
		list.add("Tom");
		list.add("Jerry");
		list.add("Lily");
		list.forEach(l->System.out.println(l));
	}
}

Consumer 接口中還包含默認方法andThen,該方法的作用是先做一個操作然後在進行另一個操作

import java.util.function.Consumer;

public class ConsumerDemo {
	private void consumerStr(String str,Consumer<String> c1,Consumer<String> c2) {
		c1.andThen(c2).accept(str);
	}
	public static void main(String[] args) {
		//傳入一個字符串先轉大寫後轉小寫
		new ConsumerDemo().consumerStr("Hello", 
				s->System.out.println(s.toUpperCase()), 
				s->System.out.println(s.toLowerCase()));
	}
}

3.Predicate接口

Predicate接口是判斷類型接口返回boolean值,包含一個抽象方法test三個默認方法and,or,negate和一個靜態方法isEqual

test 方法

import java.util.function.Predicate;

public class PredicateDemo {
	private boolean predicateStr(String s, Predicate<String> p) {
		return p.test(s);
	}

	public static void main(String[] args) {
		// 判斷字符串中是否包含字母a
		boolean b = new PredicateDemo().predicateStr("hello", s -> s.contains("a"));
		System.out.println(b);
	}
}

and or negate 分別爲與 或 非,如and

import java.util.function.Predicate;

public class PredicateDemo {
	private boolean predicateStr(String s, Predicate<String> p1,Predicate<String> p2) {
		return p1.and(p2).test(s);
	}

	public static void main(String[] args) {
		// 判斷字符串中是否包含字母a和b
		boolean b = new PredicateDemo().predicateStr("hello", s->s.contains("a"), s->s.contains("b"));
		System.out.println(b);
	}
}

4.Function接口

該接口是一個轉換類型的接口,根據一個類型的數據得到另一個類型的數據java.util.function.Function<T,R> Function 接口中最主要的抽象方法爲: R apply(T t) ,根據類型T的參數獲取類型R的結果。和一個默認方法andThen和Consumer中andThen類似

例:將字符串轉換爲int類型的數字

import java.util.function.Function;

public class FunctionDemo {
	private Integer strToInt(String str,Function<String, Integer> fun) {
		 return fun.apply(str);
	}
	public static void main(String[] args) {
		Integer i = new FunctionDemo().strToInt("100", s->Integer.parseInt(s));
		System.out.println(i);
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章