Now that I'm using Java 8 in the field, I'm using streams and lambda expressions. I will write what I can write without checking (will be updated at any time)
Thank you for your advice. 2019/07/19: Changed title (added stream) 2019/07/19: Changed the description of IntStream to ** stream ** with int type element
If there is a reference site, I will add it. Qiita --Introduction to Java Stream API
Use the function interface. Generate a stream. It can be used from Java 8. Process in the order of generation operation → intermediate operation → termination operation.
If you can understand it, increase it.
stream() Create a Stream. The sample creates a Stream from the list.
List<String> list = Arrays.asList("hoge","fuga","poyo");
list.stream()
IntStream.range(startInclusive, endExclusive) Create a stream with an int type element. ʻEnd Exclusive` is not included. Generate 0-9 IntStream.
IntStream.range(0, 10)
IntStream.rangeClosed(startInclusive, endInclusive) Create a stream with an int type element. Includes this ʻend Inclusive`. Generate 1 to 10 IntStream.
IntStream.rangeClosed(1,10)
If you can understand it, increase it.
filter Create a new stream of only the elements that match the conditions. Generate even-numbered streams.
IntStream.range(0,10).filter(i -> i % 2 == 0)
mapToObj Convert to Object. The sample converts ~ Integer ~ int to String.
IntStream.range(0,10).mapToObj(i -> String.valueOf(i))
forEach Loop processing. No return value.
IntStream.range(0,10).mapToObj(i -> String.valueOf(i)).forEach(System.out::println);
collect You can create lists and maps. The sample creates a List.
List<String> list = IntStream.range(0,10).mapToObj(i -> String.valueOf(i)).collect(Collectors.toList());
toArray Create an array.
String[] array = IntStream.range(0,10).mapToObj(i -> String.valueOf(i)).toArray(String[]::new);
count Returns the number of elements.
long count = IntStream.range(0,10).mapToObj(i -> String.valueOf(i)).count();
I will update it from time to time.
Recommended Posts