package com.test;
import junit.framework.TestCase;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import static org.junit.Assert.*;
// refer to https://www.cnblogs.com/Joyce-day-day-up/p/14001648.html
public class mainTest extends TestCase {
public void testFunction() {
//匿名内部类的写法:
// Function func = new Function() {
// @Override
// public Integer apply(String str) {
// // TODO Auto-generated method stub
// return Integer.valueOf(str);
// }
//
// };
//Lambda表达式
Function func = (str)->{return Integer.valueOf(str);};
System.out.println(func.apply("520"));
}
public void testPredicate() {
//匿名内部类
// Predicate predicate = new Predicate() {
// @Override
// public boolean test(Integer t) {
// return t>20?true:false;
// }
//
// };
//Lambda表达式
Predicate predicate = (t) -> {
return t > 20 ? true : false;
};
System.out.println(predicate.test(20));
}
public void testConsumer() {
//匿名内部类
// Consumer consumer = new Consumer() {
// @Override
// public void accept(String t) {
// System.out.println(t);
// }
// };
//Lambda表达式
Consumer consumer = (str) -> {
System.out.println(str);
};
consumer.accept("Hello");
}
public void testSupplier() {
// Supplier supplier = new Supplier() {
// @Override
// public String get() {
// return "OK";
// }
//
// };
//Lambda 表达式
Supplier supplier = () -> {
return "OK";
};
System.out.println(supplier.get());
}
}