Streams API
๋ค๋ฅธ ๊ฐ์ฒด๋ฅผ ํฌํจํ๋ ์ปฌ๋ ์ ์ด๋ผ๋ ์ฐ๋ฆฌ๊ฐ ์ป๊ณ ์ ํ๋ ์ ๋ณด๋ ๋น์ทํ ์ ์๋ค.
์๋ฅผ ๋ค์ด, '์ด๋ฌํ ์กฐ๊ฑด์ ๋ง๋ ์์ดํ ์ ์ค', '์ด๋ฌํ ๋ฐฉ๋ฒ์ ํ์ฉํด์ ๋ชจ๋ ์์ดํ ์ ๋ฐ๊ฟ์ค, ' '์ค๋ณต๋ ๋ฐ์ดํฐ๋ ์ง์์ค', ๋๋ '์ด๋ฐ ๋ฐฉ๋ฒ์ผ๋ก ์์๋ค์ ์ ๋ ฌํด ์ค' ๋ฑ์ ์ปฌ๋ ์ ์ ์์ฒญํ๋ค๊ณ ์๊ฐํด ๋ณด์. ์ฐ๋ฆฌ๋ ์ด๋ฌํ ์์ฒญ์ ์ฒ๋ฆฌํ๊ธฐ ์ํด for๋ฌธ๊ณผ Iterator๋ฅผ ์ด์ฉํด์ ์ฝ๋๋ฅผ ์์ฑํ๊ฑฐ๋ ๊ฐ ์ปฌ๋ ์ ํด๋์ค์ ๋ง๋ ๋ฉ์๋๋ฅผ ์ฌ์ฉํด ์๋ค. List๋ฅผ ์ ๋ ฌํ ๋๋ Collections.sort()๋ฅผ ์ฌ์ฉํ๊ณ ๋ฐฐ์ด์ ์ ๋ ฌํ ๋๋ Arrays.sort()๋ฅผ ์ฌ์ฉํ๋ค.
Streams API๋ ๋ฐ์ดํฐ ์์ค๋ฅผ ์ถ์ํ ํด์ ๋ฐ์ดํฐ ์์ค๊ฐ ๋ฌด์์ด๋ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๋ค๋ฃฐ ์ ์๋ ๋ฉ์๋๋ฅผ ์ ๊ณตํด ์ค๋ค. ๊ทธ๋์ ์ฐ๋ฆฌ๋ '์ด๋ป๊ฒ' ๋ณด๋ค๋ '๋ฌด์์'์ ์ด์ ์ ๋ง์ถฐ ์ปฌ๋ ์ ์ Streams API๊ฐ ์ ๊ณตํ๋ ์ฐ์ฐ๋ค์ ์ฌ์ฉํ ์ ์๋ค.
Stream ๋ฉ์๋์๋ ์ค๊ฐ ์ฐ์ฐ(intermediate operations)ํ๊ณ ์ต์ข ์ฐ์ฐ(terminal operations)์ด ์๋๋ฐ ์ต์ข ์ฐ์ฐ์ ์ฌ์ฉํด์ผ ์ํ๋ ๊ฒฐ๊ณผ(output)๋ฅผ ์ป์ ์ ์๋ค. ์ค๊ฐ ์ฐ์ฐ์ ์ง์ฐ๋ ์ฐ์ฐ์ผ๋ก ์ต์ข ์ฐ์ฐ์ด ์ํ๋๊ธฐ ์ ๊น์ง๋ ์ค๊ฐ ์ฐ์ฐ์ด ์ํ๋์ง ์๋๋ค.
- ์ค๊ฐ ์ฐ์ฐ(lazy) : ์คํธ๋ฆผ ๋ฉ์๋๋ก ๋ค๋ฅธ ์คํธ๋ฆผ์ ๋ฐํํ๋ค. ์ต์ข
์ฐ์ฐ์ ํธ์ถํด์ ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๊ธฐ ์ ๊น์ง ์คํ์ฒ๋ผ ์ค๊ฐ ์ฐ์ฐ์ ๊ณ์ ์น์ด์ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๋ค. ์ค๊ฐ ์ฐ์ฐ์ stateless ํ๊ณ stateful๋ก ๋๋ ์ ์๋๋ฐ ๋ง์ฝ ์ค๊ฐ ์ฐ์ฐ์ด ์ฐ์ฐ์ ํ๊ธฐ ์ํด ๋ฐ์ดํฐ ์์ค์ ์ ๋ณด๋ฅผ ๋ชจ๋ ์์์ผ ํ๋ค๋ฉด ๊ทธ ์ฐ์ฐ์ stateful ํ๋ค๊ณ ํ ์ ์๋ค. ์๋ฅผ ๋ค์ด, filter()๋ stateless ํ๊ณ , sorted()๋ stateful ํ๋ค๊ณ ํ ์ ์๋ค.
- ์) Stream<T> limit (long maxSize) : ์คํธ๋ฆผ์ ์ผ๋ถ๋ฅผ ์๋ผ๋ผ ๋ ์ฌ์ฉ. maxSize๋งํผ ์คํธ๋ฆผ์ ์์๋ฅผ ์ ํํ๋ค.
- ์ต์ข
์ฐ์ฐ(eager) : ์คํธ๋ฆผ์ ์์๋ฅผ ์๋ชจํด์ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค์ด ๋ธ๋ค. ์ต์ข
์ฐ์ฐ ํ์์๋ ์คํธ๋ฆผ์ด ๋ซํ๊ณ ๋ ์ด์ ์ฌ์ฉํ ์ ์๋ค.
- ์) boolean anyMatch(Predicate<? super T> predicate) : returns true if any element matches the provided predicate
์คํธ๋ฆผ ์คํผ๋ ์ด์ ์ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ(stream pipeline)์ 3๋จ๊ณ๋ก ๋๋ ์ ์๊ณ , ์ด ์ค 1๋ฒ์งธ์ 3๋ฒ์งธ๋ ๊ผญ ํ์ํ๋ค.
- ์ปฌ๋ ์ ์ ์คํธ๋ฆผํํ๊ธฐ
- ๋ง๋ค์ด์ค ์คํธ๋ฆผ์ ์ค๊ฐ ์ฐ์ฐ ์ฌ์ฉ
- ๊ฒฐ๊ณผ๋ฌผ์ ์ต์ข ์ฐ์ฐ์ ์ฌ์ฉํด์ ๊ฐ์ ธ์ค๊ธฐ
์คํธ๋ฆผ์ ํน์ง์ ๋ค์๊ณผ ๊ฐ๋ค.
- ์ฌ์ฌ์ฉ์ด ๋ถ๊ฐ๋ฅํ๋ค. ๋ง์ฝ ์คํธ๋ฆผ์ ๋ณ์์ ์ ์ฅํด ๋๊ณ ์ฌ์ฌ์ฉํ๋ ค๊ณ ํ๋ฉด exception์ด ๋ฐ์ํ๋ค.
- ์คํธ๋ฆผ์ด ๋์ํ๋ ๋์์๋ ์ปฌ๋ ์ ์ ๋ณ๊ฒฝํ ์ ์๋ค.
- ์คํธ๋ฆผ ์ฐ์ฐ์ ์๋ณธ ์ปฌ๋ ์ ์ ๋ณ๊ฒฝํ์ง ์๋๋ค.
์ค๋ธ์ ํธ 5์ฅํ๊ณ 12์ฅ์ ์คํธ๋ฆผ์ด ๋์์ TODO๋ก ์ ์ด๋๊ณ ๋์ด๊ฐ๋ ์ฝ๋๋ฅผ ๊ฐ์ ธ์๋ค.
- 5์ฅ ์ฝ๋
- 12์ฅ ์ฝ๋
Lambda
๋๋ค์์ ์ ์ดํดํ๋ฉด Stream API๋ฅผ ๋ ์ฝ๊ฒ ์ฌ์ฉํ ์ ์๋ค. ๋๋ค์์ ๊ฐ์ฒด์ด๊ณ , single abstract method์ ํธ์ถํด์ ์ฌ์ฉํ๋ค. ๋๋ค์์ ํจ์ํ ์ธํฐํ์ด์ค(functional interface)๋ฅผ ๊ตฌํํ๊ณ ์๊ณ ํจ์ํ ์ธํฐํ์ด์ค์๋ ์ค์ง ํ๋์ ์ถ์ ๋ฉ์๋๋ง ์ ์๋์ด ์์ด์ผ ํ๋ค. (static ๋ฉ์๋์ default ๋ฉ์๋์ ๊ฐ์๋ ์๊ด์์) ์ต๋ช ๋ด๋ถ ํด๋์ค์ ๋น์ทํจ.
ํจ์ํ ์ธํฐํ์ด์ค
- BinaryOperator<T> : method apply T ํ์ ์ ์ธ์ 2๊ฐ๋ฅผ ๋ฐ์์ T ํ์ ์ ๊ฐ์ ๋ฐํํจ
- Consumer<T> : method accept T ํ์ ์ ์ธ์๋ฅผ ๋ฐ๊ณ , ๋ฐํ๊ฐ์ ์์
- Function<T, R> : method apply T ํ์ ์ ์ธ์๋ฅผ ๋ฐ์์ R ํ์ ์ ๊ฐ์ ๋ฐํํจ
- Predicate<T> : method test T ํ์ ์ ์ธ์๋ฅผ ๋ฐ์์ boolean์ ๋ฐํํจ
- Supplier<T> : method get ์ธ์๋ฅผ ๋ฐ์ง ์๊ณ , T ํ์ ์ ๊ฐ์ ๋ฐํํจ. ์คํธ๋ฆผ์์ ์ฐ์ฐ์ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ ์ปฌ๋ ์ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๋ฐ ์ฃผ๋ก ์ฌ์ฉ๋จ
- UnaryOperator<T> : method get ์ธ์๋ฅผ ๋ฐ์ง ์๊ณ , T ํ์ ์ ๊ฐ์ ๋ฐํํจ
Stream<T> filter(Predicate<? super T>predicate)
Returns a stream of the elements that match the given predicate.
<T> : the Type of the object in the stream
<R> : the type of the Result of the method
์ฌ๊ธฐ์ <? super T>๋ ์์ผ๋ ์นด๋์ธ๋ฐ ์ฌ๊ธฐ์์ T๋ lower bound์ด๊ณ ,
<? extends T>์์์ T๋ upper bound์ด๋ค.
์๋ฅผ ๋ค๋ฉด, List<? super Integer>์ด๋ฉด List<Integer>, List<Number>, ๊ทธ๋ฆฌ๊ณ List<Object>๊ฐ ๋ค์ด๊ฐ ์ ์๋ค.
๋๋ค์์ ๋ ๊ฐ๋จํ๊ฒ ํํํ ์ ์๋ ๋ฐฉ๋ฒ์ผ๋ก๋ ๋ฉ์๋ ์ฐธ์กฐ (method reference)๊ฐ ์๋๋ฐ,
ํ๋์ ๋ฉ์๋๋ง ํธ์ถํ๋ ๋๋ค์์ 'ํด๋์ค ์ด๋ฆ::๋ฉ์๋์ด๋ฆ' ๋๋ '์ฐธ์กฐ๋ณ์::๋ฉ์๋์ด๋ฆ'์ผ๋ก ๋ฐ๊ฟ ์ ์๋ค.
๋ฉ์๋ ์ฐธ์กฐ์๋ 4๊ฐ์ง ํ์ ์ด ์๋ค.
- instance method => String::toUpperCase String ํด๋์ค์ toUpperCase ๋ฉ์๋ ํธ์ถ
String::toUpperCase String ํด๋์ค์ toUpperCase ๋ฉ์๋ ํธ์ถ
์๋ ๋ ๊ฐ์ ๋๋ค์์ ๋ฉ์๋ ์ฐธ์กฐ๋ก ๋ฐ๊พผ ๊ฒ.
(String s) -> {return s.toUpperCase();}
s -> s.toUpperCase()
- instance method of specific object => System.out::println
- static method => Math::sqrt
- constructor => TreeMap::new ๊ธฐ๋ณธ ์์ฑ์(์ธ์๊ฐ ์๋)๋ฅผ ํธ์ถํด์ ๊ฐ์ฒด๋ฅผ ์์ฑํ ๋ ์ฌ์ฉ
์๋ฐ 8์์ ๋๋ค์ ์คํธ๋ฆผ์ด ์ถ๊ฐ๋๋ฉด์ functional programming์ด ๊ฐ๋ฅํด์ก๊ณ ,
๋ณ์๋ค์ด immutable ํ๊ธฐ ๋๋ฌธ์ ๋ฐํ์์์ ๋ฐ์ดํฐ์ ๋ฌธ์ ๊ฐ ์๊ธธ ํ๋ฅ ์ด ์ค์ด๋ค์ด ๋ฉํฐ์ค๋ ๋ ํ๊ฒฝ์์ ๋ณ๋ ฌ์ฒ๋ฆฌํ๋ ๊ฒ์ ์ข ๋ ์ฝ๊ฒ ๋ง๋ค์ด ์ค๋ค.
// TODO
๋ฉํฐ์ค๋ ๋ ๋ถ๋ถ์ ์์ง ๊ณต๋ถํ์ง ์์๋๋ฐ, ๋์ค์ ์ ์ด๋ ๊ฒ ๋๋์ง ์ฐพ์๋ด์ผ๊ฒ ๋ค.
์ฐธ๊ณ ์๋ฃ
ํค๋ํผ์คํธ ์๋ฐ ch.12
์๋ฐ์ ์ ์ ch.14
์คํ๋ง ์ ๋ฌธ์ ์ํ ์๋ฐ ๊ฐ์ฒด ์งํฅ์ ์๋ฆฌ์ ์ดํด ๋ถ๋ก B.
https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html
https://docs.oracle.com/javase/tutorial/extra/generics/morefun.html
https://stackoverflow.com/questions/2827585/what-is-super-t-syntax
https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html
'์คํฐ๋ > ์๋ฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
์ ๋ฌด ํจ์จ์ ์ฌ๋ ค์ฃผ๋ Intellij ๋จ์ถํค (์๋์ฐ ๊ธฐ์ค) (1) | 2024.02.25 |
---|---|
Spring Boot file upload download rest api ์์ - 1 (0) | 2024.02.19 |
์๋ฐ iterators๋? (0) | 2024.02.11 |
์๋ฐ์์ Map ์ํ ๋ฐฉ๋ฒ (0) | 2024.01.27 |
Garbage Collection in Java (0) | 2022.10.28 |
๋๊ธ