1. Why do we use the stream ?
- You don’t need to think about how to build.
It’s like the Query. How do we get the max value before the Java8, We always use the ‘for’ keyword or ‘if’ keyword for getting that data. And We should consider the logic for getting the result of calculating like the max value. In other word, If you don’t use the Stream then you always think about how we build the logic for getting some data. But if you use this, you don’t need to think about the focus of the building.
- You can get the advantage of the parallel.
Previus the Java8, Parallel coding is too difficult to a lot of the Java developers. You should use the ‘Thread’ technic and always consider about the ‘Mutual Exclusion’ It’s too low technic to Application Developers. So If you use the Stream then you can get the parallel advantage without Thread, The thinking about the Locking.
- It’s more readable than previous.
You just concentrate about the think you want using the stream like the query. so consequently, the source code is more shorter that previous and more intuitive.
2. Stream Manipulation Computation
It’s the summary of the use of stream basically.
- filter()
‘filter()’ function is for seperating the elements of the stream utilzing any function returning boolean value.
1 |
|
- takewhile()
Do you know the break keyword for the Java ? when you want to finish some iteration status, You can use this keyword. In the filter() function, It’s always iterate everything elements although you got the value you want. If you want to stop the iteration when you complete your mission then use this keyword.
1 |
|
- dropwhile()
It’s other side of the takewhile() function. If you want to get the large number then use this keyword.
1 |
|
- distinct()
If you don’t want to get duplicate value like the Set then use this.
1 |
|
- limit()
You can limit the number of the result using this function
1 |
|
- skip()
If you want to skip the elements coming first, then use this function.
1 |
|
- map()
If you want translate the type of the stream. then use this function.
1 | List<Dish> menu; |
The type of the menu is the List
3. Stream Search Computation
- allMatch()
It return the boolean value. It checkes what the all of the elements pass the condition off or not.
1 |
|
- anyMatch()
If the elements pass the condition at least 1 thing, then It will return true.
1 |
|
- nonMatch()
Everyting should be fail about the condition. if you want get the true.
1 |
|
- findFirst()
If you want to get the first element of the result, then use this.
1 | List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); |
This function allow to get the null value basically because of empty list source. So It’s return the type Optional thing. but If you sure that don’t have the null value, You can get the result without Optional using the get() function.
- findAny()
If you want to get element randomly from the result, then use this.
1 |
|
4. Primitive Specialized Stream
The stream offer the privitive specialized stream like IntStream, LongStream, DoubleStream. I think you already know the terms of Boxing, Unboxking. It’s the translating between the primitive type and the Object type. Actually You should pay really high cost when it occurred. So Java8 offer us primitive specialized stream to avoid this high cost.
1 |
|
5. The various way to build the stream
- Stream.of()
1 |
|
- Arrays.stream()
1 |
|
- Stream.iterate()
1 |
|
- Stream.generate()
1 |
|