๐ ์คํธ๋ฆผ ํท๊ฐ๋ฆด ๋๋ง๋ค ๊ทธ๋ฅ ์ฌ๊ธฐ์์ ์ฐพ์๋ณด๋ ค๊ณ ๊ธฐ๋ณธ์๋ฅผ ๋ณด๋ฉฐ ํํ์ด ์ ๋ฆฌํ ๊ฒ์ ์ค๋ ๋๋์ด ์์ฑํ๋ค. (๋ด์ฉ์ด ๋ค์ ๊ธธ ์ ์์ต๋๋ค!) ์คํธ๋ฆผ..๊ฐ๋ณด์๊ณ ..!!
1. ์คํธ๋ฆผ ์๋ฏธ
1) ์ปฌ๋ ์
์ ์ ์ฅ ์์๋ฅผ ํ๋์ฉ ์ฐธ์กฐํด ๋๋ค์์ผ๋ก ์ฒ๋ฆฌํ ์ ์๋๋ก ๋์์ฃผ๋ ๋ฐ๋ณต์. (์๋ฐ8๋ถํฐ ์ถ๊ฐ)
2) ์๋ฐ7 ์ด์ ๊น์ง๋ List< String > ์ปฌ๋ ์
์์ ์์๋ฅผ ์์ฐจ์ ์ผ๋ก ์ฒ๋ฆฌํ๊ธฐ ์ํด Iterator ๋ฐ๋ณต์๋ฅผ ์ฌ์ฉํ์.
/* Iterator */
List<String> list = Arrays.asList("ํ๊ธธ๋", "๊น์๋ฐ");
Iterator<String> iterator = list.iterator();
while(iterator.hasNext()){
String name = iterator.next();
}
/* Stream */
List<String> list = Arrays.asList("ํ๊ธธ๋", "๊น์๋ฐ");
Stream<String> stream = list.stream();
stream.forEach( name -> System.out.println(name));
2. ์คํธ๋ฆผ์ ํน์ง
1) ๋๋ค์์ผ๋ก ์์ ์ฒ๋ฆฌ ์ฝ๋๋ฅผ ์ ๊ณต.
2) ์คํธ๋ฆผ์ ์์
์ ๋ด๋ถ ๋ฐ๋ณต์ผ๋ก ์ฒ๋ฆฌํจ. (for๋ฌธ์ ๋ฉ์๋ ์์ผ๋ก ๋ฃ์ด์ ์ฒ๋ฆฌ)
3) ์์
์ ๋ด๋ถ ๋ฐ๋ณต์ผ๋ก ์ฒ๋ฆฌํ๊ธฐ ๋๋ฌธ์ ๋ณ๋ ฌ ์ฒ๋ฆฌ๊ฐ ์ฌ์.
4) ์คํธ๋ฆผ์ ๋ฐ์ดํฐ ์์ค๋ก๋ถํฐ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ธฐ๋ง ํ ๋ฟ ์๋ณธ์ ๋ณ๊ฒฝํ์ง ์์.
List<Integer> list = Arrays.asList(3,1,5,4,2);
List<Integer> sortedList = list.stream().sorted() //list๋ฅผ ์ ๋ ฌํด์
.collect(Collectors.toList()); //์๋ก์ด List์ ์ ์ฅ
System.out.println(list); // [3,1,5,4,2] ์๋ณธ ๋ณ๊ฒฝ ์์ด
System.out.println(sortedList); // [1,2,3,4,5] ์ ๋ ฌ๋ ๋ณต์ฌ๋ณธ๋ง ์์
5) ์คํธ๋ฆผ์ Iterator ์ฒ๋ผ ์ผํ์ฉ์. (ํ์ํ๋ฉด ๋ค์ ์คํธ๋ฆผ์ ์์ฑํด์ผ ํจ)
strStream.forEach(System.out::println); //๋ชจ๋ ์์๋ฅผ ํ๋ฉด์ ์ถ๋ ฅ(์ต์ข
์ฐ์ฐ)
int numOfStr = strStream.count(); //์๋ฌ. ์ต์ข
์ฐ์ฐ ํ ์คํธ๋ฆผ์ด ์ด๋ฏธ ๋ซํ.
6) ์ต์ข ์ฐ์ฐ ์ ๊น์ง ์ค๊ฐ ์ฐ์ฐ์ด ์ํ๋์ง ์์. (์ง์ฐ๋ ์ฐ์ฐ)
IntStream intStream = new Random().ints(1,46); //1~45 ๋ฒ์์ ๋ฌดํ ์คํธ๋ฆผ
intStream.distinct().limit(6).sorted() //์ค๊ฐ ์ฐ์ฐ
.forEach(i->System.out.print(i+",")); //์ต์ข
์ฐ์ฐ
7) ๊ธฐ๋ณธํ ์คํธ๋ฆผ - IntStream, LongStream, DoubleStream....etc.
- ์คํ ๋ฐ์ฑ & ์ธ๋ฐ์ฑ์ ๋นํจ์จ์ด ์ ๊ฑฐ๋จ. (Stream<Integer> ๋์ IntStream ์ฌ์ฉํ๋ฉด ์ฑ๋ฅ์ด ๋ ์ข์์ง)
- ์ซ์์ ๊ด๋ จ๋ ์ ์ฉํ ๋ฉ์๋๋ฅผ Stream<T>๋ณด๋ค ๋ ๋ง์ด ์ ๊ณตํจ.
3. ์คํธ๋ฆผ ๋ง๋๋ ๋ฒ
1) ์คํธ๋ฆผ ๋ง๋ค๊ธฐ
2) ์ค๊ฐ ์ฐ์ฐ (0~3๋ฒ๊น์ง)
- ์ฐ์ฐ ๊ฒฐ๊ณผ๊ฐ ์คํธ๋ฆผ์ธ ์ฐ์ฐ. ๋ฐ๋ณต์ ์ผ๋ก ์ฌ์ฉ ๊ฐ๋ฅ.
- ์ค๊ฐ ์ฒ๋ฆฌ์์๋ ๋งคํ, ํํฐ๋ง, ์ ๋ ฌ ์ํ.
3) ์ต์ข
์ฐ์ฐ (0~1๋ฒ๊น์ง)
- ์ฐ์ฐ ๊ฒฐ๊ณผ๊ฐ ์คํธ๋ฆผ์ด ์๋ ์ฐ์ฐ. ๋จ ํ ๋ฒ๋ง ์ ์ฉ ๊ฐ๋ฅ (์คํธ๋ฆผ์ ์์๋ฅผ ์๋ชจ)
- ๋ฐ๋ณต, ์นด์ดํ
, ํ๊ท , ์ดํฉ ๋ฑ์ ์ง๊ณ ์ฒ๋ฆฌ ์ํ.
4. ์คํธ๋ฆผ์ ์ข
๋ฅ
1) ์๋ฐ8๋ถํฐ java.util.stream ํจํค์ง์์ ์คํธ๋ฆผ API๋ค์ ์ ๊ณตํจ.
2) ์ปฌ๋ ์
์ผ๋ก๋ถํฐ ์คํธ๋ฆผ ์ป๊ธฐ
- Collection ์ธํฐํ์ด์ค์ stream()์ผ๋ก ์ปฌ๋ ์
์ ์คํธ๋ฆผ์ผ๋ก ๋ณํ
Stream<E> stream() //Collection ์ธํฐํ์ด์ค์ ๋ฉ์๋
===์์ 1===
List<Studunt> studentList = Arrays.asList(
new Student("ํ๊ธธ๋", 10),
new Student("๊น์๋ฐ", 20)
);
Stream<Student> stream = studentList.stream();
//
===์์ 2===
List<Integer> list = Arrays.asList(1,2,3,4,5);
Stream<Integer> intStream = list.stream(); //list๋ฅผ ์คํธ๋ฆผ์ผ๋ก ๋ณํ
//์คํธ๋ฆผ์ ๋ชจ๋ ์์๋ฅผ ์ถ๋ ฅ
intStream.forEach(System.out::print); //12345
intStream.forEach(System.out::print); //์๋ฌ. ์ต์ข
์ฐ์ฐ ํ ์ผํ์ฉ์ธ ์คํธ๋ฆผ์ด ์ด๋ฏธ ๋ซํ
intStream = list.stream(); //list๋ก๋ถํฐ stream์ ๋ค์ ์์ฑํด์ ์ถ๋ ฅ ๊ฐ๋ฅ
intStream.forEach(System.out::print);
3) ๊ฐ์ฒด ๋ฐฐ์ด๋ก๋ถํฐ ์คํธ๋ฆผ ์ป๊ธฐ
Stream<T> Stream.of(T... values) //๊ฐ๋ณ ์ธ์
Stream<T> Stream.of(T[])
Stream<T> Arrays.stream(T[])
//from~to ๋ฐฐ์ด์ ๋ฒ์ ๋ง๋ค๊ธฐ (๋ง์ง๋ง to๋ ์๋ค์ด๊ฐ -1)
Stream<T> Arrays.stream(T[] array, int startInclusive, int endExclusive)
===์์===
Stream<String> strStream = Stream.of("a", "b", "c"); //๊ฐ๋ณ ์ธ์
Stream<String> strStream = Stream.of(new String[]{"a", "b", "c"});
Stream<String> strStream = Arrays.stream((new String[]{"a", "b", "c"});
Stream<String> strStream = Arrays.stream((new String[]{"a", "b", "c"}, 0, 3);
- ๊ธฐ๋ณธํ ๋ฐฐ์ด๋ก๋ถํฐ ์คํธ๋ฆผ ์ป๊ธฐ
IntStream IntStream.of(int... values) //Stream ์ด ์๋ IntStream
IntStream IntStream.of(int[])
IntStream Arrays.stream(int[])
IntStream Arrays.stream(int[] array, int startInclusive, int endExclusive)
4) ํน์ ๋ฒ์์ ์ ์๋ฅผ ์์๋ก ๊ฐ๋ ์คํธ๋ฆผ ์ป๊ธฐ (IntStream, LongStream)
// rangeClosed() : ์ฒซ๋ฒ์งธ ์ธ์๊ฐ์์ ๋๋ฒ์งธ ์ธ์๊ฐ๊น์ง ์์ฐจ์ ์ผ๋ก IntStream ๋ฆฌํด (๋ ์ซ์ ํฌํจ ์ํจ)
IntStream IntStream.range(int begin, int end)
// rangeClosed() : ์ฒซ๋ฒ์งธ ์ธ์๊ฐ์์ ๋๋ฒ์งธ ์ธ์๊ฐ๊น์ง ์์ฐจ์ ์ผ๋ก IntStream ๋ฆฌํด (๋ ์ซ์ ํฌํจ)
IntStream IntStream.rangeClosed(int begin, int end)
IntStream intStream = IntStream.range(1, 5); // 1,2,3,4
IntStream intStream = IntStream.rangeClosed(1, 5); // 1,2,3,4,5
5) ๋์๋ฅผ ์์๋ก ๊ฐ๋ ์คํธ๋ฆผ ์์ฑ (์์์ ์ ๋ง๋ค๊ธฐ)
IntStreamintStream = new Random().ints(); //๋ฌดํ ์คํธ๋ฆผ ์์ฑ
intStream.limit(5).forEach(System.out::println); //5๊ฐ์ ์์๋ง ์ถ๋ ฅ
IntStream intStream = new Random().ints(5); //ํฌ๊ธฐ๊ฐ 5์ธ ๋์ ์คํธ๋ฆผ ๋ฐํ
//ints(), longs(), doubles() ๋ฉ์๋
Integer.MIN_VALUE <= ints() <= Integer.MAX_VALUE
Long.MIN_VALUE <= longs() <= Long.MAX_VALUE
0.0 <= doubles() < 1.0
6) ๋๋ค์์ผ๋ก ์คํธ๋ฆผ ์ป๊ธฐ - iterate(), generate()
- ๋๋ค์์ ์์ค๋ก ํ๋ ๋ฌดํ ์คํธ๋ฆผ ์์ฑ
static <T> Stream<T> iterate(T seed, UnaryOperator<T> f) //์ด์ ์์์ ์ข
์์
static <T> Stream<T> generate(Supplier<T> s) //์ด์ ์์์ ๋
๋ฆฝ์
//iterate(T seed, UnaryOperator f) ๋จํญ ์ฐ์ฐ์
Stream<Integer> intStream = Stream.iterate(1, n -> n+2); //ํ์ ์ถ๋ ฅ
Stream<Integer> intStream2 = Stream.iterate(0, n -> n+2); //์ง์ ์ถ๋ ฅ
intStream.limit(10).forEach(System.out::println);
intStream2.limit(10).forEach(System.out::println);
//generate(Supplier s) : ์
๋ ฅ๋ง ์๊ณ , ์ถ๋ ฅ๋ง ์์
Stream<Integer> oneStream = Stream.generate(()->1);
oneStream.limit(10).forEach(System.out::println);
7) ํ์ผ๋ก๋ถํฐ ์คํธ๋ฆผ ์ป๊ธฐ
- ํ์ผ์ ์์ค๋ก ํ๋ ์คํธ๋ฆผ ์์ฑํ๊ธฐ
// ํ์ผ์ ๊ฒฝ๋ก ์ ๋ณด๋ฅผ ๊ฐ์ง๊ณ ์๋ Path ๊ฐ์ฒด ์์ฑ
Path path = Paths.get("src/sec02/stream_kind/lineddata.txt");
// Charset.defaultCharset()๋ ์ด์์ฒด์ ์ ๊ธฐ๋ณธ ๋ฌธ์์
Stream<String> stream = Files.lines(path, Charset.defaultCharset());
// BufferedReader์ lines() ์ด์ฉ
File file = path.toFile();
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
stream = br.lines();
8) ๋น์ด์๋ ์คํธ๋ฆผ ๋ง๋ค๊ธฐ
9) ๋๋ ํ ๋ฆฌ๋ก๋ถํฐ ์คํธ๋ฆผ ์ป๊ธฐ
Path path = Paths.get("C:/JavaProgramming/source");
Stream<Path> stream = Files.list(path);
stream.forEach( p -> System.out.println(p.getFileName()));
5. ์คํธ๋ฆผ ํ์ดํ๋ผ์ธ
1) ๋ฆฌ๋์
- ๋๋์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณตํด์ ์ถ์ํ๋ ๊ฒ.
- ์) ๋ฐ์ดํฐ์ ํฉ๊ณ, ํ๊ท , ์ต๋๊ฐ, ์นด์ดํ
๋ฑ
- ์ปฌ๋ ์
์ ์์๋ฅผ ๋ฆฌ๋์
์ ๊ฒฐ๊ณผ๋ฌผ๋ก ๋ฐ๋ก ์ง๊ณํ ์ ์๋ ๊ฒฝ์ฐ ๋ฆฌ๋์
์ด ์ง๊ณํ๊ธฐ ์ข๋๋ก ํํฐ๋ง, ๋งคํ, ์ ๋ ฌ, ๊ทธ๋ฃนํ ๋ฑ ์ค๊ฐ ์ฒ๋ฆฌ ํ์
2) ์ค๊ฐ์ฒ๋ฆฌ์ ์ต์ข
์ฒ๋ฆฌ
- ์คํธ๋ฆผ์ ๋ฐ์ดํฐ์ ์ค๊ฐ ์ฒ๋ฆฌ์ ์ต์ข
์ฒ๋ฆฌ๋ฅผ ํ์ดํ๋ผ์ธ(์ฌ๋ฌ ๊ฐ์ ์คํธ๋ฆผ์ด ์ฐ๊ฒฐ๋ ๊ตฌ์กฐ)๋ก ํด๊ฒฐ. ์ต์ข
์ฒ๋ฆฌ ์์ ์ ๊น์ง ์ค๊ฐ ์ฒ๋ฆฌ๋ ์ง์ฐ๋จ.
์) ํ์ ์ปฌ๋ ์ ์์ ๋จ์ ํ๊ท ๋์ด ์ง๊ณํ๊ธฐ
double ageAvg = list.stream() // ์ค๋ฆฌ์ง๋ ์คํธ๋ฆผ
.filter(m -> m.getSex() == Member.MALE) // ์ค๊ฐ ์ฒ๋ฆฌ ์คํธ๋ฆผ
.mapToInt(Member :: getAge) // ์ค๊ฐ ์ฒ๋ฆฌ ์คํธ๋ฆผ
.average() // ์ต์ข
์ฒ๋ฆฌ
.getAsDouble
6. ์คํธ๋ฆผ์ ์ฐ์ฐ - ์ค๊ฐ ์ฐ์ฐ ๋ฉ์๋
1) ํํฐ๋ง
- ์์๋ค์ ๊ฑธ๋ฌ๋ด๋ ์ญํ ์ ํ๋ฉฐ distinct(), filter()๋ ๋ชจ๋ ์คํธ๋ฆผ์ด ๊ฐ์ง๊ณ ์๋ ๋ฉ์๋.
2) ๋งคํ
- ์คํธ๋ฆผ์ ์์๋ฅผ ๋ค๋ฅธ ์์๋ก ๋์ฒดํ๋ ์์
.
- flatMap() : ์์๋ฅผ ๋์ฒดํ๋ ๋ณต์ ๊ฐ์ ์์๋ค๋ก ๊ตฌ์ฑ๋ ์๋ก์ด ์คํธ๋ฆผ ๋ฆฌํด. ์คํธ๋ฆผ์ ์คํธ๋ฆผ์ ์คํธ๋ฆผ์ผ๋ก ๋ณํ.
List<String> inputList1 = Arrays.asList("java8 lamda", "stream mapping");
inputList1.stream().flatMap(data -> Arrays.stream(data.split(" ")));
List<String> inputList2 = Arrays.asList("10, 20, 30", "40, 50, 60");
inputList2.stream()
.flatMapToInt(data -> {
String strArr = data.split(",");
int intArr = new int[strArr,length];
for(int i = 0; strArr > i; i++){
intArr[i] = Integer.parseInt(strArr[i].trim());
}
return Arrays.stream(intArr);
})
- mapXXX() : ์์๋ฅผ ๋์ฒดํ๋ ์์๋ค๋ก ๊ตฌ์ฑ๋ ์๋ก์ด ์คํธ๋ฆผ์ ๋ฆฌํด
List<Studunt> studentList = Arrays.asList(
new Student("ํ๊ธธ๋", 10),
new Student("๊น์๋ฐ", 20)
);
studentList.stream().mapToInt(Student :: getScore);
- asDoubleStream(),asLongStream() : IntStream์ int ์์ ๋๋ LongStream์ Long ์์๋ฅผ double ์์๋ก ํ์
๋ณํํด์ DoubleStream์ ์์ฑํ๋ค.
- boxed() : int, double ์์๋ฅผ Integer, Double ์์๋ก ๋ฐ์ฑํด์ Stream์ ์์ฑํ๋ค.
3) ์ ๋ ฌ
- ์์๊ฐ ์ต์ข
์ฒ๋ฆฌ๋๊ธฐ ์ ์ ์ค๊ฐ ๋จ๊ณ์์ ์์๋ฅผ ์ ๋ ฌ.
- ๊ฐ์ฒด ์์์ผ ๊ฒฝ์ฐ ํด๋์ค๊ฐ Comparable์ ๊ตฌํํ์ง ์์ผ๋ฉด sorted()๋ฅผ ํธ์ถํ์ ๋ ์์ธ๊ฐ ๋ฐ์.
- ๊ธฐ๋ณธ ๋น๊ต ๋ฐฉ๋ฒ์ผ๋ก ์ ๋ ฌํ๊ณ ์ถ์ ๋
sorted();
sorted( (a,b) -> a.compareTo(b) );
sorted(Comparator.naturalOrder());
- ๊ธฐ๋ณธ ๋น๊ต ๋ฐฉ๋ฒ๊ณผ ์ ๋ฐ๋ ๋ฐฉ๋ฒ์ผ๋ก ์ ๋ ฌํ๊ณ ์ถ์ ๋
sorted( (a,b) -> b.compareTo(a) );
sorted( Comparator.reversOrder() );
7. ์คํธ๋ฆผ์ ์ฐ์ฐ - ์ต์ข
์ฐ์ฐ
1) ๋งค์นญ
- allMatch() : ๋ชจ๋ ์์๋ค์ด ๋งค๊ฐ๊ฐ์ผ๋ก ์ฃผ์ด์ง Predicate์ ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง ์กฐ์ฌ
- anyMatch() : ์ต์ํ 1๊ฐ์ ์์๊ฐ ๋งค๊ฐ๊ฐ์ผ๋ก ์ฃผ์ด์ง Predicate์ ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง ์กฐ์ฌ
- noneMatch() : ๋ชจ๋ ์์๋ค์ด ๋งค๊ฐ๊ฐ์ผ๋ก ์ฃผ์ด์ง Predicate์ ์กฐ๊ฑด์ ๋ง์กฑํ์ง ์๋์ง ์กฐ์ฌ
2) Optional<T>
- T ํ์
๊ฐ์ฒด์ ๋ํผ ํด๋์ค.
- ๋ชจ๋ ์ข
๋ฅ์ ๊ฐ์ฒด๋ฅผ ์ ์ฅํ ์ ์์. (null ๋ ๊ฐ๋ฅ)
- ์ง๊ณ ๊ฐ์ ์ ์ฅํ๋ฉฐ ์ง๊ณ ๊ฐ์ด ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ ๋ํดํธ ๊ฐ์ ์ค์ ํ ์ ์์.
- ์ง๊ณ ๊ฐ์ ์ฒ๋ฆฌํ๋ Consumer๋ ๋ฑ๋กํ ์ ์์.
- OptionalXXX ํ์
์์ ๊ฐ์ ์ป์ผ๋ ค๋ฉด get(), getAsXXX()๋ฅผ ํธ์ถ.
// isPresent()
OptionalDoble optional = list.stream().mapToInt(Integer :: intValue).average();
if(optional.isPresent()){
~
}else{
~
}
// orElse()
double avg = list.stream().mapToInt(Integer :: intValue).average().orElse(0.0);
// ifPresent() : ๊ฐ์ด ์์ ๊ฒฝ์ฐ ๋๋ค์ ์คํ
list.stream().mapToInt(Integer :: intValue).average().ifPresent(a -> ~);
3) ์ปค์คํ
์ง๊ณ
- reduce() : ๋ค์ํ ์ง๊ณ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋๋ ๋ฉ์๋
// ์์๊ฐ ์์ ๊ฒฝ์ฐ NoSuchElementException ๋ฐ์
int sum = studentList.stream()
.map(Student :: getScore)
.reduce((a,b) -> a + b)
.get();
// ์์๊ฐ ์์ด๋ default ๊ฐ 0 ๋ฆฌํด
int sum = studentList.stream()
.map(Student :: getScore)
.reduce(0, (a,b) -> a + b)
.get();
4) ์์ง
- collect() : ์์๋ฅผ ํํฐ๋ง, ๋งคํํ ํ ์ปฌ๋ ์
์ ์์งํ๋ ์ต์ข
์ฒ๋ฆฌ ๋ฉ์๋
// ๋งค๊ฐ๊ฐ์ธ Collector๋ ์ด๋ค ์์๋ฅผ ์์งํ ๊ฒ์ธ์ง ๊ฒฐ์
// ํ์
ํ๋ผ๋ฏธํฐ์ T๋ ์์, A๋ ๋์ ๊ธฐ, R์ ์์๊ฐ ์ ์ฅ๋ ์ปฌ๋ ์
R(๋ฆฌํดํ์
) | collect(Collector<T,A,R> collector) | Stream(์ธํฐํ์ด์ค)
- Collector์ ๊ตฌํ ๊ฐ์ฒด๋ฅผ ์ป์ ์ ์๋ Collectors์ ์ ์ ๋ฉ์๋
- ์ฌ์ฉ์ ์ ์ ์ปจํ ์ด๋์ ์์ง
1) ์ฒซ ๋ฒ์งธ Supplier๋ ์์๋ค์ด ์์ง๋ ์ปจํ
์ด๋ ๊ฐ์ฒด๋ฅผ ์์ฑ.
- ์์ฐจ ์ฒ๋ฆฌ(์ฑ๊ธ ์ค๋ ๋) ์คํธ๋ฆผ : ํ๋ฒ ์คํ, ํ๋์ ์ปจํ
์ด๋ ๊ฐ์ฒด ์์ฑ
- ๋ณ๋ ฌ ์ฒ๋ฆฌ(๋ฉํฐ ์ค๋ ๋) ์คํธ๋ฆผ : ์ฌ๋ฌ๋ฒ ์คํ, ์ค๋ ๋ ๋ณ๋ก ์ฌ๋ฌ ๊ฐ ์์ฑ -> ์ต์ข
์ ์ผ๋ก ํ๋์ ์ปจํ
์ด๋ ๊ฐ์ฒด๋ก ๊ฒฐ
2) ๋๋ฒ ์งธ XXXConsumer๋ ์ปจํ
์ด๋ ๊ฐ์ฒด(R)์ ์์(T)๋ฅผ ์์ง.
- ์คํธ๋ฆผ์์ ์์๋ฅผ ์ปจํ
์ด๋์ ์์งํ ๋๋ง๋ค ์คํ.
- ์ธ๋ฒ ์งธ XXXConsumer๋ ์ปจํ
์ด๋ ๊ฐ์ฒด(R)๋ฅผ ๊ฒฐํฉ. (๋ณ๋ ฌ ์ฒ๋ฆฌ๋ง ํธ์ถ)
// ๊ธฐ๋ณธ
Stream<Student> totalStream = totalList.stream();
Stream<Student> maleStream = totalList.filter(s -> s.getSex() == Student.Sex.MALE);
Supplier<MaleStudent> supplier = ()->new MaleStudent();
BiConsumer<MaleStudent, Student> accumulator = (ms, s) -> ms.accumulate(s);
BiConsumer<MaleStudent, MaleStudent> combiner = (ms1, ms2) -> ms1.combine(ms2);
MaleStudent maleStudent = maleStream.collect(supplier, accumulator, combiner);
// ๋ณ์ ์๋ต
MaleStudent maleStudent = totalList.stream()
.filter(s->s.getSex() == Student.Sex.MALE)
.collect(
() -> new MaleStudent(),
(r, t) -> r.accumulate(t),
(r1, r2) -> r1.combine(r2)
);
// ๋๋ค์ ๋ฉ์๋ ์ฐธ์กฐ
MaleStudent maleStudent = totalList.stream()
.filter(s -> s.getSex() == Student.Sex.MALE)
.collect(MaleStudent :: new
, MaleStudent :: accumulate
, MaleStudent :: combine);
- ์์๋ฅผ ๊ทธ๋ฃนํํด์ ์์ง
- collect()๋ ์ปฌ๋ ์
์ ์์๋ค์ ๊ทธ๋ฃนํํด์ Map ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ธฐ๋ฅ๋ ์ ๊ณต
Map<Student.Sex, List<Student>> mapBySex = totalList.stream()
.collect(Collectors.groupingBy(student :: getSex));
- ๊ทธ๋ฃนํ ํ ๋งคํ ๋ฐ ์ง๊ณ
- ๊ทธ๋ฃนํ ํ ๋งคํ, ์ง๊ณ๋ฅผ ํ ์ ์๋๋ก ๋ ๋ฒ์งธ ๋งค๊ฐ๊ฐ์ผ๋ก Collector๋ฅผ ๊ฐ์ง ์ ์์.
Map<Student.Sex, Double> mapBySex = totalList.stream()
.collect(
Collectors.groupingBy{
Student :: getSex,
Collectors.averagingDouble(Student :: getScore)
}
);
8. ๋ณ๋ ฌ ์ฒ๋ฆฌ
1) ๋ณ๋ ฌ ์ฒ๋ฆฌ : ๋ฉํฐ ์ฝ์ด CPU ํ๊ฒฝ์์ ํ๋์ ์์
์ ๋ถํ ํด์ ๊ฐ๊ฐ์ ์ฝ์ด๊ฐ ๋ณ๋ ฌ์ ์ผ๋ก ์ฒ๋ฆฌํ๋ ๊ฒ
2) ๋ชฉ์ : ์์
์ฒ๋ฆฌ ์๊ฐ ๊ฐ์
3) ๋์์ฑ(Concurrency)๊ณผ ๋ณ๋ ฌ์ฑ(Parallelism)
- ๋์์ฑ : ๋ฉํฐ ์์
์ ์ํด ๋ฉํฐ ์ค๋ ๋๊ฐ ๋ฒ๊ฐ์๊ฐ๋ฉฐ ์คํํ๋ ์ฑ์ง
- ๋ณ๋ ฌ์ฑ : ๋ฉํฐ ์์
์ ์ํด ๋ฉํฐ ์ฝ์ด๋ฅผ ์ด์ฉํด ๋์์ ์คํํ๋ ์ฑ์ง
4) ๋ณ๋ ฌ์ฑ์ ๋ฐ์ดํฐ ๋ณ๋ ฌ์ฑ๊ณผ ์์
๋ณ๋ ฌ์ฑ๋ก ๊ตฌ๋ถํ ์ ์๋ค.
- ๋ฐ์ดํฐ ๋ณ๋ ฌ์ฑ : ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ์ชผ๊ฐ์ด ์๋ธ ๋ฐ์ดํฐ๋ก ๋ง๋ค๊ณ , ์๋ธ ๋ฐ์ดํฐ๋ค์ ๋ณ๋ ฌ ์ฒ๋ฆฌํด์ ์์
์ ๋น ๋ฅด๊ฒ ๋๋ด๋ ๊ฒ
→ ์๋ฐ8์ด ์ง์ํ๋ ๋ณ๋ ฌ ์คํธ๋ฆผ์ ๋ฐ์ดํฐ ๋ณ๋ ฌ์ฑ์ ๊ตฌํํ ๊ฒ
→ ๋ฉํฐ ์ฝ์ด์ ์๋งํผ ๋์ฉ๋ ์์๋ฅผ ์๋ธ ์์๋ก ๋๋๊ณ , ๊ฐ๊ฐ์ ์๋ธ ์์๋ค์ ๋ถ๋ฆฌ๋ ์ค๋ ๋์์ ๋ณ๋ ฌ์ฒ๋ฆฌ ์ํด
- ์์
๋ณ๋ ฌ์ฑ : ์๋ก ๋ค๋ฅธ ์์
์ ๋ณ๋ ฌ ์ฒ๋ฆฌํ๋ ๊ฒ
→ ์) ์น ์๋ฒ, ๊ฐ ๋ธ๋ผ์ฐ์ ์์ ์์ฒญํ ๊ฒ์ ๊ฐ๋ณ ์ค๋ ๋์์ ๋ณ๋ ฌ๋ก ์ฒ๋ฆฌ
9. ํฌํฌ์กฐ์ธ(ForkJoin) ํ๋ ์์ํฌ
1) ๋ณ๋ ฌ ์คํธ๋ฆผ์ ์ด์ฉํ๋ฉด ๋ฐํ์ ์์ ํฌํฌ์กฐ์ธ ํ๋ ์์ํฌ ๋์
2) ํฌํฌ ๋จ๊ณ : ์ ์ฒด ๋ฐ์ดํฐ๋ฅผ ์๋ธ ๋ฐ์ดํฐ๋ก ๋ถ๋ฆฌํ ํ ์๋ธ ๋ฐ์ดํฐ๋ฅผ ๋ฉํฐ ์ฝ์ด์์ ๋ณ๋ ฌ๋ก ์ฒ๋ฆฌ
3) ์กฐ์ธ ๋จ๊ณ : ์๋ธ ๊ฒฐ๊ณผ๋ฅผ ๊ฒฐํฉํด์ ์ต์ข
๊ฒฐ๊ณผ๋ฅผ ๋ธ๋ค.
- ํฌํฌ์กฐ์ธ ํ๋ ์์ํฌ๋ ์ค๋ ๋ํ์ธ ForkJoinPool์ ์ ๊ณต.
- ๊ฐ ์ฝ์ด์์ ์๋ธ ์์๋ฅผ ์ฒ๋ฆฌํ๋ ๊ฒ์ ๊ฐ๋ณ ์ค๋ ๋๊ฐ ํด์ผ ํ๋ฏ๋ก ์ค๋ ๋ ๊ด๋ฆฌ ํ์
10. ๋ณ๋ ฌ ์คํธ๋ฆผ ์์ฑ
1) parallelStream()์ ์ปฌ๋ ์
์ผ๋ก๋ถํฐ ๋ณ๋ ฌ ์คํธ๋ฆผ์ ๋ฐ๋ก ๋ฆฌํด.
2) parallel()๋ ์์ฐจ ์ฒ๋ฆฌ ์คํธ๋ฆผ์ ๋ณ๋ ฌ ์ฒ๋ฆฌ ์คํธ๋ฆผ์ผ๋ก ๋ณํํด์ ๋ฆฌํด.
3 ๋ณ๋ ฌ ์ฒ๋ฆฌ ์ฑ๋ฅ
- ์์์ ์์ ์์๋น ์ฒ๋ฆฌ ์๊ฐ
→ ์ปฌ๋ ์
์ ์์์ ์๊ฐ ์ ๊ณ ๊ฐ ์์์ ์ฒ๋ฆฌ ์๊ฐ์ด ์งง์ผ๋ฉด ์์ฐจ ์ฒ๋ฆฌ๊ฐ ๋ ๋น ๋ฅผ ์ ์์.
→ ๋ณ๋ ฌ ์ฒ๋ฆฌ๋ ์ค๋ ๋ํ ์์ฑ, ์ค๋ ๋ ์์ฑ์ด๋ผ๋ ์ถ๊ฐ์ ์ธ ๋น์ฉ ๋ฐ์.
4) ์คํธ๋ฆผ ์์ค์ ์ข
๋ฅ
- ArrayList, ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ก ์์๋ฅผ ๊ด๋ฆฌํ๊ธฐ ๋๋ฌธ์ ์ฝ๊ฒ ๋ถ๋ฆฌํ ์ ์์ด ๋ณ๋ ฌ ์ฒ๋ฆฌ ์๊ฐ์ด ์ ์ฝ๋๋ค.
- HashSet, TreeSet, LinkedList๋ ์์ ๋ถ๋ฆฌ๊ฐ ์ฝ์ง ์์ ์๋์ ์ผ๋ก ๋๋ฆฌ๋ค.
- ์ฝ์ด์ ์ : ์ฑ๊ธ ์ฝ์ด์ผ ๊ฒฝ์ฐ ์์ฐจ ์ฒ๋ฆฌ๊ฐ ๋น ๋ฅด๋ค. ๋ณ๋ ฌ ์คํธ๋ฆผ ์ฌ์ฉ ์ ์ค๋ ๋์ ์๋ง ์ฆ๊ฐํ๊ณ ๋์์ฑ ์์
์ผ๋ก ์ฒ๋ฆฌ๋์ด ์ข์ง ์๋ค.
์ฐธ๊ณ : [ํ๋น๋ฏธ๋์ด] ์ด๊ฒ์ด ์๋ฐ๋ค (์ ์ฉ๊ถ์ Java ํ๋ก๊ทธ๋๋ฐ ์ ๋ณต) Chapter 16.์คํธ๋ฆผ
์ฐธ๊ณ : [๋์ฐ์ถํ] JAVA์ ์ ์(3ND EDITION)-์๋ฐ์ ์ ์ ์ต์ Java 8.0 ํฌํจ Chapter 14.๋๋ค์ ์คํธ๋ฆผ
'TIL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
ํ ํฐ ๊ธฐ๋ฐ ์ธ์ฆ (22.10.31 TIL) (0) | 2022.10.31 |
---|---|
Spring - @Bean, @Component (22.10.30 TIL) (0) | 2022.10.30 |
๋ฏธ๋ ๊ทธ๋ ค๋ณด๊ธฐ (22.10.28 TIL) (0) | 2022.10.28 |
findById vs getReferenceById ์ฐจ์ด(22.10.27 TIL) (0) | 2022.10.27 |
Java ์ ๋ค๋ฆญ์ค (Generics) (22.10.26 TIL) (0) | 2022.10.26 |
๋๊ธ