In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to master the Java8 Lambda expression function interface and method constructor array reference", 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 master the Lambda expression function interface and method constructor array reference in Java8"!
Catalogue
Overview of functional Interface
Functional interface example
1. Runnable interface
2. Custom functional interface
3. Pass the Lambda expression as a parameter
Built-in functional interface
Brief introduction of Lambda
Lambda syntax
Method reference
Constructor reference
Array reference
Overview of functional Interface
An interface that contains only one abstract method is called a functional interface.
You can create objects for this interface through Lambda expressions.
You can use the @ FunctionalInterface annotation on an interface to check whether it is a functional interface. The javadoc also contains a declaration that the interface is a functional interface.
The rich functional interface of Java 8 is defined under the java.util.function package.
In functional programming languages, functions are treated as first-class citizens. In programming languages that treat functions as first-class citizens, the type of Lambda expression is a function. But in Java8, it's different. In Java8, Lambda expressions are objects, not functions, and they must be attached to a special class of object types-functional interfaces.
To put it simply, in Java8, an Lambda expression is an example of a functional interface. This is the relationship between Lambda expressions and functional interfaces. That is, as long as an object is an instance of a functional interface, the object can be represented by an Lambda expression. So everything represented by anonymous implementation classes can now be written in Lambda expressions.
Functional interface example 1. Runnable interface
2. Custom functional interface @ FunctionalInterfacepublic interface MyFunctional01 {public String getValue ();} / use generics @ FunctionalInterfacepublic interface MyFunctional02 {public T getValue (T t);} 3. Pass Lambda expression public class LambdaTest {public static void main (String [] args) {String newStr = toUpperString (str-> str.toUpperCase (), "abcDEf"); System.out.println (newStr) as a parameter } public static String toUpperString (MyFunc my,String str) {return my.getValue (str);} @ FunctionalInterfaceinterface MyFunc {public T getValue (T t);} built-in functional interface import java.util.function.Consumer / * * @ Author: Yeman * @ Date: 2021-10-03-22:50 * @ Description: * / public class LambdaTest {public static void main (String [] args) {Consumer consumer = I-> System.out.println (I); consumer.accept (9);}
Brief introduction of Lambda
Lambda is an anonymous function, and we can think of the Lambda expression as a piece of code that can be passed (passing the code like data). Using it, you can write more concise and flexible code. As a more compact code style, Java's language expression ability has been improved.
It is essentially an instance of a functional interface (with only one abstract method).
Lambda expressions: a new syntax element and operator introduced in the Java 8 language. This operator is "- >" and is called the Lambda operator or arrowhead operator. It divides the Lambda into two parts:
Left side of ①: specifies the list of parameters required by the Lambda expression.
To the right of ②: the Lambda body is specified, which is the method body of the abstract method.
Lambda syntax
1. No parameters and no return value
Public class LambdaTest {public static void main (String [] args) {/ / does not use the Lambda expression Runnable runnable1 = new Runnable () {@ Override public void run () {System.out.println ("this is the first syntax!") ;}}; runnable1.run (); System.out.println ("= ="); / / use the Lambda expression Runnable runnable2 = ()-> {System.out.println ("this is a syntax for Lambda expressions!") ; runnable2.run ();}}
2. One parameter with no return value
3. Data types can be omitted because they can be inferred by the compiler, which is called "type inference"
4. If Lambda needs only one parameter, the parentheses of the parameter can be omitted
5. Lambda requires two or more parameters, multiple execution statements, and can have a return value
6. When there is only one statement in the body of Lambda, both return and large parentheses can be omitted
Method reference
When the operation to be passed to the Lambda body, there is already a method to implement, you can use the method reference.
A method reference can be seen as a deep expression of an Lambda expression. In other words, a method reference is a Lambda expression, an instance of a functional interface, pointing to a method by its name, which can be thought of as a syntactic sugar of a Lambda expression.
Requirement: the parameter list and return value type of the abstract method that implements the interface must be consistent with the parameter list and return value type of the method referenced by the method.
Format: use the operator "::" to separate the class (or object) from the method name.
There are three main uses:
① object:: non-static method name
② class:: static method name
③ class:: non-static method name
Example:
Import java.util.Comparator;import java.util.function.Consumer;/** * @ Author: Yeman * @ Date: 2021-10-03-22:50 * @ Description: * / public class LambdaTest {public static void main (String [] args) {Consumer consumer = I-> System.out.println (I); consumer.accept (9); Consumer stringConsumer = System.out:: println; stringConsumer.accept ("Hello") Comparator comparable = (XMague y)-> Integer.compare (XMague y); System.out.println (comparable.compare (2Magin9)); Comparator integerComparator = Integer:: compare; System.out.println (integerComparator.compare (9page2));}} Constructor reference
Format: ClassName::new
Combined with functional interface, it is automatically compatible with methods in functional interface. You can assign a constructor reference to a defined method, requiring that the constructor parameter list be consistent with the parameter list of the abstract method in the interface, and the return value of the method is the object of the constructor corresponding class.
Array reference
If you can think of an array as a class, an array reference is similar to a constructor reference.
Format: type []:: new
Import java.util.function.Function;/** * @ Author: Yeman * @ Date: 2021-10-03-22:50 * @ Description: * / public class LambdaTest {public static void main (String [] args) {Function function = length-> new String [length]; String [] apply = function.apply (3); System.out.println (apply [2]); System.out.println ("=") Function function1 = String []:: new; System.out.println (function1.apply (5));}} so far, I believe you have a deeper understanding of "how to master the functional interface of Lambda expressions in Java8 and the reference to the array of method constructors". 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.