Java 中的 :: 使用方式

Repository: Author: GitHub

Introduction:

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

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

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

Copyright: 唐涛 HOME | 首页 © 2020 Java中的双冒号运算符使用总结 - 图1 Java中的双冒号运算符使用总结 - 图2

Email: tangtao2099@outlook.com

Link: 知乎 GitHub 语雀


使用范围

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

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

使用总结

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

Lambda expression:
  1. stream.forEach( s-> System.out.println(s));

使用::运算符
  1. stream.forEach( System.out::println(s));

Code
  1. package tao;
  2. import java.util.function.Supplier;
  3. import java.util.stream.Stream;
  4. /**
  5. * @Author TangTao tangtao2099@outlook.com
  6. * @GitHub https://github.com/tangtaoshadow
  7. * @Website https://www.promiselee.cn/tao
  8. * @ZhiHu https://www.zhihu.com/people/tang-tao-24-36
  9. * @ClassName ColonOperator
  10. * @Note
  11. * @CreateTime 2020-06-22 03:32:00
  12. * @UpdateTime 2020-06-22 03:32:00
  13. */
  14. public class ColonOperator {
  15. public static void main(String[] args) {
  16. Supplier<Stream<String>> streamSupplier = () -> Stream.of("TangTao", "HangZhou", "China", "Stream");
  17. // Lambda
  18. streamSupplier.get().forEach(s -> System.out.println(s));
  19. System.out.println("====================");
  20. // ::
  21. streamSupplier.get().forEach(System.out::println);
  22. }
  23. }

💨静态方法

  1. package tao;
  2. import java.util.function.Supplier;
  3. import java.util.stream.Stream;
  4. /**
  5. * @Author TangTao tangtao2099@outlook.com
  6. * @GitHub https://github.com/tangtaoshadow
  7. * @Website https://www.promiselee.cn/tao
  8. * @ZhiHu https://www.zhihu.com/people/tang-tao-24-36
  9. * @ClassName ColonStatic
  10. * @Note
  11. * @CreateTime 2020-06-22 03:48:00
  12. * @UpdateTime 2020-06-22 03:48:00
  13. */
  14. public class ColonStatic {
  15. static void func(String s) {
  16. System.out.println("static func: " + s);
  17. }
  18. public static void main(String[] args) {
  19. Supplier<Stream<String>> streamSupplier = () -> Stream.of("TangTao", "HangZhou", "China", "Stream");
  20. streamSupplier.get().forEach(ColonStatic::func);
  21. }
  22. }

输出

  1. static func: TangTao
  2. static func: HangZhou
  3. static func: China
  4. static func: Stream

💨实例方法

  1. package tao;
  2. import java.util.function.Supplier;
  3. import java.util.stream.Stream;
  4. /**
  5. * @Author TangTao tangtao2099@outlook.com
  6. * @GitHub https://github.com/tangtaoshadow
  7. * @Website https://www.promiselee.cn/tao
  8. * @ZhiHu https://www.zhihu.com/people/tang-tao-24-36
  9. * @ClassName ColonInstance
  10. * @Note
  11. * @CreateTime 2020-6-22 03:54:29
  12. * @UpdateTime 2020-6-22 03:54:54
  13. */
  14. public class ColonInstance {
  15. void func(String s) {
  16. System.out.println("instance func: " + s);
  17. }
  18. public static void main(String[] args) {
  19. Supplier<Stream<String>> streamSupplier = () -> Stream.of("TangTao", "HangZhou", "China", "Stream");
  20. streamSupplier.get().forEach((new ColonInstance())::func);
  21. }
  22. }

输出

  1. instance func: TangTao
  2. instance func: HangZhou
  3. instance func: China
  4. instance func: Stream

