Comparator
public interface Comparator {
int compare(T o1, T o2);
}
public class ComparatorLambda {
public static void main(String[] args) {
String[] strs = {"kobe","james","jordan","durant"};
//使用匿名内部类的方式实现比较器
Comparator comparator = new Comparator() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
};
// Arrays.sort(strs,comparator);
// System.out.println(Arrays.toString(strs));
//TODO 用Lambda表达式实现
Arrays.sort(strs,((o1, o2) -> o1.length() - o2.length()));
System.out.println(Arrays.toString(strs));
}
}
Consumer
public interface Consumer {
void accept(T t);
default Consumer andThen(Consumer super T> after) {
Objects.requireNonNull(after);
return (T t) -> { accept(t); after.accept(t); };
}
public class ConsumerLambda {
public static void main(String[] args) {
// consumerString(s -> System.out.println(s));
consumerString(
s -> System.out.println(s.toUpperCase()),
s -> System.out.println(s.toLowerCase())
);
}
public static void consumerString(Consumer fun){
fun.accept("Hello");
}
public static void consumerString(Consumer first,Consumer sec){
first.andThen(sec).accept("Hello");
}
}
Function
public interface Function {
R apply(T t);
}
public class FunctionLambda {
public static void main(String[] args) {
method(s -> Integer.parseInt(s) + 10,
integer -> integer *= 10);
}
static void method(Function one,Function two){
int num = one.andThen(two).apply("10");
System.out.println(num+20);
}
}
Predicate
public interface Predicate {
boolean test(T t);
default Predicate and(Predicate super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) && other.test(t);
}
default Predicate negate() {
return (t) -> !test(t);
}
default Predicate or(Predicate super T> other) {
Objects.requireNonNull(other);
return (t) -> test(t) || other.test(t);
}
public class PredicateLambda {
public static void main(String[] args) {
andMethod(s -> s.contains("H"),s -> s.contains("w"));
orMethod(s -> s.contains("H"),s -> s.contains("W"));
negateMethod(s -> s.length() < 5);
}
static void andMethod(Predicate one,Predicate two){
boolean flag = one.and(two).test("Helloworld");
System.out.println("字符串符合要求吗:"+ flag);
}
static void orMethod(Predicate one,Predicate two){
boolean flag = one.or(two).test("Helloworld");
System.out.println("字符串符合要求吗:"+ flag);
}
static void negateMethod(Predicate one){
boolean flag = one.negate().test("Helloworld");
System.out.println("字符串很长吗:"+ flag);
}
}
Runnable
public interface Runnable {
public abstract void run();
}
public class RunnableLambda {
public static void main(String[] args) {
//使用匿名内部类的方式,实现多线程
new Thread(new Runnable() {
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name + " 线程已经启动");
}
}).start();
//TODO 使用 Lambda表达式,实现多线程
new Thread(
() -> {
String name = Thread.currentThread().getName();
System.out.println(name + " 线程已经启动2");
}
).start();
}
}
Supplier
public interface Supplier {
T get();
}
public class SupplierLambda {
public static void main(String[] args) {
int[] arr = {1,2,3,4,555,8};
int max = getMax(() -> {
int temp = arr[0];
for (int i = 1; i < arr.length; i++) {
if (arr[i] > temp)
temp = arr[i];
}
return temp;
});
System.out.println(max);
}
public static int getMax(Supplier supplier){
return supplier.get();
}
}