Java 8 begins to emerge with a new feature: Functional Programming using LAMBDA expressions (JSR-335). What we're going to talk about today is a subset of the LAMBDA: the virtual extension method, also known as the Public Defender (defender) approach. This feature allows you to provide a default implementation of the m
Lambda expression in Java 8
Lambda expression is a new feature of Java 8
Lambda expressions are primarily used to support the use of code blocks as method parameters, allowing for the creation of instances of functional
do you judge the type of a lambda expression? Let's take a look at the code to screen age-appropriate military personnel:
P-> p.getgender () = = Person.Sex.MALE
p.getage () >=
p.getage ()
This code is used in two places:
public static void Printpersons (Listpublic void Printpersonswithpredicate (list
When calling the Printpersons method, this method expects an argument of the Checkperson type, at which point t
collection interface returns true only if the incoming object does exist in the collection object. But we can't interfere with the functionality of the method, for example, if you use a different case scheme to think that the string you are looking for exists in the collection object, we want the contains () method to return true at this point.
To put it simply, what we want to do is "pass our own new code into" the existing method, and then call the
functional interface.See, Lambda and functional interfaces appear, and the function interface itself is for lambda service.So, what is a functional interface? It is simply that an
The lambda expression for Java 1. What is a lambda expressionSimply put, a lambda expression is an anonymous method. Lambda expressions allow programmers to use more concise code, but they also make the code less readable.A lambda
)); Stream
stream = names.stream(); stream.forEach(System.out::println); }}
Is it concise.
The following describes the Lambda expressions in Java 8 in detail.(String a, String B)-> a. compareTo (B) is a Lambda expression. The parameter list of the function is enclosed in brackets, and the-> symbol is followed by the function body.
function resulting in its multiple interface functions need to be implemented, into a "non-function interface", we can add a declaration @functionalinterface, so that others will not be added to the inside of the new interface function.@FunctionalInterfacepublicinterface MethodInterface { voiddoSomething();}
A fu
final all interface methods are essentially public.
JDK 8 controversially brings the advantages of abstract classes to the interface. The effect is that today there are a large number of abstract classes that may be replaced with interfaces by default methods. 2.:: Method Reference
A method reference provides a very useful syntax for directly referencing a method or constructor of an existing
collection interface returns true only if the incoming object does exist in the collection object. But we cannot intervene in the function of this method, for example, if a different casing scheme can be used to think that the string being looked up exists in this collection object, we hope that the contains () method will return true at this point.To put it simply, what we'd like to do is "pass our own new code into" the existing method, and then ca
a tool that uses functional programming to perform complex operations on a collection class.II) common flow operationsLazy Evaluation Method: A method that describes steam only and ultimately does not produce a new collection.Early Evaluation method: The method that will eventually generate the value from steam.1.collect (ToList ())This method generates a list of steam values and is an early evaluation operation.// The Stream.of method generates a St
(ActionEvent e) { System.out.println ("Without lambda expression is boring"); } });/ /Java 8 Way:show.addActionListener ((e), { System.out.println ("Action!! Lambda Expressions Rocks ");});In Java 8, you can use a lambda expression to replace an ugly
Lambda best practices in Java 8 (1)
Java 8 has been launched for some time. More and more developers choose to upgrade JDK. The hot news shows that JDK 7 is the most popular, followed by JDK 6 and 8. This is a good thing!
In 8, Lambda is the most popular topic, not only because of changes in syntax, but more important
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
I've tried to incorporate lambda expressions into my code, and the following code example is the result of this attempt. For those Java programmers who don't know a lambda expression at all, I strongly recommend browsing through this article before continuing with the reading.Ok, now that you're familiar with lambda ex
JDK 8 to the LAMBDA function programming support, shallow is simply to introduce some new grammatical structure, is following JDK5 introduction of generics after another one to everyone encoding way of an innovation, if you do not follow, I am afraid, over time, you will think that Java code is the Martian language. Deep down, Java is parallel processing in a lan
This series of articles translates from functional programming in Java Venkat Subramaniam
Chapter II: The use of collections
We often use a variety of collections, numbers, strings, and objects. They are everywhere, even if the code to operate a set can be slightly optimized, it can make the code a lot clearer. In this chapter, we explore how to use lambda
). Start (); String msg = "Hello"; runnable = (), {System.out.println (msg); System.out.println ("Run 1"); System.out.println ("Run 2"); }; New Thread (runnable). Start (); consumerpreviously, the parameters of methods in Java were mostly special anonymous class objects (there was only one abstract method in the interface-
Introduction of functional interfaces to Java 8Adding lambda calculus to Java 8 is an exciting new feature. Although this feature comes too late, the current mainstream development languages, JAVA seems to be the last language that supports
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.