Stream termination

Stream generation process and Stream intermediate process Process what kind of element the Stream will end after being processed in

find system

Used for searching and checking if elements remain

List<String> list = Arrays.asList("a","b","c","d","e");

// findFirst
String a = list.stream().filter(s -> s.equals("a")).findFirst().orElse(null);

// findAny
String any = list.stream().filter(s -> s.equals("a")).findAny().orElse(null);

match system

Determine if there is something that applies to the specified conditional expression

List<String> list = Arrays.asList("alex","benjamin",null,"","elsa");

boolean nonNull = list.stream().allMatch(Objects::nonNull); //false

boolean isEmpty = list.stream().anyMatch(""::equals); //true

boolean isTom = list.stream().noneMatch("tom"::equals); //true

toArray Used when you want to process as an array element Note that if you process with toArray (without arguments), it will become Object [].

List<String> list = Arrays.asList("alex","benjamin","charley","dante","elsa");

Integer[] length = list.stream().map(String::length).toArray(Integer[]::new);

collect Used when you want to process as a Collection element such as List It's easy to use, so I often use it (small average feeling)

List<String> list = Arrays.asList("alex","benjamin","charley","dante","elsa");

List<Integer> length = list.stream().map(String::length).collect(Collectors.toList());

count Aggregate the number of Stream elements that have completed generation processing and intermediate processing

List<String> list = Arrays.asList("alex","benjamin","charley","dante","elsa");

long count = list.stream().filter(s -> s.length() == 4 ).count(); // 2

forEach Internal iterator added in Java 8 In fact, it can also be used for Stream. I think you should use the standard forEach () Both samples below give the same result

Some say that Stream for Each is treated as a contraindication (Because it is a Stream for converting to other elements, but it does not return a value)

List<String> list = Arrays.asList("alex","benjamin","charley","dante","elsa");

// stream().forEach()
list.stream().filter(s -> s.length() == 4 ).forEach(System.out::println);

// list.forEach()
list.forEach(s -> {{if (s.length() == 4)System.out.println(s);}});

min,max While editing

Recommended Posts

Stream termination
Java Stream termination
[Java] Stream API --Stream termination processing
Stream play
Memo Stream
Stream API memo
[JAVA] Stream type
Try Java 8 Stream
Java Stream API
Stream generation process
Studying Java 8 (Stream)
Stream intermediate processing
[Java] Stream processing
Stream API basics
Java 9 Optional :: stream