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案例/
作者
贺小云
发布于
2026-10-22
许可协议
CC BY-NC-SA 4.0

评论区

Profile Image of the Author
贺小云
热爱技术与折腾的博客 Dancing with uncertainty. 与不确定性共舞
公告
出于成本考虑,贺云已全面转型Serverless架构
分类
标签
碎碎念
站点统计
文章
91
分类
5
标签
44
总字数
103,192
运行时长
0 天
最后活动
0 天前
站点信息
构建平台
Cloudflare Pages
博客版本
Firefly v6.16.5
文章许可
CC BY-NC-SA 4.0