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/02 Report--
This article introduces the knowledge of "what is the difference between Lambda expressions in Java8 and Scala". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Why use Lambda expressions
Lambda expressions are commonly used in graphical user interface (GUI) development. Generally speaking, GUI programming connects program behavior to events. For example, when a user presses a button (triggering an event), your program needs to perform some behavior, perhaps storing some data in a data store. In Swing, you can use ActionListener to do this:
Class ButtonHandler implements ActionListener {public void actionPerformed (ActionEvent e) {/ / do something}} class UIBuilder {public UIBuilder () {button.addActionListener (new ButtonHandler ());}}
This example shows the use of the ButtonHandler class as a callback replacement. Here the ButtonHandler class contains only the actionPerformed methods defined by the ActionListener interface. We can use anonymous inner classes to simplify the code:
Class UIBuilder {public UIBuilder () {button.addActionListener (new ActionListener () {public void actionPerformed (ActionEvent event) {/ / do something}})}}
This makes the code much simpler. When you take a closer look at the code, you will see that we also create a class that generates only one instance, which only holds a separate method. This is exactly one of the kinds of problems that Lambda expressions can solve.
2. Lambda expression replaces function
An lambda expression is literally a function. It defines the input parameters and the body of a function. In Java 8, the syntax of the lambda expression has not been determined, but it should look something like this:
(type parameter)-> function_body
A specific example:
(String S1, String S2)-> s1.length ()-s2.length ()
This lambda expression is used to calculate the length difference between two strings. There are also some extended syntax, such as avoiding type definitions of parameters (we'll see examples in a moment) and using {and} to support multi-line definitions.
The Collections.sort () method is an ideal example of lambda expression. It allows us to sort strings by length:
List list = Array.asList ("loooooong", "short", "tiny"); Collections.sort (list, (String S1, String S2)-> s1.length ()-s2.length (); > "tiny", "short", "loooooong".
So, instead of typing an implemented Comparator (comparator) into the sort method, as java must now require, we can get the same result by passing an lambda expression.
3. Instead of closures, Lambda expressions
Lambda expressions have many interesting features. One of them is that they are closures. A closure allows a function to access variables outside the direct lexical scope.
String outer = "java 8" (String S1)-> s1.length ()-outer.length ()
In the example, the lambda expression accesses a variable defined outside the scope of the string outer. This is difficult for inline closures.
4. Lambda expressions also support type inference.
Type inference was introduced by java 7, but it also applies to lambda expressions. Simply put, type inference means that programmers can omit type definitions anywhere a compiler can automatically infer types. If the type inference can be applied to the previous sort lambda expression, it can be written as follows:
List list = Arrays.asList (...); Collections.sort (list, (S1, S2)-> s1.length ()-s2.length ())
As you can see, the types of parameters S1 and S2 are omitted. Because the compiler knows that list is a collection of strings, it knows that lambda expressions used as comparators must be of the same type. Therefore, this type does not need to be explicitly declared, even if you have the freedom to do so.
The main advantage of type inference is to reduce boilerplate code, and if the compiler can identify types for us, why do we have to define them ourselves.
5. Cherish Lambda expressions and stay away from anonymous inner classes
Let's see why lambda expressions and type inferences help simplify the callback example we mentioned earlier:
Class UIBuilder {public UIBuilder () {button.addActionListener (e-> / / process ActionEvent e)}}
We download and pass a lambda expression directly into the addActionListener method instead of the previously defined class that holds the callback method. In addition to reducing template code and improving readability, it allows us to directly express what we are interested in: handling events.
Before we learn more about the advantages of lambda expressions, let's take a look at a copy of lambda expressions in Scala.
6. Lambda expression in Scala
In functional programming, functions are the basic building blocks. Scala combines object-oriented programming and functional programming in java. In Scala, an lambda expression is called "function" or "function text". The functions in Scala are first-class citizens. They can be assigned to vals or vars (final variable or non-final variable), they can be used as arguments to other functions, or they can be combined into new functions.
In Scala, the text of a function is written as follows:
(argument) = > / / funtion body
For example, the aforementioned lambda expression used by java to evaluate the length difference between two strings is written in Scala as follows:
(S1: String, S2: String) = > s1.length-s2.length
The function text in Scala is also a closure. It can access variables defined outside the direct lexical scope.
Val outer = 10 val myFuncLiteral = (y: Int) = > y * outer val result = myFuncLiteral (2) > 20
The result of this example is 20.
As you can see, we assigned the function text to the variable myFuncLiteral.
The grammatical and semantic similarities between the lambda expression of java 8 and the function text of Scala are very obvious. They are the same semantically, while the grammatical differences are arrow symbols (java8->, scala = >) and simplified symbols that we did not mention.
That's all for "what's the difference between Lambda expressions in Java8 and Scala?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.