Lambda表达式:
测试代码1:
package test.lambda;
public class LambdaDemo { // 实现Runnable接口的类 static class MyThread implements Runnable { @Override public void run() { System.out.println("线程运行中:通过实现Runnable接口"); } } public static void main(String[] args) { // 通过实现Runnable接口的类启动线程 Thread thread1 = new Thread(new MyThread()); thread1.start(); // 使用匿名内部类启动线程 Thread thread2 = new Thread(new Runnable() { @Override public void run() { System.out.println("线程运行中:通过匿名内部类"); } }); thread2.start(); // 使用Lambda表达式启动线程 Thread thread3 = new Thread(() -> System.out.println("线程运行中:通过Lambda表达式")); thread3.start(); // 由于线程调度的不确定性,三个线程的输出顺序变化。 }
}
运行结果如下:
测试代码2:
package test.lambda;
//定义一个函数式接口,包含一个无参无返回值的抽象方法。
@FunctionalInterface
interface Action {void perform();
}class TaskExecutor {// 定义一个方法,该方法接受一个Action类型的参数void execute(Action action) {// 调用Action接口的perform方法action.perform();}
}public class LambdaTest {public static void main(String[] args) {// 创建TaskExecutor的实例TaskExecutor executor = new TaskExecutor();// 使用匿名内部类传递Action接口的实例 executor.execute(new Action() { @Override public void perform() { System.out.println("通过匿名内部类执行一个无参无返回值的操作"); } }); //使用Lambda表达式作为参数传递给execute方法。//这个Lambda表达式实现了Action接口的perform方法,并在其中打印一条消息。executor.execute(() -> {System.out.println("通过Lambda表达式执行一个无参无返回值的操作");});}
}
运行结果如下:
测试代码3:
package test.lambda;//定义一个函数式接口,包含一个有参无返回值的抽象方法。
@FunctionalInterface
interface Greeter {void greet(String name);
}class GreetingService {// 该方法接收一个Greeter类型的参数并调用其greet方法void sayHello(Greeter greeter) {greeter.greet("World"); }
}public class LambdaWithParameters {public static void main(String[] args) {// 创建GreetingService的实例GreetingService service = new GreetingService();//使用匿名内部类service.sayHello(new Greeter() {@Overridepublic void greet(String name) {System.out.println("Hello, " + name + "!!!!!!!!!!!");}});// 使用Lambda表达式传递Greeter接口的实例service.sayHello((String name) -> {System.out.println("Hello, " + name + "**********");});// 更简洁的Lambda表达式(在参数类型可以推断出来时,可以省略参数类型)service.sayHello(name -> System.out.println("Hello, " + name + "..........."));}
}
运行结果如下:
测试代码4:
package test.lambda;
//定义一个函数式接口,接收一个字符串参数并返回一个布尔值。
@FunctionalInterface
interface StringChecker {boolean check(String str);
}class StringProcessor {// 该方法接收一个StringChecker和一个字符串,然后调用StringChecker的check方法。void processString(StringChecker checker, String input) {if (checker.check(input)) {System.out.println(input + " meets the criteria.");} else {System.out.println(input + " does not meet the criteria.");}}
}public class LambdaStringExample {public static void main(String[] args) {// 创建StringProcessor的实例StringProcessor processor = new StringProcessor();//使用匿名内部类processor.processString(new StringChecker() {@Overridepublic boolean check(String str) {System.out.print("匿名内部类.............."+"\n");return false;}}, null);// 使用Lambda表达式传递StringChecker接口的实例// 检查字符串长度是否大于5processor.processString(str -> str.length() > 5, "Hello");processor.processString(str -> str.length() > 5, "Hello, World!");// 使用Lambda表达式传递StringChecker接口的实例// 检查字符串是否包含特定子串processor.processString(str -> str.contains("Java"), "This is a Java program.");processor.processString(str -> str.contains("Python"), "This is a Java program.");}
}
运行结果如下:
....................................................................................................................................................
测试代码:
package test.lambda;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;//定义函数式接口
@FunctionalInterface
interface Greeting {void greet(String name);
}@FunctionalInterface
interface StringSupplier {String get();
}@FunctionalInterface
interface Sum {int sum(int a, int b);
}public class LambdaOmitMode {public static void main(String[] args) {// 使用Lambda表达式,省略参数类型,实现Greeting接口Greeting greeting = name -> System.out.println("Hello, " + name + "!");greeting.greet("Lambda");// 使用Lambda表达式,省略大括号、分号和return关键字,实现StringSupplier接口StringSupplier supplier = () -> "Hello from Lambda Supplier!";System.out.println(supplier.get());// 使用Lambda表达式,省略参数类型但不省略小括号(因为有两个参数),实现Sum接口Sum sum = (a, b) -> a + b;System.out.println("Sum: " + sum.sum(5, 3));// 过滤出长度大于3的字符串List<String> strings = Arrays.asList("Java", "Python", "C++", "Scala");List<String> filtered = strings.stream().filter(s -> s.length() > 3) .collect(Collectors.toList());// 打印过滤后的结果System.out.println("Filtered Strings: " + filtered);}
}
运行结果如下:
....................................................................................................................................................