04接口与lambda(案例)
517 字
3 分钟
04接口与lambda(案例)

InterfaceLambdaTraining 练习代码
package com.example.hr;
import java.util.function.Consumer;import java.util.function.Function;import java.util.function.Predicate;import java.util.function.Supplier;
public class InterfaceLambdaTraining {
public static class Ra1 implements Runnable { @Override // 仅供案例一使用 public void run() { System.out.println("普通方式"); } }
public static class Cs1 implements Consumer<String> { @Override // 仅供案例二使用 public void accept(String text) { System.out.println(text); } }
public static class Sp1 implements Supplier<String> { @Override // 仅供案例三使用 public String get() { return "你好Supplier"; } }
public static String createMessage() { return "方法引用"; // 仅供案例三使用 }
public static class Fc1 implements Function<String, Integer> { @Override // 仅供案例四使用 public Integer apply(String str) { return str.length(); } }
public static class Pd1 implements Predicate<String> { @Override // 仅供案例五 public boolean test(String text) { return text.length() > 3; } }
public static void main(String[] args) { // ===================== 案例一:Runnable 无参,无返回 ===================== Runnable ra1 = new Ra1(); ra1.run();
Runnable ra2 = new Runnable() { @Override public void run() { System.out.println("匿名内部类"); } }; ra2.run();
Runnable ra3 = () -> System.out.println("Lambda"); ra3.run();
// ===================== 案例二:Consumer 一个参数,无返回 ===================== Consumer<String> cs1 = new Cs1(); cs1.accept("普通方式");
Consumer<String> cs2 = new Consumer<String>() { @Override public void accept(String text) { System.out.println(text); } }; cs2.accept("匿名内部类");
Consumer<String> cs3 = (a) -> System.out.println(a); cs3.accept("Lambda");
Consumer<String> cs4 = System.out::println; cs4.accept("方法调用");
// ===================== 案例三:Supplier 无参,有返回 ===================== Supplier<String> sp1 = new Sp1(); System.out.println(sp1.get());
Supplier<String> sp2 = new Supplier<String>() { @Override public String get() { return "匿名内部类"; } }; System.out.println(sp2.get());
Supplier<String> sp3 = () -> "Lambda"; // 默认返回,也可以块包裹起来 // Supplier<String> sp3=()->{ // return "Lambda"; // }; System.out.println(sp3.get());
Supplier<String> sp4 = InterfaceLambdaTraining::createMessage; System.out.println(sp4.get());
// ===================== 案例四:Function 一个参数,有返回 ===================== Function<String, Integer> fc1 = new Fc1(); System.out.println(fc1.apply("hello"));
Function<String, Integer> fc2 = new Function<String, Integer>() { @Override public Integer apply(String str) { return str.length(); } }; System.out.println(fc2.apply("hello!"));
Function<String, Integer> fc3 = (a) -> a.length(); System.out.println(fc3.apply("hello!!"));
Function<String, Integer> fc4 = String::length; System.out.println(fc4.apply("hello!!!"));
// ===================== 案例五:Predicate 一个参数,返回boolean ===================== Predicate<String> pd1 = new Pd1(); System.out.println(pd1.test("hello"));
Predicate<String> pd2 = new Predicate<String>() { @Override public boolean test(String text) { return text.length() > 3; } };
Predicate<String> pd3 = (x) -> x.length() > 3;
Predicate<String> pd4 = pd1::test;
/* * 方法引用有4种: * 1. 对象::实例方法 * 2. 类名::静态方法 * 3. 类名::实例方法 * 4. 类名::new * * 类名::实例方法比较特殊,第一个传入的参数,会自动作为调用这个实例方法的对象(比如String::length) */ }}支持与分享
如果这篇文章对你有帮助,欢迎分享给更多人或打赏支持!
04接口与lambda(案例)
https://hecloud.top/posts/tech/java-se/04接口与lambda案例/相关文章智能推荐
1
05异常-断言-日志
深入技术2026-10-26
2
04接口-lambda-内部类
深入技术2026-10-16
3
12进阶:出事了怎么查
深入技术面对一个起不来或跑着跑着崩掉的容器,能在十分钟内定位到出错的那一层。
4
03继承与反射
深入技术2026-10-14
5
11进阶:日志与磁盘治理
深入技术看清容器的日志去了哪里、磁盘被谁吃掉,掌握限制与回收的手段。
随机文章随机推荐





