- 使用Lambda的前提
????????必须有一个函数式接口: 有且只有一个抽象方法的接口
????????@FunctionnalInterface注解
- 常见的函数式接口
- Runnable / Callable
- Supplier / Consumer
- Comparator
- Predicate
- Function
package tech.flygo.lambda.demo3;
/**
* @description: Runnable Lambda测试类
* @author: flygo
* @time: 2023/5/13 19:52
*/
public class RunnableLambda {
public static void main(String[] args) {
// 实现匿名内部类的方式,实现多线程
new Thread(new Runnable() {
@Override
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name + "线程已启动");
}
}).start();
// 使用Lambda表达式,实现多线程
new Thread(() -> {
String name = Thread.currentThread().getName();
System.out.println(name + "线程已启动");
}).start();
// 使用优化的Lambda实现多线程
new Thread(() -> System.out.println(Thread.currentThread().getName())).start();
}
}
package tech.flygo.lambda.demo3;
import java.util.function.Supplier;
/**
* @description: 提供者Lambda测试类
* @author: flygo
* @time: 2023/5/13 20:05
*/
public class SupplierLambda {
public static void main(String[] args) {
int[] array = {3, 4, 8, 99, 444};
// 调用getMax方法,参数传递Lambda
int maxNum = getMax(() -> {
int temp = array[0];
// 查找数组的最大值
for (int i : array) {
if (temp < i) {
temp = i;
}
}
return temp;
});
System.out.println(maxNum);
}
// 定义一个方法的参数传递Supplier传递使用泛型Integer
public static int getMax(Supplier<Integer> supplier) {
return supplier.get();
}
}
package tech.flygo.lambda.demo3;
import java.util.function.Consumer;
/**
* @description: 消费者Lambda测试类
* @author: flygo
* @time: 2023/5/13 20:21
*/
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())
);
}
static void consumerString(Consumer<String> consumer) {
consumer.accept("Hello");
}
static void consumerString(Consumer<String> one, Consumer<String> two) {
one.andThen(two).accept("Hello");
}
}
package tech.flygo.lambda.demo3;
import java.util.Arrays;
import java.util.Comparator;
/**
* @description: 比较器Lambda测试类
* @author: flygo
* @time: 2023/5/13 20:32
*/
public class ComparatorLambda {
public static void main(String[] args) {
String[] strs = {"adbc", "avc", "de", "dfafafdd"};
// 使用匿名内部类的方式,实现比较器
Comparator<String> comparator = new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
return o1.length() - o2.length();
}
};
Arrays.sort(strs, comparator);
System.out.println(Arrays.toString(strs));
// 使用Lambda表达式实现
Arrays.sort(strs, (o1, o2) -> o1.length() - o2.length());
System.out.println(Arrays.toString(strs));
Arrays.sort(strs, Comparator.comparingInt(String::length));
System.out.println(Arrays.toString(strs));
}
}
package tech.flygo.lambda.demo3;
import java.util.function.Predicate;
/**
* @description: 断言Lambda测试类
* @author: flygo
* @time: 2023/5/13 22:10
*/
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<String> one, Predicate<String> two) {
boolean isValid = one.and(two).test("Helloworld");
System.out.println("字符串符合要求吗:" + isValid);
}
static void orMethod(Predicate<String> one, Predicate<String> two) {
boolean isValid = one.or(two).test("HelloWorld");
System.out.println("字符串符合要求吗:" + isValid);
}
static void negateMethod(Predicate<String> predicate) {
boolean validLength = predicate.negate().test("HelloWorld");
System.out.println("字符串很长吗?" + validLength);
}
}
package tech.flygo.lambda.demo3;
import java.util.function.Function;
/**
* @description: 函数Lambda测试类
* @author: flygo
* @time: 2023/5/13 22:21
*/
public class FunctionLambda {
public static void main(String[] args) {
// 传入的值为10,one Function 第一个 10 + 10 = 20,返回值20作为two函数的值进行处理 20 * 10 = 200
method(s -> Integer.parseInt(s) + 10, i -> i * 10);
String str = "张三,18";
int age = getAgeNum(str, s -> s.split(",")[1],
s -> Integer.parseInt(s),
n -> n += 100);
System.out.println(age);
}
static void method(Function<String, Integer> one, Function<Integer, Integer> two) {
int num = one.andThen(two).apply("10");
System.out.println(num + 20);
}
static int getAgeNum(String str, Function<String, String> one,
Function<String, Integer> two,
Function<Integer, Integer> three) {
return one.andThen(two).andThen(three).apply(str);
}
}