java.util.function这个包下定义了许多函数式接口(这类接口只定义了唯一的抽象方法,并且这类接口使用了@FunctionalInterface
进行注解),大致分为了以下四类:
- Function: 接收参数,并返回结果,主要方法 R apply(T t)
- Consumer: 接收参数,无返回结果, 主要方法为 void accept(T t)
- Supplier: 不接收参数,但返回结构,主要方法为 T get()
- Predicate: 接收参数,返回boolean值,主要方法为 boolean test(T t)
再细分的话又可以分成两类
- 每个类型对int、long、double的处理,如IntFunction、IntUnrayOperator、IntBinaryOperator、IntConsumer、IntSupplier、IntPredicate。
- 每个类型又有相应的增强变体,如UnaryOperator、BiFunction、BinaryOperator、BiConsumer、BiPredicate。
包含的类如下:
案例
Function
Function<String, Integer> str2Integer = s -> Integer.valueOf(s);
Integer apply = str2Integer.apply("32");
DoubleFunction<String> df = d -> String.valueOf(d * 5.1);
String apply1 = df.apply(2.3);
DoubleToIntFunction doubleToIntFunction = d -> new Double(d).intValue();
int i = doubleToIntFunction.applyAsInt(22.1);
DoubleToLongFunction doubleToLongFunction = d -> new Double(d).longValue();
long l = doubleToLongFunction.applyAsLong(22.2);
IntFunction<String> intFunction = n -> String.valueOf(n);
String apply2 = intFunction.apply(123);
IntToDoubleFunction intToDoubleFunction = n -> new Integer(n).doubleValue();
double v = intToDoubleFunction.applyAsDouble(12);
IntToLongFunction intToLongFunction = n -> new Integer(n).longValue();
long l1 = intToLongFunction.applyAsLong(22);
LongFunction<String> longFunction = ln -> String.valueOf(ln);
String apply3 = longFunction.apply(10l);
LongToDoubleFunction longToDoubleFunction = ln -> new Long(ln).doubleValue();
double v1 = longToDoubleFunction.applyAsDouble(10l);
LongToIntFunction longToIntFunction = ln -> new Long(ln).intValue();
int i1 = longToIntFunction.applyAsInt(10l);
ToDoubleFunction<String> toDoubleFunction = s -> Double.valueOf(s);
double v3 = toDoubleFunction.applyAsDouble("1");
ToIntFunction<Double> toIntFunction = d -> d.intValue();
int i3 = toIntFunction.applyAsInt(12.3);
ToLongFunction<Double> toLongFunction = d -> d.longValue();
long l2 = toLongFunction.applyAsLong(12.3);
UnaryOperator
// 发现UnaryOperator其实就是Function,不过这个Function有点特殊,它的输入入参和输出都是同一类型的
Function<Integer, Integer> function = new UnaryOperator<Integer>() {
@Override
public Integer apply(Integer integer) {
return null;
}
};
// UnaryOperator.apply()
UnaryOperator<Integer> unaryOperator = i -> i * 2;
Integer apply = unaryOperator.apply(20);
// 几种特殊的UnaryOperator
DoubleUnaryOperator doubleUnaryOperator = d -> d * 2;
double v = doubleUnaryOperator.applyAsDouble(2.3);
IntUnaryOperator intUnaryOperator = i -> i * i;
int i = intUnaryOperator.applyAsInt(20);
LongUnaryOperator longUnaryOperator = l -> l * l;
long l = longUnaryOperator.applyAsLong(20l);
BiFunction
// BiFunction接受两个输入类型
BiFunction<Integer, Integer, Double> biFunction = (i1, i2) -> new Double(i1 + i2);
Double apply = biFunction.apply(1, 2);
// 几种特殊的BiFunction
ToDoubleBiFunction<Integer, Integer> toDoubleBiFunction = (f1, f2) -> f1 + f2;
double v2 = toDoubleBiFunction.applyAsDouble(2, 3);
ToIntBiFunction<String, String> toIntBiFunction = (s1, s2) -> Integer.valueOf(s1) + Integer.valueOf(s2);
int i2 = toIntBiFunction.applyAsInt("2", "3");
ToLongBiFunction<String,String> toLongBiFunction = (s1,s2) -> Long.valueOf(s1) + Long.valueOf(s2);
long l = toLongBiFunction.applyAsLong("2", "3");
BinaryOperator
// 发现BinaryOperator其实就是BiFunction,不过这个BiFunction有点特殊,它的输入类型和输出类型是同一类型
BiFunction<Integer,Integer,Integer> biFunction = new BinaryOperator<Integer>() {
@Override
public Integer apply(Integer integer, Integer integer2) {
return null;
}
};
// BinaryOperator的静态方法
BinaryOperator<Integer> maxBy = BinaryOperator.maxBy(Integer::compare);
Integer apply = maxBy.apply(1, 2);
BinaryOperator<String> minBy = BinaryOperator.minBy(String::compareTo);
String apply1 = minBy.apply("a", "A");
// 几种特殊的BinaryOperator
IntBinaryOperator intBinaryOperator = Integer::sum;
int i = intBinaryOperator.applyAsInt(2, 3);
DoubleBinaryOperator doubleBinaryOperator = Double::sum;
double v = doubleBinaryOperator.applyAsDouble(1.0, 2.0);
LongBinaryOperator longBinaryOperator = Long::sum;
long l = longBinaryOperator.applyAsLong(1l, 2l);
Consumer
public class ConsumerTest {
public static void main(String[] args) {
}
private static void specialConsumer() {
IntConsumer intConsumer = System.out::println;
intConsumer.accept(20);
LongConsumer longConsumer = System.out::println;
longConsumer.accept(10l);
DoubleConsumer doubleConsumer = d -> System.out.println(d);
doubleConsumer.accept(10.2);
}
private static void consumer_andThen() {
Consumer<List<Integer>> first = ConsumerTest::getFirst;
Consumer<List<Integer>> second = ConsumerTest::getSecond;
List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4));
first.andThen(second).accept(list);
}
private static void getFirst(List<Integer> list){
for (int i = 0; i < list.size(); i++) {
list.set(i, list.get(i) * list.get(i));
}
}
private static void getSecond(List<Integer> list){
list.forEach(System.out::println);
}
private static void consumer_getConsumer() {
Consumer<String> nameConsumer = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println(s);
}
};
nameConsumer.accept("Mahesh");
nameConsumer.accept("Krishna");
// 使用lambda表达式
List<Integer> oddList = new ArrayList<>();
List<Integer> evenList = new ArrayList<>();
Consumer<Integer> storeNumber = n -> {
if (n % 2 == 0) {
evenList.add(n);
}else {
oddList.add(n);
}
};
Consumer<List<Integer>> printList = (List<Integer> list) -> {
list.forEach(System.out::println);
};
storeNumber.accept(10);
storeNumber.accept(15);
storeNumber.accept(25);
storeNumber.accept(30);
System.out.println("---old number--");
printList.accept(oddList);
System.out.println("--even number--");
printList.accept(evenList);
// 使用方法引用
printList = System.out::println;
System.out.println("--使用方法引用--");
printList.accept(evenList);
}
}
Predicate
public class PredicateTest {
public static void main(String[] args) {
// test()
Predicate<String> startWith = (s) -> s.startsWith("W");
boolean aCase = startWith.test("Case");
Predicate<String> endWith = (s) -> s.endsWith("D");
boolean pdd = endWith.test("PDD");
// and()
boolean word = startWith.and(endWith).test("WORD");
// negate()
boolean aCase1 = startWith.negate().test("Case");
// or()
boolean david = startWith.or(endWith).test("DAVID");
// 几种特殊的Predicate
IntPredicate intPredicate = i -> i % 2 == 0;
boolean test = intPredicate.test(3);
LongPredicate longPredicate = l -> l % 2 == 0;
boolean test1 = longPredicate.test(3l);
DoublePredicate doublePredicate = d -> d % 2 == 0;
doublePredicate.test(3.0);
}
}
Supplier
public class SupplierTest {
public static void main(String[] args) {
}
private static void specialSupplier() {
IntSupplier intSupplier = new Random()::nextInt;
int asInt = intSupplier.getAsInt();
LongSupplier longSupplier = new Random()::nextLong;
long asLong = longSupplier.getAsLong();
DoubleSupplier doubleSupplier = new Random()::nextDouble;
}
private static void doubleStream() {
DoubleStream doubleStream = DoubleStream.generate(() -> new Random().nextDouble()).limit(5);
doubleStream.forEach(d -> System.out.println(d));
}
private static void supplierAsMethodParam() {
double result = getResult(() -> Double.max(13.56, 13.67));
System.out.println(result);
}
private static double getResult(DoubleSupplier supplier){
return supplier.getAsDouble() * 100;
}
private static void getSupplier() {
DoubleSupplier overrideMethod = new DoubleSupplier() {
@Override
public double getAsDouble() {
return Double.NEGATIVE_INFINITY;
}
};
DoubleSupplier lambdaMethod = () -> Double.NEGATIVE_INFINITY;
DoubleSupplier methodReference = SupplierTest::getDouble;
}
private static double getDouble(){
return Double.parseDouble("1.023");
}
}