Java学习路线-12:Lambda表达式与函数式编程

简介: Java学习路线-12:Lambda表达式与函数式编程

第29 章 : 函数式编程

131 Lambda表达式

JDK >= 1.8

函数式编程语言:

Scala https://www.scala-lang.org/

haskell https://www.haskell.org/

interface IMessage{
    public void send();
}
class Demo{
    public static void main(String[] args) {
        IMessage message = new IMessage() {
            public void send(){
                System.out.println("Hello Java");
            }
        };
        message.send();
        // Hello Java
    }
}

使用Lambda

@FunctionalInterface
interface IMessage{
    public void send();
}
class Demo{
    public static void main(String[] args) {
        IMessage message = () -> {
            System.out.println("Hello Java");
        };
        message.send();
        // Hello Java
    }
}

使用Lambda 表达式的要求:

SAM (Single Abstract Method)只有一个抽象方法

被称为函数式接口, 里边的方法只能有一个


Lambda表达式的格式:

方法没有参数: ()->{};

方法有参数:(参数, 参数)->{};

如果只有一行语句返回:(参数, 参数)->语句;

// 函数式接口
@FunctionalInterface
interface IMath{
    public int add(int x, int y);
}
class Demo{
    public static void main(String[] args) {
        IMath math = (x, y) -> {
            return x + y ;
        };
        System.out.println(math.add(1, 1));
        // 2
    }
}

一行返回语句简化写法

IMath math = (x, y) -> x + y ;

优势:简化代码

132 方法引用

引用数据类型最大的特点是可以进行内存指向处理

JDK < 1.8 对象引用操作

JDK >= 1.8 方法引用操作:不同的方法名可以描述同一个方法

引用静态方法: 类名称::static 方法名称

引用实例对象方法: 实例化对象::普通方法

引用特定类型的方法:特定类::普通方法

引用构造方法:类名称::new

示例:引用静态方法

// static String    valueOf(int i)
@FunctionalInterface
interface IFunction<P, R>{
    public R change(P p);
}
class Demo{
    public static void main(String[] args) {
        IFunction <Integer, String> function = String::valueOf ;
        String str = function.change(12);
        System.out.println(str.length());
        // 2
    }
}

方法引用可以为一个方法定义多个名字

要求是函数式接口

示例:引用实例对象方法

// String   toUpperCase()
@FunctionalInterface
interface IFunction<R>{
    public R upper();
}
class Demo{
    public static void main(String[] args) {
        IFunction <String> function = "Hello Java"::toUpperCase ;
        System.out.println(function.upper());
        // HELLO JAVA
    }
}

引用普通方法一般都需要实例化对象,如果不给出实例化对象,还要引用普通方法

可以使用引用特定类型的方法

示例:引用特定类型的方法

// int  compareTo(String anotherString)
@FunctionalInterface
interface IFunction<P>{
    public int compare(P p1, P p2);
}
class Demo{
    public static void main(String[] args) {
        IFunction <String> function = String::compareTo ;
        System.out.println(function.compare("hello", "java"));
        // -2
    }
}

示例:引用构造方法

class Person{
    private String name ;
    private int age ;
    public Person(String name, int age){
        this.name = name ;
        this.age = age ;
    }
    @Override
    public String toString(){
        return "Person<" + this.name + " " + this.age + ">" ;
    }
}
@FunctionalInterface
interface IFunction<C>{
    public C create(String name, int age);
}
class Demo{
    public static void main(String[] args) {
        IFunction <Person> function = Person::new ;
        System.out.println(function.create("张三", 18));
        // Person<张三 18>
    }
}

方法引用,只是弥补对引用支持

133 内建函数式接口

如果自定义Lambda表达式,需要使用@FunctionalInterface注解来声明

java.util.function 可以直接使用函数式接口

1、功能性函数式接口

@FunctionalInterface
public interface Function<T,R>{
     R apply(T t);
}f

示例

import java.util.function.Function;
class Demo{
    public static void main(String[] args) {
        Function<String, Boolean> fun = "**Hello"::startsWith;
        System.out.println(fun.apply("**"));
        // true
    }
}

2、消费型函数式接口

没有返回值

@FunctionalInterface
public interface Consumer<T>{
    void    accept(T t);
}

示例

import java.util.function.Consumer;
class Demo{
    public static void main(String[] args) {
        Consumer<String> fun = System.out::println;
        fun.accept("Hello");
        // Hello
    }
}

3、供给型函数式接口

没有接收参数,有返回值

@FunctionalInterface
public interface Supplier<T>{
    T   get();
}

示例

import java.util.function.Supplier;
class Demo{
    public static void main(String[] args) {
        Supplier<String> fun = "Hello Java"::toLowerCase;
        System.out.println(fun.get());
        // hello java
    }
}

4、断言型函数式接口

进行判断处理

@FunctionalInterface
public interface Predicate<T>{
    boolean test(T t);
}

示例

import java.util.function.Predicate;
class Demo{
    public static void main(String[] args) {
        Predicate<String> fun = "Hello"::equalsIgnoreCase;
        System.out.println(fun.test("HELLO"));
        // true
    }
}


相关文章
|
3天前
|
数据采集 人工智能 安全
|
13天前
|
云安全 监控 安全
|
4天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1085 152
|
18天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1751 9
|
9天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
694 152
|
11天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
660 14
|
6天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
438 5