@Test
public void test2() {
Comparator<Integer> comparator =(a,b)->{
System.out.println("函数式接口");
return Integer.compare(a, b);
};
System.out.println(comparator.compare(-10,8));
}
输出为-1
简洁写法
@Test
public void test2() {
Comparator<Integer> comparator =(a,b)-> Integer.compare(a, b);
System.out.println(comparator.compare(-10,8));
}
函数式接口
什么事函数式接口: 只包含一个抽象方法的接口,就称为函数式接口。我们可以通过Lambda表达式来创建该接口的实现对象。
我们可以在任意函数式接口上使用@FunctionalInterface注解,这样做可以用于检测它是否是一个函数式接口,同时javadoc也会包含一条声明,说明这个接口是一个函数式接口。
消费型接口
public void makeMoney(Integer money, Consumer<Integer> consumer) {
consumer.accept(money);
}
@Test
public void test1() {
makeMoney(100,t-> System.out.println("今天赚了"+t));
}
供给型接口
public List addNumInList(int size, Supplier<Integer> supplier) {
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < size; i++) {
list.add(supplier.get());
}
return list;
}
@Test
public void test2() {
List list = addNumInList(10, () -> (int) (Math.random() * 100));
list.forEach(t-> System.out.print(t+"\t"));
}
随机生成10个数
函数型接口
public String handleStr(String s, Function<String, String> f) {
return f.apply(s);
}
@Test
public void test3() {
System.out.println(handleStr("abc",(String s)->s.toUpperCase()));
}
断定型接口
/**
* 断定型接口
* 自定义条件过滤字符串集合
*/
public List<String> filterStr(List<String> list, Predicate<String> predicate) {
ArrayList<String> result = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (predicate.test(list.get(i))) {
result.add(list.get(i));
}
}
return result;
}
@Test
public void test4() {
List<String> list = Arrays.asList("啊", "222", "333", "六六六六");
List<String> strings = filterStr(list, (s) -> s.length() > 3);
for (String s : strings) {
System.out.println(s);
}
}
方法引用
- 对象::实例方法名
- 类::静态方法名
- 类::实例方法名
1.对象::实例方法名
@Test
public void test1() {
PrintStream out = System.out;
Consumer<String> consumer = out::println;
consumer.accept("hello");
}
2.类::静态方法名
@Test
public void test2() {
Comparator<Integer> comparator = (x, y) -> Integer.compare(x, y);
//使用方法引用实现效果
Comparator<Integer> integerComparator = Integer::compare;
System.out.println(integerComparator.compare(4, 2));
System.out.println(comparator.compare(4, 2));
}
3.类::实例方法名
@Test
public void test3() {
BiPredicate<String, String> bp = (x, y) -> x.equals(y);
//使用方法引用实现相同效果
BiPredicate<String, String> bp2 = String::equals;
System.out.println(bp.test("1", "2"));
System.out.println(bp2.test("1", "2"));
}
构造器引用
格式:类名::new
public class Employee {
private Integer id;
private String name;
private Integer age;
@Override
public String toString() {
return "Employee{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public Employee(){
}
public Employee(Integer id) {
this.id = id;
}
public Employee(Integer id, Integer age) {
this.id = id;
this.age = age;
}
public Employee(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
}
@Test
public void test4() {
//引用无参构造器
Supplier<Employee> supplier = Employee::new;
System.out.println(supplier.get());
//引用有参构造器
Function<Integer, Employee> function = Employee::new;
System.out.println(function.apply(21));
BiFunction<Integer, Integer, Employee> biFunction = Employee::new;
System.out.println(biFunction.apply(8,24));
}
结果:
数组引用
数组引用的格式:type[]:new
使用示例:
public void test5() {
Function<Integer, String[]> function = String[]::new;
String[] apply = function.apply(10);
System.out.println(apply.length);
}