Java中的双冒号运算符使用总结

Java 中的 :: 使用方式

Repository: Author: GitHub

Introduction:

Author:杭州电子科技大学 唐涛

CreateTime:2020-6-22 03:30:04

UpdateTime:2020-6-22 04:27:18

Copyright: 唐涛 HOME | 首页 © 2020

Email: tangtao2099@outlook.com

Link: 知乎 GitHub 语雀


使用范围

  • 静态方法
  • 实例方法
  • 构造函数

语法:<Class name>::<method name>

使用总结

💨输出流处理中的所有元素

Lambda expression:
stream.forEach( s-> System.out.println(s));
使用::运算符
stream.forEach( System.out::println(s));
Code
package tao;

import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @Author TangTao tangtao2099@outlook.com
 * @GitHub https://github.com/tangtaoshadow
 * @Website https://www.promiselee.cn/tao
 * @ZhiHu https://www.zhihu.com/people/tang-tao-24-36
 * @ClassName ColonOperator
 * @Note
 * @CreateTime 2020-06-22 03:32:00
 * @UpdateTime 2020-06-22 03:32:00
 */


public class ColonOperator {
    public static void main(String[] args) {

        Supplier<Stream<String>> streamSupplier = () -> Stream.of("TangTao", "HangZhou", "China", "Stream");

        // Lambda
        streamSupplier.get().forEach(s -> System.out.println(s));

        System.out.println("====================");
        // ::
        streamSupplier.get().forEach(System.out::println);
    }

}

💨静态方法

package tao;

import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @Author TangTao tangtao2099@outlook.com
 * @GitHub https://github.com/tangtaoshadow
 * @Website https://www.promiselee.cn/tao
 * @ZhiHu https://www.zhihu.com/people/tang-tao-24-36
 * @ClassName ColonStatic
 * @Note
 * @CreateTime 2020-06-22 03:48:00
 * @UpdateTime 2020-06-22 03:48:00
 */


public class ColonStatic {
    static void func(String s) {
        System.out.println("static func: " + s);
    }

    public static void main(String[] args) {
        Supplier<Stream<String>> streamSupplier = () -> Stream.of("TangTao", "HangZhou", "China", "Stream");
        streamSupplier.get().forEach(ColonStatic::func);
    }
}


输出

static func: TangTao
static func: HangZhou
static func: China
static func: Stream

💨实例方法

package tao;

import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @Author TangTao tangtao2099@outlook.com
 * @GitHub https://github.com/tangtaoshadow
 * @Website https://www.promiselee.cn/tao
 * @ZhiHu https://www.zhihu.com/people/tang-tao-24-36
 * @ClassName ColonInstance
 * @Note
 * @CreateTime 2020-6-22 03:54:29
 * @UpdateTime 2020-6-22 03:54:54
 */


public class ColonInstance {
    void func(String s) {
        System.out.println("instance func: " + s);
    }

    public static void main(String[] args) {
        Supplier<Stream<String>> streamSupplier = () -> Stream.of("TangTao", "HangZhou", "China", "Stream");
        streamSupplier.get().forEach((new ColonInstance())::func);
    }
}

输出

instance func: TangTao
instance func: HangZhou
instance func: China
instance func: Stream

💨构造函数

package tao;

import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @Author TangTao tangtao2099@outlook.com
 * @GitHub https://github.com/tangtaoshadow
 * @Website https://www.promiselee.cn/tao
 * @ZhiHu https://www.zhihu.com/people/tang-tao-24-36
 * @ClassName ColonConstructor
 * @Note
 * @CreateTime 2020-06-22 04:19:00
 * @UpdateTime 2020-06-22 04:19:00
 */


public class ColonConstructor {

    ColonConstructor(String s) {
        System.out.println("constructor: " + s);
    }

    public static void main(String[] args) {
        Supplier<Stream<String>> streamSupplier = () -> Stream.of("TangTao", "HangZhou", "China", "Stream");

        streamSupplier.get().forEach(ColonConstructor::new);

    }
}

输出

constructor: TangTao
constructor: HangZhou
constructor: China
constructor: Stream

💨Super 方法

语法

(super::methodName)
package tao;

import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @Author TangTao tangtao2099@outlook.com
 * @GitHub https://github.com/tangtaoshadow
 * @Website https://www.promiselee.cn/tao
 * @ZhiHu https://www.zhihu.com/people/tang-tao-24-36
 * @ClassName ColonSuper
 * @Note
 * @CreateTime 2020-06-22 04:02:00
 * @UpdateTime 2020-06-22 04:02:00
 */

class ColonSuperParent {
    String func(String s) {
        return "parent: " + s + "\n";
    }
}

public class ColonSuper extends ColonSuperParent {

    @Override
    String func(String s) {
        Function<String, String>
                func = super::func;
        String s1 = func.apply(s);
        s1 += "son: " + s + "\n";
        System.out.println(s1);
        return s1;
    }

    public static void main(String[] args) {
        Supplier<Stream<String>> streamSupplier = () -> Stream.of("TangTao", "HangZhou", "China", "Stream");

        streamSupplier.get().forEach(new ColonSuper()::func);
    }

}

输出

parent: TangTao
son: TangTao

parent: HangZhou
son: HangZhou

parent: China
son: China

parent: Stream
son: Stream

💨特定类型的任意对象的实例方法

package tao;

import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * @Author TangTao tangtao2099@outlook.com
 * @GitHub https://github.com/tangtaoshadow
 * @Website https://www.promiselee.cn/tao
 * @ZhiHu https://www.zhihu.com/people/tang-tao-24-36
 * @ClassName ColonArbitrary
 * @Note
 * @CreateTime 2020-06-22 04:11:00
 * @UpdateTime 2020-06-22 04:11:00
 */

class ArbitraryClass {

    private String s;

    ArbitraryClass(String s) {
        this.s = s;
    }

    public void func() {
        System.out.println("ArbitraryClass func: " + this.s);
    }
}

public class ColonArbitrary {

    public static void main(String[] args) {
        Supplier<Stream<ArbitraryClass>> streamSupplier = () -> Stream.of(
                new ArbitraryClass("TangTao"),
                new ArbitraryClass("HangZhou"),
                new ArbitraryClass("China"),
                new ArbitraryClass("Stream"));

        streamSupplier.get().forEach(ArbitraryClass::func);
    }

}

输出

ArbitraryClass func: TangTao
ArbitraryClass func: HangZhou
ArbitraryClass func: China
ArbitraryClass func: Stream

原文链接:GitHub Web

更新时间:2020-6-22 05:03:14

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值