In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use method reference in Java 8". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use method references in Java 8.
In Java8, using a method reference, such as String::isEmpty, is very simple, but you cannot use the method reference that it negates. The content of this article is how to solve this problem so that we can use method references more comprehensively.
First, take a look at an example of using method references:
Stream.of ("A", "", "B") .filter (String::isEmpty) .count ()
The output of the above code is 1, which is the number of empty strings.
If we want to get the number of non-empty strings, we can't use method references directly. Stream.of ("A", "", "B") .filter (s->! s.isEmpty ()) .count ()
Predicate in Java8 has predicate.negate () that can be converted into a negative form of assertion, but String::isEmpty cannot do so (String::isEmpty.negate () or! String::isEmpty).
Because a method reference is not a lambda or function interface, it can be parsed into one or more function interfaces. For example, String::isEmpty can at least be parsed as follows:
Predicate
Function
To solve the above problem, we can explicitly convert the method reference to a function interface through some mechanism:
Public static Predicate as (Predicate predicate) {return predicate;} converts method references to function interfaces by using a static method, accepting method reference parameters and returning a function interface. We can then use the method reference to get the number of non-empty strings in the above example. Stream.of ("A", "", "B") .filter (as (String::isEmpty) .negate ()) .count ()
Further, various combinations of Predicate can be used.
Filter (as (String::isEmpty). Negate (). And ("A":: equals)) because a method reference may be resolved into multiple function interfaces, it is easy to cause confusion if we implement many as methods with different parameters. A better way is to add the type of function parameter to the method name to distinguish it.
Import java.util.function.*
Public class FunctionCastUtil {public static BiConsumer asBiConsumer (BiConsumer biConsumer) {return biConsumer;} public static BiFunction asBiFunction (BiFunction biFunction) {return biFunction;} public static BinaryOperator asBinaryOperator (BinaryOperator binaryOperator) {return binaryOperator;} public static BiPredicate asBiPredicate (BiPredicate biPredicate) {return biPredicate;} public static BooleanSupplier asBooleanSupplier (BooleanSupplier booleanSupplier) {return booleanSupplier } public static Consumer asConsumer (Consumer consumer) {return consumer;} public static DoubleBinaryOperator asDoubleBinaryOperator (DoubleBinaryOperator doubleBinaryOperator) {return doubleBinaryOperator;} public static DoubleConsumer asDoubleConsumer (DoubleConsumer doubleConsumer) {return doubleConsumer;} public static DoubleFunction asDoubleFunction (DoubleFunction doubleFunction) {return doubleFunction;} public static DoublePredicate asDoublePredicate (DoublePredicate doublePredicate) {return doublePredicate;} public static DoubleToIntFunction asDoubleToIntFunction (DoubleToIntFunction doubleToIntFunctiontem) {return doubleToIntFunctiontem } public static DoubleToLongFunction asDoubleToLongFunction (DoubleToLongFunction doubleToLongFunction) {return doubleToLongFunction;} public static DoubleUnaryOperator asDoubleUnaryOperator (DoubleUnaryOperator doubleUnaryOperator) {return doubleUnaryOperator;} public static Function asFunction (Function function) {return function;} public static IntBinaryOperator asIntBinaryOperator (IntBinaryOperator intBinaryOperator) {return intBinaryOperator;} public static IntConsumer asIntConsumer (IntConsumer intConsumer) {return intConsumer;} public static IntFunction asIntFunction (IntFunction intFunction) {return intFunction } public static IntPredicate asIntPredicate (IntPredicate intPredicate) {return intPredicate;} public static IntSupplier asIntSupplier (IntSupplier intSupplier) {return intSupplier;} public static IntToDoubleFunction asIntToDoubleFunction (IntToDoubleFunction intToDoubleFunction) {return intToDoubleFunction;} public static IntToLongFunction asIntToLongFunction (IntToLongFunction intToLongFunction) {return intToLongFunction;} public static IntUnaryOperator asIntUnaryOperator (IntUnaryOperator intUnaryOperator) {return intUnaryOperator;} public static LongBinaryOperator asLongBinaryOperator (LongBinaryOperator longBinaryOperator) {return longBinaryOperator } public static LongConsumer asLongConsumer (LongConsumer longConsumer) {return longConsumer;} public static LongFunction asLongFunction (LongFunction longFunction) {return longFunction;} public static LongPredicate asLongPredicate (LongPredicate longPredicate) {return longPredicate;} public static LongSupplier asLongSupplier (LongSupplier longSupplier) {return longSupplier;} public static LongToDoubleFunction asLongToDoubleFunction (LongToDoubleFunction longToDoubleFunction) {return longToDoubleFunction;} public static LongToIntFunction asLongToIntFunction (LongToIntFunction longToIntFunction) {return longToIntFunction } public static LongUnaryOperator asLongUnaryOperator (LongUnaryOperator longUnaryOperator) {return longUnaryOperator;} public static ObjDoubleConsumer asObjDoubleConsumer (ObjDoubleConsumer objDoubleConsumer) {return objDoubleConsumer;} public static ObjIntConsumer asObjIntConsumer (ObjIntConsumer objIntConsumer) {return objIntConsumer;} public static ObjLongConsumer asObjLongConsumer (ObjLongConsumer objLongConsumer) {return objLongConsumer;} public static Predicate asPredicate (Predicate predicate) {return predicate;} public static Supplier asSupplier (Supplier supplier) {return supplier } public static ToDoubleBiFunction asToDoubleBiFunction (ToDoubleBiFunction toDoubleBiFunction) {return toDoubleBiFunction;} public static ToDoubleFunction asToDoubleFunction (ToDoubleFunction toDoubleFunction) {return toDoubleFunction;} public static ToIntBiFunction asToIntBiFunction (ToIntBiFunction toIntBiFunction) {return toIntBiFunction;} public static ToIntFunction asToIntFunction (ToIntFunction ioIntFunction) {return ioIntFunction;} public static ToLongBiFunction asToLongBiFunction (ToLongBiFunction toLongBiFunction) {return toLongBiFunction;} public static ToLongFunction asToLongFunction (ToLongFunction toLongFunction) {return toLongFunction } public static UnaryOperator asUnaryOperator (UnaryOperator unaryOperator) {return unaryOperator;} private FunctionCastUtil () {}}
Stream.of ("A", "", "B") .filter (asPredicate (String::isEmpty) .negate ()) .count ()
Original: https://dzone.com/articles/put-your-java-8-method-references-to-work
Translation: https://www.rowkey.me/blog/2017/09/02/java8-method-reference-work/
At this point, I believe you have a deeper understanding of "how to use method references in Java 8". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.