Reprint: http://geek.csdn.net/news/detail/94219
Before Java 9 was released, let's share some Java 8 development tips that are translated from the Java 8 Top tips of JetBrains Senior development director Trisha Geed.
"The following is the translation"
After years of development using Java 8, combined with personal use of IntelliJ idea, I summarized the following several JAVA8 techniques for your reference.
Optional
Optional is a highly underrated trait that, if applied properly, can actually help reduce the number of nullpointerexceptions (invalid pointers) errors. Here are two key points to use for optional:
- Optional should be used only for return types instead of parameters or fields
If you try to do this, IntelliJ idea will give you the following tips:
- Avoid using the called Get ()
Optional's strength is to express the null value and let you deal with it. Therefore, it is important to check the values before proceeding to the next step. Fortunately IntelliJ idea will also make a hint:
Instead, you can try using OrElse or Orelseget.
Use of lambda expressions
Lambda is another highlight of Java 8 and the following are tips for using lambda:
Simplification is the key to the correct use of lambda expressions. You can simplify them to a single line, or you can refactor more complex expressions in a method.
Because lambda expressions lack type information, the type information is sometimes put into parameters:
This is too lengthy, how to do? I prefer to have a meaningful name for the parameter. For example:
Design of lambda expressions
While we are less likely to create a method with lambda as a parameter, how do we deal with it if there is such a requirement?
- IntelliJ idea helps to implement function-type parameters
- Add @functionalinterface to your functional interface
If you need to customize the functional interface, then IntelliJ idea will be a good watchdog:
Overlay hints:
Duplicate declaration:
defined for a class other than an interface:
Streams (Stream)
The Stream API is a new introduction to Java8, where is the use of it?
- Use the streams API whenever possible for collection recursion
Please compare the following two sections of code:
Using a For loop in an array loop
Using a For loop in an array of metadata types will give you the best performance:
Summary
I'm looking for new ways to try new ideas every day, hoping to keep improving the best practices for personal programming.
Five development tips for Java 8