Day14 - jeremy0405/Codesquad_CS GitHub Wiki
๋ด๊ฐ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ ์ฒ์ ์ ํ ๊ฑด ์๋ฐ ๊ณต๋ถ๋ฅผ ๋ง ์์ํ์ ๋ ์ธํ ๋ฆฌ์ ์ด์ ์ถ์ฒ์ผ๋ก ๋ด ์ฝ๋๋ฅผ ๋๋ค์์ผ๋ก ๋ฐ๊ฟจ์ ๋์ด๋ค. ๋ณด๊ธฐ์ ๊ต์ฅํ ๋ฉ์์ด ๋ณด์ฌ์ ์ธ์ ๊ฐ ๋๋ค๋ฅผ ๊ณต๋ถํด์ ์์ ์์ฌ๋ก ํ์ฉํด์ผ์ง๋ผ๋ ์๊ฐ์ ํ๋ ๊ฒ ๊ฐ๋ค.
์ด๋ ๊ฒ ์๊ฐํ ์ง ๋ฒ์จ 3๋ฌ์ด ํ๋ ๋๋ฐ ์์ง๋ ๋๋ค์ ์ด์ํ๋ค.. ๐
์ด๋ฒ ๋ฏธ์ ์ ํตํด ๋๋ค์ ์คํธ๋ฆผ์ ๋ํด์ ๊ณต๋ถํ ์ ์๋ ๊ธฐํ๊ฐ ์๊ฒจ ๋๋ค์ ์คํธ๋ฆผ ์ฌ์ฉ๋ฒ ๋ํด์ ๊ฐ๋จํ ์ ๋ฆฌํด ๋ณด๋ ค๊ณ ํ๋ค.
- ํจ์ํ ์ธํฐํ์ด์ค(abstract ๋ฉ์๋๊ฐ 1๊ฐ)๋ฅผ ๊ฐ๋จํ ์์ผ๋ก ํํํ๋ ๊ฒ์ด๋ค.
java.util.function
์๋ฐ์์ ๋ง์ด ์ฌ์ฉ๋๋ ํจ์ํ ์ธํฐํ์ด์ค๋ค์ ์ ์ํด๋์ pakage
์์ฃผ ์ฌ์ฉ๋๋ ํจ์ํ ์ธํฐํ์ด์ค ์์
Supplier<T>
-
T get(): input ์๊ณ output :T
Consumer<T>
-
void accept(T t): input :T, output ์์
Function<T, R>
-
R apply(T t): input :T, output :R
Predicate<T>
-
boolean test(T t): input :T, output :boolean
BiConsumer<T, U>
-
void accept(T t, U u): input :T,U, output ์์
BiFunction<T, U, R>
-
R apply(T t, U u): input :T,U, output :R
BiPredicate<T, U>
-
boolean test(T t, U u): input :T,U, outputboolean
Stream์ ์ปฌ๋ ์ , ๋ฐฐ์ด์ ์ ์ฅ๋์ด ์๋ ๊ฐ์ ์ฐธ์กฐํ์ฌ ๋ฐ์ดํฐ๋ฅผ ์ฒ๋ฆฌํ๋ ํ๋ฆ์ ๋งํ๋ค. ๋๋ค๋ฅผ ํ์ฉํด์ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ ์ด์ฉํด์ ํญ์ ๋์ผํ input์ ์ ์ผํ output์ ๊ธฐ๋ํ ์ ์๋ค.
stream์ ํฌ๊ฒ stream ์์ฑ, ๊ฐ๊ณต, ์ต์ข ์ฐ์ฐ(๊ฒฐ๊ณผ ๋ง๋ค๊ธฐ)์ ์์๋ก ์ด๋ฃจ์ด์ ธ ์๋ค.
์ง์ฐ์ฑ์ ์ด์ฉํด ์ต์ข ์ฐ์ฐ์ด ์์ด์ผ์ง ๊ฐ๊ณต๊ณผ์ ์ ์คํํ๋๋ก ๋์ด ์๋ค.
- ๋ฐฐ์ด / ์ปฌ๋์ / ๋น ์คํธ๋ฆผ
Stream.iterate()- ๊ธฐ๋ณธ ํ์ ํ (IntStream, LongStream)
- ์คํธ๋ฆผ ์ฐ๊ฒฐํ๊ธฐ
๋ฐฐ์ด์ ์คํธ๋ฆผ์ผ๋ก ์์ฑ
Stream<Integer> stream = Arrays.stream(intArray);
Stream<Integer> partOfArrayStream = Arrays.stream(intArray, 1, 3);
1 ~ (3 - 1)๋ฒ ์ธ๋ฑ์ค๊น์ง๋ง ์คํธ๋ฆผ์ผ๋ก ๋ง๋ฌ
์ปฌ๋ ์ ์ ์คํธ๋ฆผ์ผ๋ก ์์ฑ
Stream<Integer> stream = intList.stream()
Stream.iterate()๋ก ์คํธ๋ฆผ ์์ฑ
Stream<Integer> iteratedStream = Stream.iterate(30, n -> n + 2).limit(5);
์ด๊ธฐ๊ฐ์ด 30, 2์ฉ ์ฆ๊ฐํ๋ 5๊ฐ์ ์์๋ฅผ ๊ฐ๋ ์คํธ๋ฆผ ์์ฑ ([30, 32, 34, 36, 38])
๊ผญ limit(5); ์ฒ๋ผ ์ฌ์ด์ฆ๋ฅผ ์ ํํด์ผ ํจ. (๊ทธ๋ฅ iterate()๋ก ๋ง๋ค๋ฉด ์คํธ๋ฆผ์ ์ฌ์ด์ฆ๊ฐ ๋ฌดํํ๊ธฐ ๋๋ฌธ)
๊ธฐ๋ณธ ํ์ ํ ์คํธ๋ฆผ ์์ฑ
IntStream intStream = IntStream.range(1, 5); // 1, 2, 3, 4 ์์ฑ
IntStream intStream = IntStream.rangeClosed(1, 5); // 1, 2, 3, 4, 5 ์์ฑ
- !! Random Class๋ฅผ ์ด์ฉํ ๋๋คํ ๊ธฐ๋ณธํ ์คํธ๋ฆผ ์์ฑ
-
DoubleStream randomDoubles = new Random().doubles(3);// ๋์ 3๊ฐ ์์ฑ
-
new Random().ints(0, 10)
.limit(10)
.forEach(System.out::println);
// 0 ~ 9 ๊น์ง์ ๋๋คํ int ๊ฐ๋ค์ 10๊ฐ ์์ฑํ ํ ์ถ๋ ฅํจ์คํธ๋ฆผ ์ฐ๊ฒฐํ๊ธฐ
Stream<String> concat = Stream.concat(strStream1, strStream2)
IntStream concat = IntStream.concat(IntStream1, IntStream2)
Stream<T> filter(Predicate<? super T> predicate);
Stream<String> stream = names.stream()
.filter(name -> name.contains("a"));<R> Stream<R> map(Function<? super T, ? extends R> mapper);
Stream<String> stream = names.stream()
.map(String::toUpperCase);<R> Stream<R> flatMap(Function<? super T, ? extends Stream<? extends R>> mapper);
students.stream()
.flatMapToInt(student ->
IntStream.of(student.getKor(),
student.getEng(),
student.getMath()))
.average().ifPresent(avg ->
System.out.println(Math.round(avg * 10)/10.0));Stream<T> sorted();
Stream<T> sorted(Comparator<? super T> comparator);
IntStream.of(14, 11, 20, 39, 23)
.sorted()
.boxed()
.collect(Collectors.toList());Stream<T> peek(Consumer<? super T> action);
์ค๊ฐ์ ๊ฐ ํ์ธ์ ํ๊ธฐ ์ํด์ ์์ฃผ ์ฌ์ฉํ๋ค.
(์คํธ๋ฆผ ์ฐ์ฐ ๋์ค ๊ถ๊ธํ ๋ถ๋ถ).peek(System.out::println)Calculating
long count = IntStream.of(1, 3, 5, 7, 9).count();
long sum = LongStream.of(1, 3, 5, 7, 9).sum();
OptionalInt min = IntStream.of(1, 3, 5, 7, 9).min();
OptionalInt max = IntStream.of(1, 3, 5, 7, 9).max();
DoubleStream.of(1.1, 2.2, 3.3, 4.4, 5.5)
.average()
.ifPresent(System.out::println); // Optional ๋์ ifPresent() ์ด์ฉReduction
// 1๊ฐ (accumulator)
Optional<T> reduce(BinaryOperator<T> accumulator);
// 2๊ฐ (identity)
T reduce(T identity, BinaryOperator<T> accumulator);OptionalInt reduced = IntStream.range(1, 4) // [1, 2, 3]
.reduce((a, b) -> {
return Integer.sum(a, b);
});
int reducedTwoParams = IntStream.range(1, 4) // [1, 2, 3]
.reduce(10, Integer::sum); // method referenceCollecting
.collect(Collectors.toList());
.collect(Collectors.joining()); // String์ ๋ชจ๋ ํฉ์ณ์ ํ๋์ String์ผ๋ก ๋ง๋๋ ๊ฒ
.collect(Collectors.joining(", ", "<", ">")); // joining(String์ฌ์ด ๊ตฌ๋ถ์, ๊ฒฐ๊ณผ ๋งจ ์, ๊ฒฐ๊ณผ ๋งจ ๋ค)String listToString =
productList.stream()
.map(Product::getName)
.collect(Collectors.joining(", ", "<", ">"));
// <potatoes, orange, lemon, bread, sugar>Matching
boolean anyMatch(Predicate<? super T> predicate); //ํ๋๋ผ๋ ๋ง์กฑํ๋์ง
boolean allMatch(Predicate<? super T> predicate); // ๋ชจ๋ ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง
boolean noneMatch(Predicate<? super T> predicate); // ๋ชจ๋ ์กฐ๊ฑด์ ๋ง์กฑํ์ง ์๋์งforEach
void forEach(Consumer<? super T> action);.forEach(System.out::println);