Stream is a convenient API which is introduced in Java 8 for aggregating values of aggregates such as arrays and collections.
In this article, I would like to introduce basic knowledge of Stream.
👽 filter
filter()
returns some elements of this stream that match the given predicate.
List |
🐝 count
count()
method in stream API counts the elements in stream object.
articles.stream().filter(i -> i.isDraft()).count() //=> n |
🏀 noneMatch
noneMatch()
method is a method which takes argument as a Predicate and if none of element of stream matches the given Predicate, then it returns true otherwise false.
articles.stream().noneMatch(i -> i.isDraft()) |
😸 Instream.range
Instream.range(int startInclusive, int endExclusive)
returns a sequential ordered stream elements from startInclusive(inclusive) to endExclusive(exclusive) by an incremental step of 1.
IntStream.range(1, 4).forEach(System.out::println); |
🍄 Stream Optional Value
java.util.Optional
is a wrapper for a non-null value that may or may not contain a non-null value.
final Optional |
To get and use the value from Optional
, write it as follows:
article.ifPresent(value -> System.out.println("Article.id is" + article.getId())); |
ifPresent(Consumer super T> action)
method from the Optional class takes an action on the value contained in Optional object.
🐰 Add original validation
In this section, I would like to introduce how to add an original validation.
Annotation class
Initially, please create an annotation class:
package com.example.validation; |
Validation class
Please add validation class:
package com.example.validation; |
Set validation message
Please define a validation message in Unicode. native2ascii
command can convert UTF-8 to Unicode or ASCII.
native2ascii |
You should add a converted sentence to ValidationMessages.properties
:
com.example.validation.ArticleTitle.message=\u30bf\u30a4\u30c8\u30eb\u306f\u30e6\u30cb\u30fc\u30af\u306b\u3057\u3066\u304f\u3060\u3055\u3044 |
Apply validator
You can apply your original validator to your code:
package com.example.validation; |
😀 Special Thanks
- http://winterbe.com/posts/2014/07/31/java8-stream-tutorial-examples/
- https://terasolunaorg.github.io/guideline/public_review/ArchitectureInDetail/Validation.html#how-to-extend
- http://www.java2s.com/Tutorials/Java/Java_Stream/0035__Java_Optional.htm
- http://www.ne.jp/asahi/hishidama/home/tech/java/stream.html
- http://www.task-notes.com/entry/20150521/1432177200
- https://www.ibm.com/developerworks/jp/java/library/j-java8idioms5/index.html
- https://www.concretepage.com/java/jdk-8/java-8-stream-concat-count-sorted-distinct-example
- https://www.concretepage.com/java/jdk-8/java-8-stream-allmatch-anymatch-nonematch-example
- http://morizyun.github.io/java/spring-framework-validation.html
🖥 Recommended VPS Service
VULTR provides high performance cloud compute environment for you.
Vultr has 15 data-centers strategically placed around the globe, you can use a VPS with 512 MB memory for just $ 2.5 / month ($ 0.004 / hour).
In addition, Vultr is up to 4 times faster than the competition, so please check it => Check Benchmark Results!!