์ƒˆ์†Œ์‹

Back-End/Java

[Java 8] Stream API ๊ธฐ๋ณธ ๋ฌธ๋ฒ• ๋ฐ ํ™œ์šฉ ์˜ˆ์ œ

  • -

 

 

 

 

 

Java 8์˜ Stream API๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ณ  ๋ณ€ํ™˜ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ•๋ ฅํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. ์ด API๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋ฐฐ์—ด, ๋ฆฌ์ŠคํŠธ, ๋งต ๋“ฑ์˜ ์ปฌ๋ ‰์…˜์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”์ถœํ•˜๊ณ  ํ•„ํ„ฐ๋ง, ๋งคํ•‘, ์ง‘๊ณ„ ๋“ฑ ๋‹ค์–‘ํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ์—๋Š” Stream API์˜ ๊ธฐ๋ณธ ๋ฌธ๋ฒ•๊ณผ ํ™œ์šฉ ์˜ˆ์ œ๋ฅผ ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

 


#1. Stream ์ƒ์„ฑ

Stream์€ Collection, List, Set, Array ๋“ฑ์˜ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. Stream์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

#1. 1. Collection์œผ๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ

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

 

#1. 2.  Array๋กœ๋ถ€ํ„ฐ ์ƒ์„ฑ

String[] arr = {"a", "b", "c"};
Stream<String> stream = Arrays.stream(arr);

 

#1. 3. Stream.of() ์ด์šฉ

Stream<String> stream = Stream.of("a", "b", "c");

 

 

 


#2. Stream ์ค‘๊ฐ„ ์—ฐ์‚ฐ

Stream์— ๋Œ€ํ•ด์„œ ์ค‘๊ฐ„ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ค‘๊ฐ„ ์—ฐ์‚ฐ์€ Stream์„ ๋ฐ˜ํ™˜ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์—ฌ๋Ÿฌ ๊ฐœ์˜ ์ค‘๊ฐ„ ์—ฐ์‚ฐ์„ ์—ฐ๊ฒฐํ•˜์—ฌ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

#2. 1. filter()

์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ๋งŒ์„ ์„ ํƒํ•˜์—ฌ Stream์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<String> stream = Stream.of("apple", "banana", "cherry", "durian");
Stream<String> filteredStream = stream.filter(str -> str.length() > 5);

 

#2. 2. map()

๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ๋งคํ•‘ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด Stream์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<String> stream = Stream.of("apple", "banana", "cherry");
Stream<Integer> mappedStream = stream.map(str -> str.length());

 

#2. 3. flatMap()

๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ๋งคํ•‘ ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด Stream์„ ์ƒ์„ฑํ•˜๊ณ , ์ด๋ฅผ ํ•˜๋‚˜์˜ Stream์œผ๋กœ ๋ณ‘ํ•ฉํ•˜์—ฌ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<List<Integer>> stream = Stream.of(Arrays.asList(1, 2), Arrays.asList(3, 4));
Stream<Integer> flatMappedStream = stream.flatMap(list -> list.stream());

 

#2. 4. sorted()

์š”์†Œ๋ฅผ ์ •๋ ฌํ•˜์—ฌ Stream์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<String> stream = Stream.of("banana", "apple", "durian", "cherry");
Stream<String> sortedStream = stream.sorted();

 

#2. 5. distinct()

์ค‘๋ณต๋œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•œ Stream์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<String> stream = Stream.of("apple", "banana", "apple", "cherry");
Stream<String> distinctStream = stream.distinct();

 

#2. 6. limit()

์ฒ˜๋ฆฌํ•  ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ์ œํ•œํ•˜์—ฌ Stream์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<String> stream = Stream.of("apple", "banana", "cherry", "durian");
Stream<String> limitedStream = stream.limit(2);

 

#2. 7. skip()

์ฒ˜๋ฆฌํ•  ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ณ , ๋‚˜๋จธ์ง€ ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋œ Stream์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<String> stream = Stream.of("apple", "banana", "cherry", "durian");
Stream<String> skippedStream = stream.skip(2);

 

 

 


#3. Stream ์ตœ์ข… ์—ฐ์‚ฐ

