Java Stream
์ ์ฐ๋๊ฐ?
- ๋ฐฐ์ด๊ณผ ์ปฌ๋ ์ ์ ํจ์ํ์ผ๋ก ์ฒ๋ฆฌ
- ๊ฐ๋จํ ๋ณ๋ ฌ์ฒ๋ฆฌ(๋ฉํฐ ์ค๋ ๋ฉ), ์ฆ ๋ง์ ์์๋ค์ ๋น ๋ฅด๊ฒ ์ฒ๋ฆฌํ ์ ์๋ค. (์ด๋ฅผ ์ํด์๋ parallelStream()์ ์ฌ์ฉํด์ผํ๋ค.)
Test Set
ArrayList<string> list = new ArrayList<>(Arrays.asList("Apple", "Banana", "Melon", "Grape", "Strawberry"));
System.out.println(list);
map
list.stream().map(s->s.toUpperCase());
list.stream().map(String::toUpperCase);
์์๋ค์ ํน์ ์กฐ๊ฑด์ ํด๋นํ๋ ๊ฐ์ผ๋ก ๋ณํํด์ค๋ค.
filter
list.stream().filter(t->t.length() > 5)
์์๋ฅผ ํน์ ๊ธฐ์ค์ผ๋ก ๊ฑธ๋ฌ๋ด์ค๋๋ค.
sorted
list.stream().sorted()
๋ฆฌ์คํธ์ ์์๋ฅผ ์ ๋ ฌํฉ๋๋ค.
๊ฐ๊ณตํ ๊ฒฐ๊ณผ๊ฐ์ ๊ฐ์ ธ์ค๋ joining, toList
- Collectors.joining์ ์ด์ฉํด ๋ฆฌ์คํธ๋ฅผ ์กฐ์ธ์ ๊ธฐ์ค์ผ๋ก ๋ฐฐ์นํ ์ ์๋ค. String์ผ๋ก ๋ฆฌํด
- Collectors.toList๋ฅผ ์ด์ฉํด ๋ฆฌ์คํธ๋ก ๋ฆฌํด
[๋ ํผ๋ฐ์ค]