Last week, we talked about the new features of Java8, what is functional programming, what is the Lambda expression, this week let us continue to talk about these new features.
This week, we'll talk about what is the Stream API and what is Optional.
Stream API you let me want to rewrite all my previous code
why would i say that, because the work that might have required one or more of the for loops or for-each loop in the past in now done in one line.
Before collection types for multithreaded operations may need to pay attention to many places, but now you need to turn on a switch, you can make you to the operation of the collection type efficiency many times.
Before you may have to worry about a lot of places before, but now you just need to focus on the logic that you want to implement, and leave the rest to Java8 itself.
Before you may have to explain how it works, but the code is not very readable, and now your code can directly "read" without any comment.
Use Optional to make your application no longer null-pointer exceptions
Optional, which is a generic class that indicates whether the object exists, can use the API to determine if it is empty.
Reference Java8 In Action —"
Using Optional forces you to more actively unquote Optional objects to address the problem of missing variable values, and in the end, you can more effectively prevent unexpected null pointer exceptions in your code.
Using Optional helps you design better apis, and the user only needs to read the method signature to see if the method accepts a value of the Optional type."
That's what I've learned from Java 8 ,Personal opinions are for reference only.
Pere Tang. 0am on July 15, 2017