Stream์— ๋Œ€ํ•ด์„œ ์ตœ์ข… ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ตœ์ข… ์—ฐ์‚ฐ์€ Stream์„ ๋ฐ˜ํ™˜ํ•˜์ง€ ์•Š๊ณ , ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

#3. 1. forEach()

๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

Stream<String> stream = Stream.of("apple", "banana", "cherry", "durian");
stream.forEach(str -> System.out.println(str));

 

#3. 2. toArray()

Stream์˜ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<String> stream = Stream.of("apple", "banana", "cherry", "durian");
String[] arr = stream.toArray(String[]::new);

 

#3. 3. reduce()

Stream์˜ ์š”์†Œ๋ฅผ ํ•˜๋‚˜๋กœ ์ค„์—ฌ ํ•˜๋‚˜์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5);
int sum = stream.reduce(0, (acc, val) -> acc + val);

 

#3. 4. collect()

Stream์˜ ์š”์†Œ๋ฅผ ์ˆ˜์ง‘ํ•˜์—ฌ ์ƒˆ๋กœ์šด Collection, List, Set ๋“ฑ์œผ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<String> stream = Stream.of("apple", "banana", "cherry", "durian");
List<String> list = stream.collect(Collectors.toList());

 

#3. 5. count()

Stream์˜ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<String> stream = Stream.of("apple", "banana", "cherry", "durian");
long count = stream.count();

 

#3. 6. anyMatch()

ํ•˜๋‚˜ ์ด์ƒ์˜ ์š”์†Œ๊ฐ€ ์กฐ๊ฑด์— ๋งž๋Š”์ง€ ๊ฒ€์‚ฌํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<String> stream = Stream.of("apple", "banana", "cherry", "durian");
boolean result = stream.anyMatch(str -> str.startsWith("a"));

 

#3. 7. allMatch()

๋ชจ๋“  ์š”์†Œ๊ฐ€ ์กฐ๊ฑด์— ๋งž๋Š”์ง€ ๊ฒ€์‚ฌํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<String> stream = Stream.of("apple", "banana", "cherry", "durian");
boolean result = stream.allMatch(str -> str.length() > 3);

 

#3. 8. noneMatch()

๋ชจ๋“  ์š”์†Œ๊ฐ€ ์กฐ๊ฑด์— ๋งž์ง€ ์•Š๋Š”์ง€ ๊ฒ€์‚ฌํ•˜์—ฌ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

Stream<String> stream = Stream.of("apple", "banana", "cherry", "durian");
boolean result = stream.noneMatch(str -> str.startsWith("z"));
300x250

 

 


#4. ํ™œ์šฉ ์˜ˆ์ œ

 

์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ ์ฐพ๊ธฐ

List<String> list = Arrays.asList("apple", "banana", "cherry");
Optional<String> result = list.stream().filter(s -> s.startsWith("a")).findFirst();
if (result.isPresent()) {
    System.out.println(result.get());
}

filter() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ๋ฅผ ์ฐพ๊ณ  findFirst() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฒซ ๋ฒˆ์งธ ์š”์†Œ๋ฅผ ์ฐพ์Šต๋‹ˆ๋‹ค. findFirst() ๋ฉ”์„œ๋“œ๋Š” Optional ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ, isPresent() ๋ฉ”์„œ๋“œ๋กœ ๊ฒฐ๊ณผ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•œ ํ›„, get() ๋ฉ”์„œ๋“œ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๊ฐ€์ ธ์˜ต๋‹ˆ๋‹ค.

 

 

์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ ์ˆ˜์ง‘ํ•˜๊ธฐ

List<String> list = Arrays.asList("apple", "banana", "cherry");
List<String> result = list.stream().filter(s -> s.startsWith("a")).collect(Collectors.toList());
System.out.println(result);

collect() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์—์„œ ์ถ”์ถœํ•œ ์š”์†Œ๋ฅผ ์ปฌ๋ ‰์…˜์œผ๋กœ ์ˆ˜์ง‘ํ•ฉ๋‹ˆ๋‹ค. Collectors.toList() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ๋กœ ์ˆ˜์ง‘ํ•ฉ๋‹ˆ๋‹ค.

 

 

