java8中:: 用法示例(JDK8双冒号用法)
作者:A点点圈圈A 发布时间:2023-11-25 06:21:21
标签:java8,双冒号,::
JDK8中有双冒号的用法,就是把方法当做参数传到stream内部,使stream的每个元素都传入到该方法里面执行一下。
代码其实很简单:
以前的代码一般是如此的:
public class AcceptMethod {
public static void printValur(String str){
System.out.println("print value : "+str);
}
public static void main(String[] args) {
List al = Arrays.asList("a","b","c","d");
for (String a: al) {
AcceptMethod.printValur(a);
}
//下面的for each循环和上面的循环是等价的
al.forEach(x->{
AcceptMethod.printValur(x);
});
}
}
现在JDK双冒号是:
public class MyTest {
public static void printValur(String str){
System.out.println("print value : "+str);
}
public static void main(String[] args) {
List al = Arrays.asList("a", "b", "c", "d");
al.forEach(AcceptMethod::printValur);
//下面的方法和上面等价的
Consumer methodParam = AcceptMethod::printValur; //方法参数
al.forEach(x -> methodParam.accept(x));//方法执行accept
}
}
上面的所有方法执行玩的结果都是如下:
print value : a
print value : b
print value : c
print value : d
在JDK8中,接口Iterable 8中默认实现了forEach方法,调用了 JDK8中增加的接口Consumer内的accept方法,执行传入的方法参数。
JDK源码如下:
/**
* Performs the given action for each element of the {@code Iterable}
* until all elements have been processed or the action throws an
* exception. Unless otherwise specified by the implementing class,
* actions are performed in the order of iteration (if an iteration order
* is specified). Exceptions thrown by the action are relayed to the
* caller.
*
* @implSpec
* <p>The default implementation behaves as if:
* <pre>{@code
* for (T t : this)
* action.accept(t);
* }</pre>
*
* @param action The action to be performed for each element
* @throws NullPointerException if the specified action is null
* @since 1.8
*/
default void forEach(Consumer<? super T> action) {
Objects.requireNonNull(action);
for (T t : this) {
action.accept(t);
}
}
另外补充一下,JDK8改动的,在接口里面可以有默认实现,就是在接口前加上default,实现这个接口的函数对于默认实现的方法可以不用再实现了。类似的还有static方法。现在这种接口除了上面提到的,还有BiConsumer,BiFunction,BinaryOperation等,在java.util.function包下的接口,大多数都有,后缀为Supplier的接口没有和别的少数接口。
来源:https://www.cnblogs.com/diandianquanquan/p/11411958.html
0
投稿
猜你喜欢
- java.util.concurrent包中的工具实现核心都是AQS,了解ReentrantLock的实现原理,需要先分析AQS以及AQS与
- 我们知道HashMap集合是允许存放null值的hashMap是根据key的hashCode来寻找存放位置的,那当key为null时, 怎么
- 一、简单介绍翻看Spring的源码时,发现@Bean注解的源码上标注了Since: 3.0,也就是说,@Bean注解是Spring从3.0版
- Mybatis与JPA的优缺点JPA java持久层API可理解为一种规范,Hibernate就是其具体一个实现。它的实现应用是Spring
- Interface Segregation Principle,ISP接口隔离原则主张使用多个专门的接口比使用单一的总接口要好。一个类对另外
- 阿里终面在线编程题,写出来与大家分享一下 有一个单向链表
- 推荐激活教程IntelliJ IDEA 2020最新激活码(亲测有效,可激活至 2089 年)最新idea2021注册码永久激活(激活到21
- 1、理论一般如果想将类注册到spring容器,让spring来完成实例化,常用方式如下:xml中通过bean节点来配置;使用@Service
- 在平时的开发中,我们会经常遇到这样一个需求,要在页面通过一个『导出』按钮把查询出的数据导出到 Excel 表格中。本文即为实现上述需求的一个
- 本文实例讲述了C#获取网页源代码的方法。分享给大家供大家参考。具体如下:public string GetPageHTML(string u
- 前几天在跟公司大佬讨论一个问题时,看到他使用Handler的一种方式,旁边的同事在说:以前不是这么用的啊。这个问题引发了我的好奇,虽然当时翻
- 日期和时间格式由 日期和时间模式字符串 指定。在 日期和时间模式字符串 中,未加引号的字母 'A' 到 'Z'
- 一、Java后端使用MultipartFile@PostMapping(value = "/upload")  
- [LeetCode] 159. Longest Substring with At Most Two Distinct Characters
- 1、静态代码块①、格式在java类中(方法中不能存在静态代码块)使用static关键字和{}声明的代码块:public class Code
- 目录环境准备1.数据库操作1.1获取所有数据库1.2获取指定库的所有集合名1.3.删除数据库2.文档操作2.1插入文档2.2查询文档2.3分
- foreach查询不出结果也不报错问题首先,执行的时候语法没有报错,其次sql语句拿到数据库去执行能查到数据,但是在接口这边返回空输数据,查
- 微服务治理Spring Cloud 工具套件为微服务治理提供了全面的技术支持。这些治理工具主要包括服务的注册与发现、负载均衡管理、动态路由、
- Spring Boot产生环形注入***************************APPLICATION FAILED TO STAR
- 在上一节中,我带大家学习了详解SpringBoot集成Redis来实现缓存技术方案,尤其是结合Spring Cache的注解的实现方案,接下