Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use double colons in java8

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to use double colons in java8". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use double colons in java8.

The code is actually very simple:

The previous code generally looked like this:

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);} / / the for each cycle below and the cycle above are equivalent al.forEach (x-> {AcceptMethod.printValur (x);});}}

Now the JDK double colon is:

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); / / the following method and the above equivalent Consumer methodParam = AcceptMethod::printValur; / / method parameter al.forEach (x-> methodParam.accept (x)); / / method execution accept}}

The execution results of all the above methods are as follows:

Print value: aprint value: bprint value: cprint value: d

In JDK8, the forEach method is implemented by default in interface Iterable 8, and the accept method in the interface Consumer added in JDK8 is called to execute the passed method parameters.

The JDK source code is as follows:

/ * * 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 *

The default implementation behaves as if: * {@ code * for (T: this) * action.accept (t); *} * * @ 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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report