์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ ์ง‘๊ณ„ํ•˜๊ธฐ

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
int sum = numbers.stream().reduce(0, Integer::sum);
System.out.println(sum);

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int sum = list.stream().reduce(0, (a, b) -> a + b);
System.out.println(sum);

**reduce()**๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ๋ฅผ ์—ฐ์‚ฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” reduce() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ **list**์ŠคํŠธ๋ฆผ์—์„œ ๋ชจ๋“  ์š”์†Œ์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•œ ํ›„, ํ•ด๋‹น ํ•ฉ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค. ์ด๋•Œ, **reduce()**๋ฉ”์„œ๋“œ์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” ์ดˆ๊ธฐ๊ฐ’์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ, ๋‘ ๋ฒˆ์งธ ์ธ์ž๋Š” ๊ฐ ์š”์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋žŒ๋‹ค์‹์ž…๋‹ˆ๋‹ค.

 

 

์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ ๊ทธ๋ฃนํ™”ํ•˜๊ธฐ

List<Person> people = Arrays.asList(
    new Person("John", 25),
    new Person("Jane", 30),
    new Person("Bob", 25),
    new Person("Alice", 30)
);
Map<Integer, List<Person>> result = people.stream().collect(Collectors.groupingBy(Person::getAge));
System.out.println(result);

groupingBy() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์†Œ๋ฅผ ๊ทธ๋ฃนํ™”ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด ๋ฉ”์„œ๋“œ๋Š” ๊ทธ๋ฃนํ™” ๊ธฐ์ค€์ด ๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” Person ํด๋ž˜์Šค์—์„œ ๋‚˜์ด๋ฅผ ๊ฐ€์ ธ์˜ค๋Š” getAge() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ทธ๋ฃนํ™”๋ฅผ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. groupingBy() ๋ฉ”์„œ๋“œ๋Š” Map ์ธ์Šคํ„ด์Šค๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ, ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•˜๋ ค๋ฉด System.out.println() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

 

 

๋ณ‘๋ ฌ ์ฒ˜๋ฆฌํ•˜๊ธฐ

List<String> list = Arrays.asList("apple", "banana", "cherry", "date", "eggfruit");
long count = list.parallelStream().filter(s -> s.startsWith("a")).count();
System.out.println(count);

parallelStream() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์„ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๋Œ€์šฉ๋Ÿ‰ ๋ฐ์ดํ„ฐ๋ฅผ ๋น ๋ฅด๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” parallelStream() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์„ ๋ณ‘๋ ฌ ์ฒ˜๋ฆฌํ•˜๊ณ , filter() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ "a"๋กœ ์‹œ์ž‘ํ•˜๋Š” ์š”์†Œ๋ฅผ ์ถ”์ถœํ•œ ํ›„, count() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์š”์†Œ์˜ ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•ฉ๋‹ˆ๋‹ค.

์ด์™ธ์—๋„ Stream API์—์„œ๋Š” sorted(), distinct(), skip(), limit() ๋“ฑ ๋‹ค์–‘ํ•œ ์—ฐ์‚ฐ์ž๊ฐ€ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ์ด๋Ÿฌํ•œ ์—ฐ์‚ฐ์ž๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ๋ฅผ ์‰ฝ๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

 

Optional ํ™œ์šฉํ•˜๊ธฐ

Optional<String> optional = Optional.of("Hello");
if (optional.isPresent()) {
    String result = optional.get();
    System.out.println(result);
} else {
    System.out.println("No value");
}

Optional ํด๋ž˜์Šค๋Š” null-safeํ•œ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” Optional ํด๋ž˜์Šค์˜ of() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ "Hello" ๋ฌธ์ž์—ด์„ ๊ฐ์‹ผ Optional ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  isPresent() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ๋น„์–ด ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ฉ๋‹ˆ๋‹ค. ๊ฐ์ฒด๊ฐ€ ๋น„์–ด ์žˆ์ง€ ์•Š์œผ๋ฉด get() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ๊ฐ’์„ ๊ฐ€์ ธ์™€์„œ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

 

Stream์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ

List<String> list = Arrays.asList("apple", "banana", "cherry");
String[] array = list.stream().toArray(String[]::new);
System.out.println(Arrays.toString(array));

