๐ก ์ปฌ๋ ์ ์ ์ ์ฅ๋ ์๋ฆฌ๋จผํธ๋ค์ ํ๋์ฉ ์ํํ๋ฉด์ ์ฒ๋ฆฌํ ์ ์๋ ๊ธฐ๋ฅ์ด๋ค. ๋๋ค์๊ณผ ํจ๊ป ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ๋ฐ์ดํฐ์ ๋ํ ์ฒ๋ฆฌ๋ฅผ ๊ฐ๊ฒฐํ๊ฒ ํํํ ์ ์๋ค. ๋ด๋ถ ๋ฐ๋ณต์๋ฅผ ์ฌ์ฉํด ๋ณ๋ ฌ์ฒ๋ฆฌ๋ฅผ ํ ์ ์๋ค.
/* Stream */
List<String> list = new ArrayList<>(
Arrays.asList("hello", "stream","world")
);
/* ํน์ ์์๋ฅผ ์ฐพ๊ธฐ ์ํด iterator ๋๋ foreach ๋ฑ์ ํ์ฉ */
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
String str = iterator.next();
if(str.equals("stream")) {
System.out.println("๊ฑธ๋ฌ๋ธ ๊ฐ : " + str);
}
}
/* ์์ ๋์์ Stream์ ํ์ฉํด ์ํ */
list.stream()
.filter("stream"::equals)
.forEach(str -> System.out.println("๊ฑธ๋ฌ์ง ๊ฐ : " + str));
iterator ์ฒ๋ผ ํ ๋ฒ๋ง ์ฌ์ฉ๋๊ณ ์ฌ๋ผ์ง๋ค. public static void main(String[] args) {
/* ์คํธ๋ฆผ์ ๋ณ๋ ฌ ์ฒ๋ฆฌ */
List<String> list = new ArrayList<>(
Arrays.asList("java", "mariadb", "html", "css", "mybatis")
);
/* ๋ชจ๋ ์์
์ ๊ธฐ๋ณธ์ ์ผ๋ก main ์ค๋ ๋์์ ์ผ์ด๋๋ค. */
System.out.println("for each ===================");
for(String s : list ){
print(s);
}
System.out.println("normal stream ==================");
list.stream().forEach(Application2::print);
/* ๋ณ๋ ฌ ์คํธ๋ฆผ ํ์ฉ */
/* ์ฒ๋ฆฌํด์ผํ๋ ๋ฐ์ดํฐ๊ฐ ๋ง์ ๊ฒฝ์ฐ ํจ์จ์ ์ผ๋ก ์ฒ๋ฆฌํ ์ ์๋ค. */
/* ์์ฐจ์ ์ผ๋ก ์ฒ๋ฆฌ๋์ง๋ ์๋๋ค. */
System.out.println("parallel stream ==================");
list.parallelStream().forEach(Application2::print);
list.parallelStream().forEach(Application2::print);
}
private static void print(String str) {
System.out.println(str + " : " + Thread.currentThread().getName());
}
- ์์ฑ : ์คํธ๋ฆผ ์์ฑ
- ๊ฐ๊ณต : ์ํ๋ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค๊ธฐ ์ํ ํํฐ๋ง, ๋งคํ ๋ฑ์ ์์
- ๊ฒฐ๊ณผ : ์ต์ข ๊ฒฐ๊ณผ๋ฅผ ๋์ถํ๋ค.
๋ฐฐ์ด, ์ปฌ๋ ์ -> ์คํธ๋ฆผ ์์ฑ -> ๋งคํ -> ํํฐ๋ง -> ๊ฒฐ๊ณผ
/* ๋ฐฐ์ด์ ์คํธ๋ฆผ์ผ๋ก ์์ฑ*/
String[] sarr = {"java", "mariadb", "jdbc"};
Stream<String> strStream1 = Arrays.stream(sarr);
/* forEach() : Consumer ํ์
์ ๋ฐ์ ์ต์ข
๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค์ด๋*/
strStream1.forEach(System.out::println);
/* ์ํ๋ ์ธ๋ฑ์ค ๊ฐ๋ง ์ถ๋ ฅํ ์ ์๋ค. */
Stream<String> strStream2 = Arrays.stream(sarr, 0, 2);
strStream2.forEach(System.out::println);
/* ์ปฌ๋ ์
์ ์คํธ๋ฆผ์ผ๋ก ์์ฑ*/
List<String> stringList = Arrays.asList("html", "css", "javascript");
Stream<String> strStream3 = stringList.stream();
strStream3.forEach(System.out::println);
/* ์ปฌ๋ ์
์๋ foreach() ๊ธฐ๋ฅ์ด ์ ์๋์ด ์๋ค. */
/* ๋ฐ๋ผ์ ๋จ์ํ foreach() ๊ธฐ๋ฅ๋ง ์ฌ์ฉํ ๋์๋ streamํ ํ์ง ์๊ณ ์ฌ์ฉํ ์ ์๋ค. */
stringList.forEach(System.out::println);
Stream().builder()/* Builder๋ฅผ ํ์ฉํ ์คํธ๋ฆผ ์์ฑ */
/* ์๋ ๋จผํธ๋ก ๋ฐ๋ก ์คํธ๋ฆผ์ ์์ฑํ๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ค. */
Stream<String> strStream4 = Stream.<String>builder()
.add("ํ๊ธธ๋")
.add("์ ๊ด์")
.add("์ค๋ด๊ธธ")
.build();
strStream4.forEach(System.out::println);
Stream().iterate()/* iterate() ๋ฅผ ํ์ฉํ์ฌ ์์ด ํํ์ ์คํธ๋ฆผ ์์ฑ */
/* Stream.iterate(์์์ , ์ ๋ฌ๋ ๊ฐ์ ๊ท์น)[.limit(ํ์)] */
Stream<Integer> intStream =
Stream.iterate(10, value -> value * 2).limit(10);
intStream.forEach(System.out::println);
/* ๊ธฐ๋ณธ ์๋ฃํ ์คํธ๋ฆผ ์์ฑ */
/* ์คํธ๋ฆผ์ผ๋ก ๋ง๋ค ๋ฒ์ 5 ~ 9*/
IntStream intStream = IntStream.range(5, 10);
intStream.forEach(i -> System.out.print(i + " "));
System.out.println();
/* rangeClosed(์์๊ฐ, ๋ง์ง๋ง๊ฐ) :> ๋ง์ง๋ง ๊ฐ ํฌํจ*/
LongStream longStream = LongStream.rangeClosed(5, 10);
longStream.forEach(i -> System.out.print(i + " "));
System.out.println();
/* Wrapper ํด๋์ค ์๋ฃํ์ ์คํธ๋ฆผ์ผ๋ก ๋ณํ์ด ํ์ํ ๊ฒฝ์ฐ boxing ํ ์ ์๋ค. */
Stream<Double> doubleStream = new Random().doubles(5).boxed();
doubleStream.forEach(i -> System.out.print(i + " "));
System.out.println();
/* ๋ฌธ์์ด์ IntStream์ผ๋ก ๋ณํ*/
/* chars() : ๋ฌธ์ ํ๋ํ๋๋ฅผ IntStream()d์ผ๋ก ๋ณ๊ฒฝ */
IntStream helloworldChars = "hello world".chars();
helloworldChars.forEach(i-> System.out.print(i + " "));
System.out.println();
/* ๋ฌธ์์ด splitํด์ Stream์ผ๋ก ์์ฑ */
Stream<String> splitStream =
Pattern.compile(", ")
.splitAsStream("html, css, javascript");
splitStream.forEach(i-> System.out.print(i + " "));
System.out.println();
/* Stream.of() */
/* ์์๋ฅผ ๋ฃ์ด ๋ฐ๋ก Stream ํ์
์ผ๋ก ๋ง๋ค ์ ์๋ค. */
Stream<String> stringStream1 = Stream.of("hello", "world");
Stream<String> stringStream2 = Stream.of("java", "mariadb");
/* ์คํธ๋ฆผ์ 1ํ์ฉ์ด๋ค. */
/* ์ต์ข
์ฐ์ฐ (ex: foreach()) ๋ฑ์ด ์ํ๋ ์คํธ๋ฆผ์ ์ฌ์ฌ์ฉ์ด ๋ถ๊ฐ๋ฅํ๋ค. */
/* ์ฌ์ฌ์ฉ ์ java.lang.IllegalStateException์ด ๋ฐ์ํ๋ค. --> ์ฃผ์ ์ฝ๋ ํ๋ฉด concat ์์ ๋ฐ์ */
/*
stringStream1.forEach(i -> System.out.print(i +" "));
stringStream2.forEach(i -> System.out.print(i +" "));
*/
/* concat() : ์คํธ๋ฆผ์ ํฉ์น๋ ๋ฉ์๋ */
Stream<String> concatStream = Stream.concat(stringStream1, stringStream2);
concatStream.forEach(i -> System.out.print(i + " "));
System.out.println();
์คํธ๋ฆผ์ ๋ฐ์ดํฐ๋ฅผ ์ํ๋ ๊ฒฐ๊ณผ ๊ฐ์ผ๋ก ๋ง๋ค๊ธฐ ์ํด ํ๋ ์ค๊ฐ ์ฒ๋ฆฌ ์์ ์ โ๊ฐ๊ณตโ์ด๋ผ ํ๋ค.
์คํธ๋ฆผ ๋ฐ์ดํฐ ๊ฐ๊ณต ๋ฉ์๋๋ค์ Stream์ ๋งค๊ฐ๋ณ์๋ก ๋ฐ์ Stream์ ๋ฐํํ๋ฏ๋ก ์ฐ์์ ์ผ๋ก ์ฐ๊ฒฐํด์ ์ฌ์ฉํ ์ ์๋ค.
์ด ๋ filter-map ๊ธฐ๋ฐ์ API๋ฅผ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ์ง์ฐ ์ฐ์ฐ์ ํตํด ์ฑ๋ฅ์ ์ต์ ํ ํ ์ ์๋ค.
Predicate๋ boolean์ ๋ฆฌํดํ๋ @FunctionalInterface์ด๋ค./* ์คํธ๋ฆผ์ ์ค๊ฐ ์ฐ์ฐ, ์ค๊ณ ์ฐ์ฐ ์ค ํ๋์ธ filter ์ฌ์ฉ */
/* 0 ~ 9 ๊น์ง ์คํธ๋ฆผํ */
IntStream intStream = IntStream.range(0, 10);
/* **filter** : ์คํธ๋ฆผ์์ ํน์ ๋ฐ์ดํฐ๋ง ๊ฑธ๋ฌ๋ด๋ ๋ฉ์๋์ด๋ค. */
/* ๋งค๊ฐ๋ณ์๋ก **Predicate** ํ์
์ด ์ ์ธ๋์ด ์๋ค. */
/* ใด> ๋งค๊ฐ๋ณ์๋ฅผ ๋ฐ์ boolean ํ์
๋ฐํ */
intStream.filter(i -> i % 2 == 0).forEach(System.out::println);
Operationํ์
์ผ๋ก ์ ๋ฌ๋ ๋งค๊ฐ๋ณ์์ ๋ฐํํ๋ ๊ฐ์ ํ์
์ด ๊ฐ๋ค./* ์คํธ๋ฆผ ์ค๊ณ ์ฐ์ฐ ์ค ํ๋์ธ map() ์ฌ์ฉ */
/* ์ค๊ณ ์ฐ์ฐ์ ์ต์ข
์ฐ์ฐ ์ํ ์ ๊น์ง ์ฌ๋ฌ ๋ฒ ์ํ ํ ์ ์๋ค. */
/* ๋จผ์ ์ผ์ด๋ ์ค๊ณ ์ฐ์ฐ์ ๊ฒฐ๊ณผ๊ฐ ํ์ดํ๋ผ์ธ์ ํตํด ๋ค์ ์ค๊ณ ์ฐ์ฐ์ผ๋ก ์ ๋ฌ ๋๋ค. */
IntStream intStream = IntStream.range(0, 10);
/* map : ์คํธ๋ฆผ์ ๋ด๊ธด ๋ฐ์ดํฐ๋ฅผ ๋๋ค์์ ํตํด ๊ฐ๊ณตํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์๋ก์ด ์คํธ๋ฆผ์ ๋ด๋๋ค. */
/* map์ ๋งค๊ฐ๋ณ์๋ **Operation** ํ์
์ผ๋ก , ๋งค๊ฐ๋ณ์์ ๋ฐํ ๊ฐ์ ํ์
์ด ๊ฐ๋ค. */
intStream.filter(i -> i % 2 == 0).map(i -> i * 5).forEach(System.out::println);