欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

java8中:: 用法示例(JDK8雙冒號(hào)用法)

 更新時(shí)間:2019年09月03日 15:17:03   作者:A點(diǎn)點(diǎn)圈圈A  
這篇文章主要給大家介紹了關(guān)于java8 中的:: 用法(JDK8雙冒號(hào)用法)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用java8具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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)了。類(lèi)似的還有static方法。現(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í)現(xiàn)普通類(lèi)注入service對(duì)象

    Java實(shí)現(xiàn)普通類(lèi)注入service對(duì)象

    這篇文章主要介紹了Java實(shí)現(xiàn)普通類(lèi)注入service對(duì)象,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • SpringBoot在接收參數(shù)的七種方式詳解

    SpringBoot在接收參數(shù)的七種方式詳解

    這篇文章主要介紹了SpringBoot在接收參數(shù)的七種方式詳解,隨著前后端的分離,接口方式開(kāi)發(fā)成為普遍的開(kāi)發(fā)形式,前端相對(duì)于后端來(lái)說(shuō),常用的接口傳參方式就一定要了解和熟悉,下面?我們梳理了常用的七種?Controller層接受參數(shù)的方式,需要的朋友可以參考下
    2023-10-10
  • Java concurrency集合之LinkedBlockingDeque_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    Java concurrency集合之LinkedBlockingDeque_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理

    LinkedBlockingDeque是雙向鏈表實(shí)現(xiàn)的雙向并發(fā)阻塞隊(duì)列。該阻塞隊(duì)列同時(shí)支持FIFO和FILO兩種操作方式,即可以從隊(duì)列的頭和尾同時(shí)操作(插入/刪除);并且,該阻塞隊(duì)列是支持線(xiàn)程安全。
    2017-06-06
  • 最新評(píng)論