toArray() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” String ํƒ€์ž…์„ ์š”์†Œ๋กœ ๊ฐ–๋Š” List ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„, stream() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์œผ๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  toArray() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์„ String ํƒ€์ž…์˜ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

 

 

์ŠคํŠธ๋ฆผ์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
list.forEach(System.out::println);

forEach() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” Integer ํƒ€์ž…์„ ์š”์†Œ๋กœ ๊ฐ–๋Š” List ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ ํ›„, forEach() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์š”์†Œ๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

 

Stream์—์„œ ๊ฐ’ ์ถ”์ถœํ•˜๊ธฐ

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int sum = list.stream().mapToInt(Integer::intValue).sum();
System.out.println(sum);

์ŠคํŠธ๋ฆผ์—์„œ ๊ฐ’์„ ์ถ”์ถœํ•  ๋•Œ๋Š” mapToInt(), mapToLong(), **mapToDouble()**๊ณผ ๊ฐ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ํ•œ ํ›„์— ์ถ”์ถœํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” mapToInt() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ Integer ํƒ€์ž…์„ int ํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜ํ•œ ํ›„์—, sum() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชจ๋“  ์š”์†Œ์˜ ํ•ฉ์„ ๊ตฌํ•ฉ๋‹ˆ๋‹ค.

 

 

Stream์—์„œ ์ค‘๋ณต ์š”์†Œ ์ œ๊ฑฐํ•˜๊ธฐ

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 2, 4);
List<Integer> result = list.stream().distinct().collect(Collectors.toList());
System.out.println(result);

distinct() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์—์„œ ์ค‘๋ณต ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” distinct() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๋ณต๋œ **2**์™€ 4 ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•œ ํ›„, toList() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒˆ๋กœ์šด List ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

 

Stream์—์„œ ์š”์†Œ๋ฅผ ๋ถ„ํ• ํ•˜๊ธฐ

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
Map<Boolean, List<Integer>> partitioned = list.stream().collect(Collectors.partitioningBy(i -> i % 2 == 0));
System.out.println(partitioned);

partitioningBy() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ๋ฅผ ๋ถ„ํ• ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” **partitioned**๋ผ๋Š” Map ๊ฐ์ฒด์— 2๋กœ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์š”์†Œ์™€ ์•„๋‹Œ ์š”์†Œ๋ฅผ ๋ถ„ํ• ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•ฉ๋‹ˆ๋‹ค.

 

 

Stream์—์„œ ์š”์†Œ๋ฅผ ์ •๋ ฌํ•˜๊ธฐ

List<Integer> list = Arrays.asList(5, 3, 2, 1, 4);
List<Integer> sorted = list.stream().sorted().collect(Collectors.toList());
System.out.println(sorted);

sorted() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ๋ฅผ ์ •๋ ฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” sorted() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์ŠคํŠธ๋ฆผ์„ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ ํ›„, toList() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒˆ๋กœ์šด List ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

 

Stream์—์„œ ์š”์†Œ๋ฅผ ์ œํ•œํ•˜๊ธฐ

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> limited = list.stream().limit(3).collect(Collectors.toList());
System.out.println(limited);

limit() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ๋ฅผ ์ œํ•œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” limit() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์ŠคํŠธ๋ฆผ์—์„œ ์ตœ๋Œ€ 3๊ฐœ์˜ ์š”์†Œ๋งŒ์„ ์ถ”์ถœํ•œ ํ›„, toList() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒˆ๋กœ์šด List ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

 

Stream์—์„œ ์š”์†Œ๋ฅผ ๊ฑด๋„ˆ๋›ฐ๊ธฐ

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> skipped = list.stream().skip(3).collect(Collectors.toList());
System.out.println(skipped);

skip() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ๋ฅผ ๊ฑด๋„ˆ๋›ธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” skip() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์ŠคํŠธ๋ฆผ์—์„œ ์ฒ˜์Œ 3๊ฐœ์˜ ์š”์†Œ๋ฅผ ๊ฑด๋„ˆ๋›ด ํ›„, ๋‚˜๋จธ์ง€ ์š”์†Œ๋ฅผ ์ถ”์ถœํ•œ ํ›„ **toList()**๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒˆ๋กœ์šด **List**๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

 

