java 8 functional interfaces and lambda expressions
java 8 functional interfaces and lambda expressions
Alibabacloud.com offers a wide variety of articles about java 8 functional interfaces and lambda expressions, easily find your java 8 functional interfaces and lambda expressions information here online.
New Features of JDK 8-Lambda expressions and new features of JDK 8 lambda
JDK 8 has been released for nearly four years. It seems a little out of date to talk about its new features ". Although JDK8 is no longer "new",
A core concept introduced in Java 8 is the functional interface (functional Interfaces).By adding an abstract method to the interface, these methods can be run directly from the interface.If an interface defines a unique abstract method , then this interface becomes a
other unchanged parts as a method body, then we can save the repeated code. The obviously changed part is listed above, the method for converting Employee e into key, but we know that java cannot pass methods as parameters. However, this is not a problem for a slightly experienced programmer. We can use interfaces to achieve our goal and encounter another problem. The return values of the above three metho
annotation: @FunctionalInterface , not required, is used to indicate that the interface is designed as a functional interface2.4 Use of Lambdapredicate filter out empty strings in the list// 定义一个函数式接口interface PredicateConsumer calculates the square of each element in the list and outputs@FunctionalInterfacepublic interface Consumerfunction** returns the length of each element in the list@FunctionalInterfacepublic interface Function2.5 type Inference
= new Shapefa Ctory ();
Get an object of Circle and call it draw method.
Shape shape1 = Shapefactory.getshape ("CIRCLE");
Call draw method of Circle
Shape1.draw ();
Get an object of Rectangle and call it draw method.
Shape shape2 = Shapefactory.getshape ("RECTANGLE");
Call draw method of Rectangle
Shape2.draw ();
}
Verify output:
Inside Circle::d Raw () method.
Inside Rectangle::d Raw () method.
Factory mode: Refactoring u
overload of using the Update method directly is not the quickest. You can use public int update(String sql, PreparedStatementSetter pss) this overload to get a better run speed:publicvoidcreatethrows IOException { template.update( "insert into item (name, price, prc_date) values (?, ?, now())", new PreparedStatementSetter() { @Override publicvoidsetValuesthrows SQLException { ps.setString(1, item.name); ps.setBigDecimal(2, item.price); } })
to be maintained. Such a design can easily become cumbersome and difficult to maintain, ultimately destroying the expansion of the original intention.
There is also a workaround-functional interfaces, and lambda expressions-that we can use to design scalable policies. Instead of having to create new
while the lambda expression is streamlined into a single line of code, it is necessary to constantly adjust the structure of the code to move toward a more stable and robust direction, the original code logic, although the process is understandable, but ugly and long, as too many tasks.5. Conversion from a set of known types to a collection of another typeThe approximate logic is to obtain the raw material associated with the production product (incl
while the lambda expression is streamlined into a single line of code, it is necessary to constantly adjust the structure of the code to move toward a more stable and robust direction, the original code logic, although the process is understandable, but ugly and long, as too many tasks.5. Conversion from a set of known types to a collection of another typeThe approximate logic is to obtain the raw material associated with the production product (incl
lambda expression can be implicitly assigned to a functional interface, for example, we can create a reference to the Runnable interface through a lambda expression.
Runnable r = (), System.out.println ("Hello World");
When you do not specify a functional interface, the compiler automatically
a line of lambda syntax helps to improve the robustness of your code (the focus here is not on the use of lambda, but I think it's worth recording)The business scenario is to increase the production schedule (plan), a schedule can have multiple products, a product needs one or more ingredients, so the relationship is a pair of many, the following look at my first write code (pseudo-function expression)、Foc
as we've already mentioned in the previous blog, lambda expressions allow for more concise code to create an instance of an interface with only one abstract method. Now let's write a Java command-mode to study the syntax of the lambda expression itself.
here Repeat the command mode:
Consider a scenario where a metho
of the lambda expression (is it an Object ?)
Lambda expressions can be considered as an Object (note the wording ). The type of the lambda expression is called "target type )". The target type of the lambda expression is "function interface", which is a new concept introduc
the definition of anonymous class is an expression, therefore, you can define this class directly in the expression. The example below is a good example.
1 HelloWorld frenchGreeting = new HelloWorld() { 2 String name = "tout le monde"; 3 public void greet() { 4 greetSomeone("tout le monde"); 5 } 6 public void greetSomeone(String someone) { 7 name = someone; 8 System.out.println("Salut " + name); 9 }10 };
T
Lambda expression is the most significant new Java language feature since Java SE 5 introduced generics, this article is an article in the last issue of Java Magazine in 2012, which introduces LAMDBA's design intent, application scenarios, and basic syntax.The lambda express
method is a functional interface (a functional interface may also be shifting to a live multiple default or static method). Since the function interface contains only an abstract method, you can omit the name of the method when implementing the method. So you can use a lambda expression instead of an anonymous inner class expression, called as follows:printPerso
After understanding some of the basic concepts and applications of Java 8 Lambda, we have a question: what is a lambda expression compiled into? This is an interesting question that involves the specific implementation of the JDK. This article introduces the details of OpenJDK's conversion to
Original link: Start Using Java Lambda Expressions Download the sample program Examples.zip.
Introduction (Translator Note: Although it looks very advanced, the essence of a lambda expression is just a "syntactic sugar", which is inferred by the compiler and helps you transform the code packaged as normal, so you c
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.