Languege/Java & Spring / / 2023. 11. 15. 17:32

Stream ์‚ฌ์šฉ๋ฒ•

๐Ÿ’ฅ ๊ฐœ์š”

์šฐํ…Œ์ฝ” 3์ฃผ์ฐจ ์ฝ”๋“œ๋ฆฌ๋ทฐ ์ง„ํ–‰ ์ค‘ Lotto๋ฅผ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ๋ฐ›์€ ๊ฐœ์ˆ˜๋งŒํผ ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋งŒ๋“œ๋Š”๋ฐ, ๋” ์ข‹์€ ์ฝ”๋“œ๋ฅผ ์ถ”์ฒœํ•ด์ฃผ์…”์„œ Stream์˜ ํ™œ์šฉ๊ณผ ์‹ฌํ™”์— ๋Œ€ํ•ด ์•Œ์•„์•ผ๊ฒ ๋‹ค. ๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.

 

1. Stream ์ƒ์„ฑํ•˜๊ธฐ

Java์—์„œ Stream์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์–‘ํ•ฉ๋‹ˆ๋‹ค. ์ปฌ๋ ‰์…˜, ๋ฐฐ์—ด, ํŠน์ • ๋ฒ”์œ„์˜ ์ˆซ์ž ๋“ฑ์—์„œ Stream์„ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

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

2. Stream ๊ฐ€๊ณตํ•˜๊ธฐ(์ค‘๊ฐ„์—ฐ์‚ฐ)

Stream์„ ๊ฐ€๊ณตํ•˜๋Š” ์ค‘๊ฐ„ ์—ฐ์‚ฐ์—๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋“ค์€ Stream์„ ๋ณ€ํ˜•ํ•˜์ง€๋งŒ, ์ตœ์ข… ์—ฐ์‚ฐ์ด ํ˜ธ์ถœ๋˜๊ธฐ ์ „๊นŒ์ง€๋Š” ์‹คํ–‰๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

[ ํ•„ํ„ฐ๋ง - Filter ]

ํŠน์ • ์กฐ๊ฑด์— ๋งž๋Š” ์š”์†Œ๋งŒ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.

stream.filter(s -> s.startsWith("a"));

[ ๋ฐ์ดํ„ฐ ๋ณ€ํ™˜ - Map ]

๊ฐ ์š”์†Œ์— ํ•จ์ˆ˜๋ฅผ ์ ์šฉํ•˜์—ฌ ๋ณ€ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

stream.map(String::toUpperCase);

[ ์ •๋ ฌ - Sorted ]

์ฃผ์–ด์ง„ Comparator์— ๋”ฐ๋ผ Stream์˜ ์š”์†Œ๋ฅผ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.

stream.sorted(Comparator.naturalOrder());

[ ์ค‘๋ณต ์ œ๊ฑฐ - Distinct ]

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

stream.distinct();

[ ํŠน์ • ์—ฐ์‚ฐ ์ˆ˜ํ–‰ - Peek ]

๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ถ”๊ฐ€์ ์ธ ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ์ฃผ๋กœ ๋””๋ฒ„๊น…์— ์œ ์šฉํ•ฉ๋‹ˆ๋‹ค.

stream.peek(System.out::println);

[ ์›์‹œ Stream <-> Stream ]

๊ธฐ๋ณธํ˜• ํƒ€์ž…์˜ Stream๊ณผ ๊ฐ์ฒดํ˜• Stream ๊ฐ„์˜ ๋ณ€ํ™˜์ž…๋‹ˆ๋‹ค.

IntStream intStream = stream.mapToInt(String::length);

3. Stream ๊ฒฐ๊ณผ ๋งŒ๋“ค๊ธฐ(์ตœ์ข… ์—ฐ์‚ฐ)

Stream์˜ ์ตœ์ข… ์—ฐ์‚ฐ์œผ๋กœ ๊ฒฐ๊ณผ๋ฅผ ๋„์ถœํ•ฉ๋‹ˆ๋‹ค. ์ด ์—ฐ์‚ฐ์„ ์ˆ˜ํ–‰ํ•œ ํ›„์—๋Š” Stream์„ ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

[ ์ตœ๋Œ“๊ฐ’/์ตœ์†Ÿ๊ฐ’/์ดํ•ฉ/ํ‰๊ท /๊ฐฏ์ˆ˜ - Max/Min/Sum/Average/Count ]

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

stream.count();

[ ๋ฐ์ดํ„ฐ ์ˆ˜์ง‘ - collect ]

Stream์˜ ์š”์†Œ๋ฅผ ๋‹ค์–‘ํ•œ ๋ฐฉ์‹์œผ๋กœ ์ˆ˜์ง‘ํ•ฉ๋‹ˆ๋‹ค.

1. Collectors.toList()

stream.collect(Collectors.toList());

2. Collectors.joining()

stream.collect(Collectors.joining(", "));

3. Collectors.averagingInt(), Collectors.summingInt(), Collectors.summarizingInt()

stream.collect(Collectors.averagingInt(String::length));

4. Collectors.groupingBy()

stream.collect(Collectors.groupingBy(String::length));

5. Collectors.partitioningBy()

stream.collect(Collectors.partitioningBy(s -> s.length() > 2));

[ ์กฐ๊ฑด ๊ฒ€์‚ฌ - Match ]

ํŠน์ • ์กฐ๊ฑด์ด Stream์˜ ๋ชจ๋“  ์š”์†Œ์— ๋Œ€ํ•ด ์ฐธ์ธ์ง€, ํ•˜๋‚˜๋ผ๋„ ์ฐธ์ธ์ง€, ๋ชจ๋‘ ๊ฑฐ์ง“์ธ์ง€ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.

stream.allMatch(s -> s.contains("a"));

[ ํŠน์ • ์—ฐ์‚ฐ ์ˆ˜ํ–‰ - forEach ]

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

stream.forEach(System.out::println);

 

 

์ถœ์ฒ˜

https://mangkyu.tistory.com/114

 

[Java] Stream API์˜ ํ™œ์šฉ ๋ฐ ์‚ฌ์šฉ๋ฒ• - ๊ธฐ์ดˆ (3/5)

1. Stream ์ƒ์„ฑํ•˜๊ธฐ ์•ž์„œ ์„ค๋ช…ํ•œ๋Œ€๋กœ Stream API๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋จผ์ € Stream์„ ์ƒ์„ฑํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ํƒ€์ž…์— ๋”ฐ๋ผ Stream์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅธ๋ฐ, ์—ฌ๊ธฐ์„œ๋Š” Collection๊ณผ Array์— ๋Œ€ํ•ด์„œ Stream์„ ์ƒ์„ฑํ•˜

mangkyu.tistory.com

 

  • ๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ ๊ณต์œ 
  • ๋„ค์ด๋ฒ„ ๋ฐด๋“œ ๊ณต์œ 
  • ํŽ˜์ด์Šค๋ถ ๊ณต์œ 
  • ์นด์นด์˜ค์Šคํ† ๋ฆฌ ๊ณต์œ