1. 생성: 스트림 객체 생성
Map<Integer, String> map = new HashMap<>();
map.put(1, "a");
map.put(2, "d");
map.put(3, "z");
map.put(4, "g");
map.put(5, "e");
map.put(6, "e");
map.put(7, "r");
map.put(8, "z");
map.put(9, "a");
map.put(10, "h");
map.put(11, "o");
map.put(12, "m");
Stream<String> stream = map.values().stream();
2. 중간 연산: 연산 결과가 스트림. 반복 가능.
stream.distinct().forEach(System.out::print); //adzgerhom
stream.sorted().forEach(System.out::print); //aadeeghmorzz
stream.limit(4).forEach(System.out::print); //adzg
stream.filter(a -> a.equals("a")).forEach(System.out::print); //aa
3. 최종 연산: 연산 결과가 스트림이 아닌 연산. 한번만 가능.
stream.forEach(System.out::print);
long result = stream.limit(5).count();
System.out.println(result); //5
Optional<String> result = stream.limit(5).findAny();
System.out.println(result.get());
Optional<String> result = stream.limit(5).findFirst();
System.out.println(result.get()); //a