Stream์—์„œ ์š”์†Œ๋ฅผ ๋งคํ•‘ํ•˜๊ธฐ

List<String> words = Arrays.asList("hello", "world");
List<String> uniqueCharacters = words.stream().flatMap(s -> Arrays.stream(s.split("")))
                                                .distinct()
                                                .collect(Collectors.toList());
System.out.println(uniqueCharacters);

flatMap() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ๋ฅผ ๋งคํ•‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” flatMap() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ words ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ ๋ฌธ์ž์—ด์„ ๋ฐฐ์—ด๋กœ ๋ถ„ํ• ํ•˜๊ณ , ๊ฐ ๋ฌธ์ž๋ฅผ ๊ฐœ๋ณ„ ์š”์†Œ๋กœ ๋งŒ๋“  ํ›„ ํ•ด๋‹น ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒˆ๋กœ์šด ์ŠคํŠธ๋ฆผ์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„ distinct() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ค‘๋ณต ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•œ ํ›„, toList() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•ด๋‹น ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒˆ๋กœ์šด List ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

 

Stream์—์„œ ์š”์†Œ๋ฅผ ์ง‘๊ณ„ํ•˜๊ธฐ

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
IntSummaryStatistics stats = list.stream().mapToInt((x) -> x).summaryStatistics();
System.out.println(stats);

summaryStatistics() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ๋ฅผ ์ง‘๊ณ„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” summaryStatistics() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ list ์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ์˜ ํ•ฉ, ํ‰๊ท , ์ตœ๋Œ€๊ฐ’, ์ตœ์†Œ๊ฐ’, ๊ฐœ์ˆ˜ ๋“ฑ์„ ๊ณ„์‚ฐํ•œ ํ›„, ํ•ด๋‹น ๊ฐ’์„ ํฌํ•จํ•˜๋Š” ์ƒˆ๋กœ์šด IntSummaryStatistics ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค. ์ดํ›„, ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

 

Stream์—์„œ ์š”์†Œ๋ฅผ ์นด์šดํŠธํ•˜๊ธฐ

List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
long count = list.stream().count();
System.out.println(count);

count() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ๋ฅผ ์นด์šดํŠธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” count() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ list ์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ณ„์‚ฐํ•œ ํ›„, ํ•ด๋‹น ๊ฐœ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

 

 

Stream์—์„œ ์š”์†Œ๋ฅผ ๋ณ€ํ™˜ํ•˜๊ธฐ

List<String> list = Arrays.asList("apple", "banana", "cherry", "date", "elderberry", "fig");
List<String> transformedList = list.stream().map(s -> s.toUpperCase()).collect(Collectors.toList());
System.out.println(transformedList);

map() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ŠคํŠธ๋ฆผ์—์„œ ์š”์†Œ๋ฅผ ๋ณ€ํ™˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์œ„ ์˜ˆ์ œ์—์„œ๋Š” map() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ list ์ŠคํŠธ๋ฆผ์—์„œ ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜ํ•œ ํ›„, ํ•ด๋‹น ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒˆ๋กœ์šด List ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

 

 


๋งˆ์น˜๋ฉฐ..

Java Stream API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ๋ฅผ ๋”์šฑ ์‰ฝ๊ณ  ๊ฐ„๊ฒฐํ•˜๊ฒŒ ์ฒ˜๋ฆฌ๋˜๋Š” ๋‚ด์šฉ์„ ์ •๋ฆฌํ•ด ๋ดค์Šต๋‹ˆ๋‹ค. Stream์˜ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ํ™œ์šฉํ•˜์—ฌ ์•ž์œผ๋กœ๋Š” ๋” ๋น ๋ฅด๊ณ  ํ’ˆ์งˆ ์ข‹์€ ์†Œ์Šค์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•˜๋Š” ์ข‹์€ ๊ฐœ๋ฐœ์ž๊ฐ€ ๋˜๋„๋ก ํ•œ ๋ฐœ์ž๊ตญ ๋” ์•ž์œผ๋กœ ๋‚˜์•„๊ฐ€ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋งŽ์ด ์‘์› ๋ถ€ํƒ๋“œ๋ฆด๊ฒŒ์š”. ๐Ÿ˜

 

 

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜Š

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค. ๐Ÿ‘