import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
static class A {
int x, y;
public A(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
//Stream API ์ด์ฉํ ๊ตฌํ
List<Integer> list = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
//์ค๊ฐ ์ฒ๋ฆฌ mapToInt -> IntStream
//i๋ผ๋ ์
๋ ฅ์ ๋ํด i๋ผ๋ ์ถ๋ ฅ์ผ๋ก ์ ํ
//max, OptionalInt -> ์ต์ข
์ฒ๋ฆฌ
//๋ฆฌ์คํธ๊ฐ ๋น์ด์๋ ๊ฒฝ์ฐ ๋๋ฌธ์ null ๋๋ฌธ์ Optional์ฒ๋ฆฌ
//getAsInt -> Optional์ฒ๋ฆฌ ๊ฐ ๊ฐ์ ธ์ค๊ธฐ
int maxVal = list.stream().mapToInt(i -> i).max().getAsInt();
System.out.println("maxVal : " + maxVal);
//์คํธ๋ฆผ์ ํน์ง
//์๋ณธ ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ์ง ์๋๋ค.
//์คํธ๋ฆผ์ ์ผํ์ฉ์ด๋ค.
//๋ด๋ถ ๋ฐ๋ณต์ผ๋ก ์์
์ ์ฒ๋ฆฌํ๋ค.
//์ฒด์ด๋์ผ๋ก ์์
//์คํธ๋ฆผ ์ฌ์ฉ ์์
//๋ฆฌ์คํธ๋ฅผ ๋ฐฐ์ด๋ก ๋ฐํ
//๋ฐฐ์ด์ ๋ฆฌ์คํธ๋ก ๋ฐํ
//List<Integer> -> Integer[]
//Integer = Wrapping class
//list.stream(). -> Stream<Integer>
//Integer[]::new) ํญ์ ๋ฐฐ์ด์ ์์ฑ์๋ฅผ ๋ง๋ค์ด ์ค์ผํจ.
Integer[] array = list.stream().toArray(Integer[]::new);
//List<Integer> -> int[]
//list.stream() -> Stream<Integer>
//mapToInt -> IntStream
//toArray -> ์ด๋ฏธ int[]๋ก ๋ง๋ค์ด์ฃผ๋๊ฑธ ์๊ณ ์๊ธฐ ๋๋ฌธ์ ๋ฐฐ์ด์ ์์ฑ์ ํ์X
int[] intArray = list.stream().mapToInt(i -> i).toArray();
//int[] -> List<Integer>
//Arrays.stream(intArray) -> IntStream
//boxed int๊ฐ Integer๊ฐ ๋๋๊ฒ ๋ฐ์ฑ ์๋ฏธ. -> stream<Integer>
//collect ๋ฐํ ๋ฐ์๋ ํน๋ณํ ๊ฐ์ผ๋ก ๋ฐํ ๋ฐ๊ธฐ์ํ ์์
//๋ฐฐ์ด์ ํน์ ํ ํ์์ผ๋ก ๋ฐ๊ฟ์ฃผ๋ คํ ๋
//Collectors.toList() -> List<Integer>๋ก ๋ฐํ๋จ.
//java 8.
List<Integer> list1 = Arrays.stream(intArray).boxed().collect(Collectors.toList());
//java 16์ด์
List<Integer> list2 = Arrays.stream(intArray).boxed().toList();
//์ฌ๋ฌ ๊ฐ์ ๋ฐฐ์ด์ ๋์์ ๋ฐ๋ณต.
int[] array1 = {10, 20, 30, 40};
int[] array2 = {100, 200, 300, 400};
System.out.println("== ๋ ๋ฐฐ์ด์ ๋์์ ์ฌ์ฉ! ๋๋ฐฐ์ด ๋ํ๊ธฐ ==");
//range(0, array1.length). -> 0~array.length-1 ๋ฐ๋ณตํ๋ IntStream
//map(i -> array1[i] + array2[i]) -> ๋๋ค์์ ์ด์ฉํ ๊ธฐ๋ฅ ์ปค์คํ
//System.out::println -> void
IntStream.range(0, array1.length).map(i -> array1[i] + array2[i]).forEach(System.out::println);
//๋ ๋ฐฐ์ด์ ๊ฒฐํฉํ์ฌ A๊ฐ์ฒด ์์ฑ
int[] array3 = {10, 20, 30, 40};
int[] array4 = {100, 200, 300, 400};
//IntStream.range(0, array3.length). -> IntStream
//boxed -> Stream<Integer>
//map(i -> new A(array3[i], array4[i])) -> Stream<A>
//collect(Collectors.toList()) -> List<A>
List<A> as = IntStream.range(0, array3.length).boxed().map(i -> new A(array3[i], array4[i])).collect(Collectors.toList());
System.out.println("== ๋ ๋ฐฐ์ด์ ๊ฒฐํฉํ์ฌ A๊ฐ์ฒด ์์ฑ ==");
System.out.println("List<A> : " + as);
//์ ๋๊ฐ์ด ์ต๋์ธ ๊ฐ ์ฐพ๊ธฐ
List<Integer> listAbs = new ArrayList<>();
listAbs.add(-10);
listAbs.add(40);
listAbs.add(110);
listAbs.add(-140);
//istAbs.stream() -> Stream<Integer>
//max((x, y) -> Math.abs(x) - Math.abs(y)) -> Optional<Integer>
//(x, y) -> Math.abs(x) - Math.abs(y) -> Comperator
//get() -> Integer
int getMaxVal = listAbs.stream().max((x, y) -> Math.abs(x) - Math.abs(y)).get();
System.out.println("== ์ ๋๊ฐ์ด ์ต๋์ธ ๊ฐ ์ฐพ๊ธฐ ==");
System.out.println("getMaxVal : " + getMaxVal);
//์ซ์๋ฅผ ์ ๋ ฌํ ๋ฌธ์์ด๋ก ์ด์ด ๋ถ์ด๊ธฐ
//[10, 40, 9, 15] -> [9, 10, 15, 40] -> "9101450"
List<Integer> listConcat = new ArrayList<>();
listConcat.add(10);
listConcat.add(40);
listConcat.add(9);
listConcat.add(15);
//listConcat.stream(). -> Stream<Integer>
//sorted() -> Stream<Integer>
//map(String::valueOf) -> Stream<String>
//collect(Collectors.joining()) -> String
//joining() -> ์ฌ์ด์ ๋ฌธ์์ด์ ๋ผ์์ ๋ฌธ์์ด์ ์ฐ๊ฒฐ
String result = listConcat.stream().sorted().map(String::valueOf).collect(Collectors.joining());
System.out.println("== ์ซ์๋ฅผ ์ ๋ ฌํ ๋ฌธ์์ด๋ก ์ด์ด ๋ถ์ด๊ธฐ ==");
System.out.println("listConcat : " + result);
}
}
Leave a comment