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);
	}
}

 

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