Java【代码 23】使用双冒号 :: 简洁代码及方法引用(静态方法+构造方法+实例方法+函数式编程举例)

本文介绍了Java中双冒号(::)操作符的用法,主要用于方法引用。通过DemoEntity类和DoubleColonDemo示例,展示了四种典型用法:

  • 1)静态方法引用(类名::静态方法);
  • 2)构造方法引用(类名::new);
  • 3)实例方法引用(实例对象::实例方法);
  • 4)函数式编程中的方法引用。

在Stream操作中,双冒号语法使用频率较高,相比Lambda表达式,方法引用直接引用现有方法,代码更简洁。文章包含完整代码示例,演示了不同场景下的具体应用。

1.stream举例

1.1 实例方法

java 复制代码
List<String> list = paramIns.stream().map(paramIn::getXxx).collect(Collectors.toList());

1.2 静态方法

java 复制代码
ArrayList<String> strings = new ArrayList<>();
strings.forEach(System.out::println);

2.实例对象

java 复制代码
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DemoEntity {

    String demoName;
    String describe;

    public static String selfIntroduce(String code) {
        return "我是demo的代码:" + code;
    }

}

3.demo源码

3.1 静态方法引用【类名::静态方法名】

java 复制代码
public class DoubleColonDemo {

    public static void main(String[] args) {
        // 1.静态方法引用【类名::静态方法名】
        Function<String, Integer> functionParseInt = Integer::parseInt;
        Integer value = functionParseInt.apply("666");
        System.out.println(value);
        // 666
    }

}

3.2 构造方法引用【类名::new】

java 复制代码
public class DoubleColonDemo {

    public static void main(String[] args) {
 
        BiFunction<String, String, DemoEntity> biFunction = DemoEntity::new;
        DemoEntity demoEntity = biFunction.apply("双引号用法", "代码举例说明");
        System.out.println(demoEntity.toString());
        // DemoEntity(demoName=双引号用法, describe=代码举例说明)

    }
    
}

3.3 实例方法引用【实例对象::实例方法名】

java 复制代码
public class DoubleColonDemo {

    public static void main(String[] args) {

        String content = "FunctionSubstring";
        Function<Integer, String> functionSubstring = content::substring;
        String result = functionSubstring.apply(8);
        System.out.println(result);
        // Substring
        // 自定义的实例方法引用【实例对象::实例方法名】
        Function<String, String> selfIntroduce = DemoEntity::selfIntroduce;
        String selfIntroduceStr = selfIntroduce.apply("Do something!");
        System.out.println(selfIntroduceStr);
        // 我是demo的代码:Do something!

    }

}

3.4 函数式编程:方法引用

java 复制代码
public class DoubleColonDemo {

    public static void main(String[] args) {
     
        String parameter = "hello world!";
        System.out.println(speakOutLoud(String::toUpperCase, parameter));
        // HELLO WORLD!
    }

    /**
     * 测试参数是函数的方法
     *
     * @param function  方法传递的函数
     * @param parameter 方法传递的参数
     */
    private static String speakOutLoud(Function<String, String> function, String parameter) {
        return function.apply(parameter);
    }

}

3.5 方法汇总

java 复制代码
public class DoubleColonDemo {

    public static void main(String[] args) {
        // 1.静态方法引用【类名::静态方法名】
        Function<String, Integer> functionParseInt = Integer::parseInt;
        Integer value = functionParseInt.apply("666");
        System.out.println(value);
        // 666

        // 2.构造方法引用【类名::new】
        BiFunction<String, String, DemoEntity> biFunction = DemoEntity::new;
        DemoEntity demoEntity = biFunction.apply("双引号用法", "代码举例说明");
        System.out.println(demoEntity.toString());
        // DemoEntity(demoName=双引号用法, describe=代码举例说明)

        // 3.实例方法引用【实例对象::实例方法名】
        String content = "FunctionSubstring";
        Function<Integer, String> functionSubstring = content::substring;
        String result = functionSubstring.apply(8);
        System.out.println(result);
        // Substring
        // 自定义的实例方法引用【实例对象::实例方法名】
        Function<String, String> selfIntroduce = DemoEntity::selfIntroduce;
        String selfIntroduceStr = selfIntroduce.apply("Do something!");
        System.out.println(selfIntroduceStr);
        // 我是demo的代码:Do something!

        // 4.函数式编程:方法引用
        String parameter = "hello world!";
        System.out.println(speakOutLoud(String::toUpperCase, parameter));
        // HELLO WORLD!
    }

    /**
     * 测试参数是函数的方法
     *
     * @param function  方法传递的函数
     * @param parameter 方法传递的参数
     */
    private static String speakOutLoud(Function<String, String> function, String parameter) {
        return function.apply(parameter);
    }

}

4.总结

Lambda表达式也是一种函数式接口,它一般用自己提供方法体,而方法引用一般直接引用现成的方法。我们在stream方法里用到双冒号的几率较大,其他地方我们用的较少,这种用法简洁明了,推荐使用。

相关推荐
CaffeinePro几秒前
FastAPI自动接口文档定制与美化、权限管控
后端·fastapi
AI人工智能+电脑小能手8 分钟前
【大白话说Java面试题 第151题】【06_Spring篇】第11题:说一下 Spring Bean 的生命周期?
java·开发语言·后端·spring·面试
赫媒派2 小时前
Gin 12年零破坏API,架构哲学如何练成?
后端·go·gin
fliter3 小时前
Arborium:把 tree-sitter 语法高亮打包成 Rust 文档生态的基础设施
后端
张三丰23 小时前
不会写代码的高管用Claude Code两天上线新程序,工程师接手后发现:一个Bug,让AI一天烧掉一个月服务器费!
后端
Ai拆代码的曹操3 小时前
从一条转账 SQL 到分布式事务:5 种方案的全方位对比与实战
后端
掘金小豆3 小时前
Spring 事务失效的 6 大场景,你踩过几个?
后端·spring·面试
im_lanny4 小时前
Agent = Model + Harness:决定 AI 智能体上限的,往往不是模型而是“装具”
后端
阿文和她的Key4 小时前
AI新词太多?把它们串成一条线就清楚了
后端
笨鸟飞不快4 小时前
当规则比代码跑得快:我对用 LiteFlow 编排信贷业务的一点思考
后端·设计