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 Lambda expression and functional Interface in JAVA

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly shows you the "JAVA Lambda expression and functional interface how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "JAVA Lambda expression and functional interface how to use" this article.

Lambda expressions were born to solve the problem of code redundancy of anonymous inner classes created by JAVA. Examples are as follows:

Public class Lambda {public static void main (String [] args) {Gog gog = new Gog () {@ Override public void say () {System.out.println ("WOW");}; gog.say ();}} interface Gog {void say ();}

Here we want to implement the say method of the interface. Since instantiating the interface creates an anonymous inner class, we need to implement all the methods in the interface. A lot of code is redundant to implement a simple method. So what if we use Lambda expressions to simplify. Examples are as follows:

Public class Lambda {public static void main (String [] args) {/ / Lambda expression is written as follows: ()-> {function implementation} when the function implementation has only one line of code, the curly braces can be omitted as follows: Gog gog = ()-> System.out.println ("WOW"); gog.say ();}} interface Gog {void say ();}

An interface that contains only one abstract method is called a functional interface. Only functional interfaces can be simplified using Lambda expressions. Actually, the reason is very simple. If there are multiple methods in the interface, the Lambda expression will not be able to explicitly call that function.

According to the return value and parameter list of abstract functions, functional interfaces can be simply divided into the following four types:

/ / 1. Supply API: Interface Supplier has no parameters and has a return value of interface Supplier {String say ();} / 2. Consumer API: Interface Consumer has only input and no return value interface Consumer {void say (String name);} / / 3. API for breaking type: the parameter t is passed in Interface Predicate, and the return type is boolean type interface Predicate {boolean say (String name);} / / 4. Functional API: Interface Function input parameter type is T, and return value type is Rinterface Function {String say (int age);}

Give us a few Lambda functions that are commonly used around us.

Public class MyThread {public static void main (String [] args) {new Thread (()-> {System.out.println ("create thread");}) .start ();}}

As the above code must be very familiar with, a very simple way to create threads. Let's go to the constructor corresponding to Thread.

As you can see, the input parameter of the constructor we call is the Runnable interface, and when we look at our creation, we clearly use the Lambda expression, so we can conclude that the Runnable interface must be a functional interface.

Attention! Since functions in an interface can have a default implementation after jdk8, this method of having a default implementation does not prevent the interface from becoming a functional interface. Examples are as follows:

Public class Lambda {public static void main (String [] args) {Supplier supplier = ()-> "wow"; System.out.println (supplier.say ());} interface Supplier {String say (); default String name () {return ";}}

There are two functions in the Supplier interface, but they can still be functional interfaces, which can be simplified with Lambda expressions.

These are all the contents of the article "how to use Lambda expressions and functional interfaces in JAVA". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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