In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "Summary of the usage of lambda expressions in java8". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the summary of the usage of lambda expressions in java8.
Lambda expression of java8
Objective: behavior parameterization
A Lambda expression is a concise way to represent an anonymous function that can be passed: it doesn't have a name, but it has a list of arguments, a function body, a return type, and possibly a list of exceptions that can be thrown.
The basic syntax of Lambda is (parameters)-> expression or (parameters)-> {statements;}. Return is implied in the expression of (parameters)-> expression, such as the statement in the curly braces of ()-> 42; (parameters)-> {statements;}.
For example:
()-> 42 / / parameter is empty, return an int (List list)-> list.isEmpty () / / parameter is list, return a boolean (int x, int y)-> xroomy / / parameter is two int, return an int (String s)-> System.out.println (s); / / parameter is a String, no result (String s)-> {System.out.println (s) } / / Parameter is a String, print string
Where to use which lambda
A functional interface is an interface that defines only one abstract method, even if there are multiple default methods. FunctionalInterface marks a functional interface and adds compilation checks. The purpose of the default method in a functional interface is to change the published interface without breaking the existing implementation.
Lambda expressions can be used wherever a functional interface is accepted as a parameter.
Example:
Public void execute (Runnable r) {r.run ();} execute (()-> {}); / / use a functional interface where lambda,Runnable is a null parameter and no return value, that is, ()-> void / / fetch returns a functional interface, ()-> Stringpublic Callable fetch () {return ()-> "Tricky example; -)";}
Why is it only used in functional interfaces? Lambda expressions have no function names, only parameter lists, function bodies and return values. If the interface has multiple methods, it cannot be directly matched to the correct method, so only a functional interface of an abstract method can be satisfied.
Predicate
Java.util.function.Predicate is a functional interface with multiple default methods. The abstract method is (T t)-> bool. Take a look at the code and you will understand.
FunctionalInterfacepublic interface Predicate {/ / API method. The input parameter is generic T and returns bool. That is, (T t)-> bool boolean test (T t); / / default method, and operates default Predicate and (Predicate)
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.