Java

[Java] Java8 Stream 사용법

nan2 2022. 3. 29. 14:12
반응형

 

1. 생성

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

//방법1
Arrays.stream(list);

//방법2
list.stream();

//방법3
Stream.of("a", "b", "c");

 

 

2. 반환타입

 

- 중간연산 : return Stream;

 

filter(), map(), flatmap(), distinct(), sort(), peek(), limit(), skip() ..

 

 

- 최종연산 : return 특정한 type

 

 

3. collect

 

collect()를 구현한 collectors의 정적 메서드

- toList(), toSet(), toCollection(), toMap()

 

list.stream().collect(Collectors.toList());

list.stream().collect(Collectors.toSet());

list.stream().collect(Collectors.toCollection(HashSet :: new));

list.stream().collect(Collectors.toMap("key", "value"));
반응형