Skip to main content

JDK8 lambda表达式

JDK8 中包含了很多内置的函数式接口。有些是在以前版本的Java中大家耳熟能详的,例如Comparator接口,或者Runnable接口。对这些现成的接口进行实现,可以通过@FunctionalInterface 标注来启用Lambda功能支持

Predicate
Predicate 是一个布尔类型的函数,该函数只有一个输入参数。Predicate接口包含了多种默认方法,用于处理复杂的逻辑动词(and,or, negate).

1

Example
[java]
public class PeopleDao {

public List findByAge(List list, int age) {
return list.stream().filter(e -> e.getAge() == age).collect(Collectors.toList());
}

public static void main(String[] args) {
List list = new ArrayList();

for (int i = 0; i < 100; i++) {
People people = new People();
people.setAge(i);
people.setStress("Shanxi");
people.setName("P " + i);
list.add(people);
}

PeopleDao dao = new PeopleDao();
List res = dao.findByAge(list, 10);
res.stream().forEach(e -> System.out.println(e.toString()));

Predicate agePredicate = (p) -> p.getAge() == 10;
list.stream().filter(agePredicate).forEach(p -> System.out.println(p.toString()));
}

}

[/java]
JDK自带了几个基本类型的Predicate,就像DoublePredicate,LongPredicate,IntPredicate。

Function
Function接口接收一个参数,并返回单一的结果。默认方法可以将多个函数串在一起(compse, andThen)
2

[java]
package com.learn.core.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import com.learn.core.domain.People;

/**
* Created by caveup on 2017/2/20.
*/
public class PeopleDao {

public List<People> findByAge(List<People> list, int age) {
return list.stream().filter(e -> e.getAge() == age).collect(Collectors.toList());
}

public static void main(String[] args) {
List<People> list = new ArrayList<People>();

for (int i = 0; i < 100; i++) {
People people = new People();
people.setAge(i);
people.setStress("Shanxi");
people.setName("P" + i);
list.add(people);
}

PeopleDao dao = new PeopleDao();
List<People> res = dao.findByAge(list, 10);
res.stream().forEach(e -> System.out.println(e.toString()));

Predicate<People> agePredicate = (p) -> p.getAge() == 10;
list.stream().filter(agePredicate).forEach(p -> System.out.println(p.toString()));

Function<People, String> name = (p) -> p.getName();
Function<String, String> upFunction = String::toUpperCase;

System.out.println(name.apply(list.get(0)));
System.out.println(upFunction.apply("hello"));

Function<String, Integer> toInteger = Integer::valueOf;
Function<String, String> backToString = toInteger.andThen(String::valueOf);
System.out.println(backToString.apply("10"));
}

}
[/java]

Consumer
Consumer代表了在一个输入参数上需要进行的操作,就是对输入的参数做一些行为或者不做任何行为。
3

[java]
public class ConsumerLambda {

public static void main(String[] args) {

People people = new People();
people.setAge(10);
people.setStress("Shanxi");
people.setName("Padfasdfa");

Consumer<People> peopleConsumer = p -> System.out.println(p.toString());
peopleConsumer.accept(people);
}
}

[/java]

Supplier
Supplier接口产生一个给定类型的结果。与Function不同的是,Supplier没有输入参数
4

[java]
public class Supplierlambda {

public static void main(String[] args) {
People people = new People();
people.setAge(10);
people.setStress("Shanxi");
people.setName("Padfasdfa");

Supplier<People> peopleSupplier = () -> new People();

System.out.println(peopleSupplier.get().toString());
}
}

[/java]