💨构造函数

  1. package tao;
  2. import java.util.function.Supplier;
  3. import java.util.stream.Stream;
  4. /**
  5. * @Author TangTao tangtao2099@outlook.com
  6. * @GitHub https://github.com/tangtaoshadow
  7. * @Website https://www.promiselee.cn/tao
  8. * @ZhiHu https://www.zhihu.com/people/tang-tao-24-36
  9. * @ClassName ColonConstructor
  10. * @Note
  11. * @CreateTime 2020-06-22 04:19:00
  12. * @UpdateTime 2020-06-22 04:19:00
  13. */
  14. public class ColonConstructor {
  15. ColonConstructor(String s) {
  16. System.out.println("constructor: " + s);
  17. }
  18. public static void main(String[] args) {
  19. Supplier<Stream<String>> streamSupplier = () -> Stream.of("TangTao", "HangZhou", "China", "Stream");
  20. streamSupplier.get().forEach(ColonConstructor::new);
  21. }
  22. }

输出

  1. constructor: TangTao
  2. constructor: HangZhou
  3. constructor: China
  4. constructor: Stream

💨Super 方法

语法

  1. (super::methodName)
  1. package tao;
  2. import java.util.function.Function;
  3. import java.util.function.Supplier;
  4. import java.util.stream.Stream;
  5. /**
  6. * @Author TangTao tangtao2099@outlook.com
  7. * @GitHub https://github.com/tangtaoshadow
  8. * @Website https://www.promiselee.cn/tao
  9. * @ZhiHu https://www.zhihu.com/people/tang-tao-24-36
  10. * @ClassName ColonSuper
  11. * @Note
  12. * @CreateTime 2020-06-22 04:02:00
  13. * @UpdateTime 2020-06-22 04:02:00
  14. */
  15. class ColonSuperParent {
  16. String func(String s) {
  17. return "parent: " + s + "\n";
  18. }
  19. }
  20. public class ColonSuper extends ColonSuperParent {
  21. @Override
  22. String func(String s) {
  23. Function<String, String>
  24. func = super::func;
  25. String s1 = func.apply(s);
  26. s1 += "son: " + s + "\n";
  27. System.out.println(s1);
  28. return s1;
  29. }
  30. public static void main(String[] args) {
  31. Supplier<Stream<String>> streamSupplier = () -> Stream.of("TangTao", "HangZhou", "China", "Stream");
  32. streamSupplier.get().forEach(new ColonSuper()::func);
  33. }
  34. }

输出

  1. parent: TangTao
  2. son: TangTao
  3. parent: HangZhou
  4. son: HangZhou
  5. parent: China
  6. son: China
  7. parent: Stream
  8. son: Stream

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

  1. package tao;
  2. import java.util.function.Supplier;
  3. import java.util.stream.Stream;
  4. /**
  5. * @Author TangTao tangtao2099@outlook.com
  6. * @GitHub https://github.com/tangtaoshadow
  7. * @Website https://www.promiselee.cn/tao
  8. * @ZhiHu https://www.zhihu.com/people/tang-tao-24-36
  9. * @ClassName ColonArbitrary
  10. * @Note
  11. * @CreateTime 2020-06-22 04:11:00
  12. * @UpdateTime 2020-06-22 04:11:00
  13. */
  14. class ArbitraryClass {
  15. private String s;
  16. ArbitraryClass(String s) {
  17. this.s = s;
  18. }
  19. public void func() {
  20. System.out.println("ArbitraryClass func: " + this.s);
  21. }
  22. }
  23. public class ColonArbitrary {
  24. public static void main(String[] args) {
  25. Supplier<Stream<ArbitraryClass>> streamSupplier = () -> Stream.of(
  26. new ArbitraryClass("TangTao"),
  27. new ArbitraryClass("HangZhou"),
  28. new ArbitraryClass("China"),
  29. new ArbitraryClass("Stream"));
  30. streamSupplier.get().forEach(ArbitraryClass::func);
  31. }
  32. }

输出

  1. ArbitraryClass func: TangTao
  2. ArbitraryClass func: HangZhou
  3. ArbitraryClass func: China
  4. ArbitraryClass func: Stream

原文链接:GitHub Web 更新时间:2020-6-22 05:03:14