Lambda best practices in Java 8 (1)

Source: Internet
Author: User
Tags stream api

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 importantly, it brings the idea of functional programming. I think it is a good programmer, it is necessary to learn the idea of functional programming to broaden your mind. So this article will talk about Lambda's application scenarios, performance, and the negative side.
Why does Java require Lambda?

In January 1996, Java 1.0 was released. Since then, the computer programming field has undergone earth-shaking changes. Business development requires more complex applications, and most programs run on more powerful machines equipped with multi-core CPUs. The emergence of Java Virtual Machine JVM with efficient runtime compilers enables programmers to focus more on coding clean and easy to maintain code, instead of thinking about how to use every CPU clock and every byte of memory.

The emergence of multi-core CPU becomes "the elephant in the room", which cannot be ignored, but no one is willing to face up to it. The introduction of locks in algorithms is not only prone to errors, but also consumes time. Java. util. concurrent packages and many third-party class libraries have been developed to abstract concurrency and help programmers write programs that run well on multi-core CPUs. Unfortunately, we have not gone far enough until now.

When developers of those class libraries use Java, they find that the abstract level is not enough. Processing Big Data is a good example. In the face of big data, Java still lacks efficient parallel operations. Java 8 allows developers to write complex collection processing algorithms. By simply modifying a method, code can run efficiently on a multi-core CPU. To compile a class library for parallel processing of these big data, you need to modify the existing Java language: Add a lambda expression.

Of course, there is a price to do this. programmers must learn how to write and read code containing lambda expressions. However, this is not a financial loss. Compared with writing a large segment of complex, thread-safe code, it is much easier to learn a little new syntax and some new habits. When developing enterprise-level applications, good class libraries and frameworks greatly reduce development time and costs, and clear the obstacles to easy-to-use and efficient class libraries.

If you have not touched the Lambda syntax, you can refer to it here.
Lambda application scenarios

It is necessary for you to learn the concept of functional programming, such as a preliminary study of functional programming. However, I will focus on the practicality of functional programming, this includes technologies that can be understood and used by most programmers. We are concerned about how to write code well, rather than code that conforms to the functional programming style.

1. Replace the Anonymous class with ()-> {}

Now Runnable threads, Swing, JavaFX event listener code, etc. In java 8, you can use Lambda expressions to replace ugly anonymous classes.

 
 
  1. //Before Java 8: 
  2. new Thread(new Runnable() { 
  3. @Override 
  4. public void run() { 
  5. System.out.println("Before Java8 "); 
  6. }).start(); 
  7.  
  8. //Java 8 way: 
  9. new Thread(() -> System.out.println("In Java8!")); 
  10.  
  11. // Before Java 8: 
  12. JButton show = new JButton("Show"); 
  13. show.addActionListener(new ActionListener() { 
  14. @Override 
  15. public void actionPerformed(ActionEvent e) { 
  16. System.out.println("without lambda expression is boring"); 
  17. }); 
  18.  
  19.  
  20. // Java 8 way: 
  21. show.addActionListener((e) -> { 
  22. System.out.println("Action !! Lambda expressions Rocks"); 
  23. }); 


2. Replace the External Loop with an inner loop

External Loop: describes how to do it. More than two for loops nested in the Code are more difficult to understand. Only List elements can be processed in sequence;

Inner Loop: describes what to do, not how to do it; it is not necessary to process the elements in the List in sequence

 
 
  1. //Prior Java 8 : 
  2. List features = Arrays.asList("Lambdas", "Default Method", 
  3. "Stream API", "Date and Time API"); 
  4. for (String feature : features) { 
  5. System.out.println(feature); 
  6.  
  7. //In Java 8: 
  8. List features = Arrays.asList("Lambdas", "Default Method", "Stream API", 
  9. "Date and Time API"); 
  10. features.forEach(n -> System.out.println(n)); 
  11.  
  12. // Even better use Method reference feature of Java 8 
  13. // method reference is denoted by :: (double colon) operator 
  14. // looks similar to score resolution operator of C++ 
  15. features.forEach(System.out::println); 
  16.  
  17. Output: 
  18. Lambdas 
  19. Default Method 
  20. Stream API 
  21. Date and Time API 


3. Support for function Programming

To support function programming, Java 8 adds a new package of java. util. function, where java. util. function. Predicate is an interface that supports Lambda function programming:

 
 
  1. public static void main(args[]){ 
  2. List languages = Arrays.asList("Java", "Scala", "C++", "Haskell", "Lisp"); 
  3.  
  4. System.out.println("Languages which starts with J :"); 
  5. filter(languages, (str)->str.startsWith("J")); 
  6.  
  7. System.out.println("Languages which ends with a "); 
  8. filter(languages, (str)->str.endsWith("a")); 
  9.  
  10. System.out.println("Print all languages :"); 
  11. filter(languages, (str)->true); 
  12.  
  13. System.out.println("Print no language : "); 
  14. filter(languages, (str)->false); 
  15.  
  16. System.out.println("Print language whose length greater than 4:"); 
  17. filter(languages, (str)->str.length() > 4); 
  18.  
  19. public static void filter(List names, Predicate condition) { 
  20. names.stream().filter((name) -> (condition.test(name))) 
  21. .forEach((name) -> {System.out.println(name + " "); 
  22. }); 
  23.  
  24. Output: 
  25. Languages which starts with J : 
  26. Java 
  27. Languages which ends with a 
  28. Java 
  29. Scala 
  30. Print all languages : 
  31. Java 
  32. Scala 
  33. C++ 
  34. Haskell 
  35. Lisp 
  36. Print no language : 
  37. Print language whose length greater than 4: 
  38. Scala 
  39. Haskell 


4. Processing Data? MPs queue is more concise

The Stream API added in Java 8 makes data processing in the set more convenient, with higher performance and better readability.

Assume a business scenario: perform a discount on goods of more than 20 yuan, and finally get the discount price for these goods.

 
 
  1. final BigDecimal totalOfDiscountedPrices = prices.stream() 
  2. .filter(price -> price.compareTo(BigDecimal.valueOf(20)) > 0) 
  3. .map(price -> price.multiply(BigDecimal.valueOf(0.9))) 
  4. .reduce(BigDecimal.ZERO,BigDecimal::add); 
  5.  
  6. System.out.println("Total of discounted prices: " + totalOfDiscountedPrices); 

Imagine how many lines are required if Object-oriented Data is used? How many cycles? How many intermediate variables need to be declared?

For more information about Stream APIs, see my previous articles.


Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.