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方法里用到双冒号的几率较大,其他地方我们用的较少,这种用法简洁明了,推荐使用。

相关推荐
IT空门:门主11 分钟前
Spring 注入三剑客:@Resource、@Autowired、@RequiredArgsConstructor 到底该用哪个?
java·后端·spring
ServBay16 分钟前
云端 AI 蜜月期宣告结束,为什么 2026 年开发者转向本地优先架构
后端·ai编程
IT_陈寒22 分钟前
Vite这个坑我帮你踩了,动态导入居然这样才生效
前端·人工智能·后端
Sam_Deep_Thinking34 分钟前
Spring Boot 的启动原理是什么?
java·spring boot·后端
南部余额41 分钟前
Spring WebClient 从入门到精通
java·后端·spring
摇滚侠1 小时前
Spring 零基础入门到进阶 基于注解管理 Bean 38-43
xml·java·后端·spring·intellij-idea
SamDeepThinking1 小时前
我们当年是如何真实落地BFF的?
java·后端·架构
Asmewill1 小时前
Centos系统docker时间同步方案
后端
用户8356290780511 小时前
使用 Python 操作 Word 评论和回复
后端·python
心在飞扬1 小时前
CentOS + Node.js 全套部署命令
后端