java8中:: 用法示例(JDK8雙冒號(hào)用法)
JDK8中有雙冒號(hào)的用法,就是把方法當(dāng)做參數(shù)傳到stream內(nèi)部,使stream的每個(gè)元素都傳入到該方法里面執(zhí)行一下。
代碼其實(shí)很簡(jiǎn)單:
以前的代碼一般是如此的:
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循環(huán)和上面的循環(huán)是等價(jià)的
al.forEach(x->{
AcceptMethod.printValur(x);
});
}
}
現(xiàn)在JDK雙冒號(hào)是:
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);
//下面的方法和上面等價(jià)的
Consumer methodParam = AcceptMethod::printValur; //方法參數(shù)
al.forEach(x -> methodParam.accept(x));//方法執(zhí)行accept
}
}
上面的所有方法執(zhí)行玩的結(jié)果都是如下:
print value : a
print value : b
print value : c
print value : d
在JDK8中,接口Iterable 8中默認(rèn)實(shí)現(xiàn)了forEach方法,調(diào)用了 JDK8中增加的接口Consumer內(nèi)的accept方法,執(zhí)行傳入的方法參數(shù)。
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);
}
}
另外補(bǔ)充一下,JDK8改動(dòng)的,在接口里面可以有默認(rèn)實(shí)現(xiàn),就是在接口前加上default,實(shí)現(xiàn)這個(gè)接口的函數(shù)對(duì)于默認(rèn)實(shí)現(xiàn)的方法可以不用再實(shí)現(xiàn)了。類似的還有static方法?,F(xiàn)在這種接口除了上面提到的,還有BiConsumer,BiFunction,BinaryOperation等,在java.util.function包下的接口,大多數(shù)都有,后綴為Supplier的接口沒(méi)有和別的少數(shù)接口。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
java 數(shù)據(jù)的加密與解密普遍實(shí)例代碼
本篇文章介紹了一個(gè)關(guān)于密鑰查詢的jsp文件簡(jiǎn)單實(shí)例代碼,需要的朋友可以參考下2017-04-04
Java Swing組件單選框JRadioButton用法示例
這篇文章主要介紹了Java Swing組件單選框JRadioButton用法,結(jié)合具體實(shí)例形式分析了Swing單選框JRadioButton的使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2017-11-11
JAVA自定義注解實(shí)現(xiàn)接口/ip限流的示例代碼
本文主要介紹了JAVA自定義注解實(shí)現(xiàn)接口/ip限流的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
SpringBoot實(shí)戰(zhàn)之SSL配置詳解
今天小編就為大家分享一篇關(guān)于SpringBoot實(shí)戰(zhàn)之SSL配置詳解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02
logstash將mysql數(shù)據(jù)同步到elasticsearch方法詳解
這篇文章主要為大家介紹了logstash將mysql數(shù)據(jù)同步到elasticsearch方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Java Bean與Map之間相互轉(zhuǎn)化的實(shí)現(xiàn)方法
這篇文章主要介紹了Java Bean與Map之間相互轉(zhuǎn)化的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
Java實(shí)現(xiàn)普通類注入service對(duì)象
Java concurrency集合之LinkedBlockingDeque_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

