Java8 Stream

chamingยท2021๋…„ 12์›” 12์ผ
0

JAVA

๋ชฉ๋ก ๋ณด๊ธฐ
3/4

Epilogue๐Ÿ™Žโ€โ™€๏ธ
๋‚˜๋Š” ๋Œ€ํ•™๊ต๋•Œ๋„ Java8์ดํ›„์˜ ์ถ”๊ฐ€๋œ ์‚ฌํ•ญ์„ ๋ฐฐ์šฐ์ง„ ๋ชปํ–ˆ๊ณ ,
์‹ค๋ฌด๋Š” ํ•ญ.์ƒ. legacyํ™˜๊ฒฝ์˜ ์ฝ”๋“œ๋ฅผ ๋งŽ์ด ์ ‘ํ•˜์—ฌ์„œ ์‚ฌ์‹ค Lambda๋‹ˆ Stream์— ๋Œ€ํ•œ ํ™œ์šฉ์ฝ”๋“œ๋ฅผ ๋ณธ ์ ๋„ ๋ณ„๋กœ์—†๊ณ , ์‚ฌ์‹ค ๊ทธ๋ƒฅ List for loop๋Œ๋ ค๋„ ๋ฌธ์ œ๊ฐ€ ์—†์—ˆ์œผ๋‹ˆ๊นŒ, ๋‚˜์ค‘์— ๊ณต๋ถ€ํ•ด์•ผ์ง€ ํ•˜๊ณ  ๋ฏธ๋ฃจ๋˜ ๊ณต๋ถ€๋ฅผ ๋Šฆ์—ˆ์ง€๋งŒ ์ด์ œ๋ผ๋„ ์‹œ์ž‘ํ•ด๋ณธ๋‹ค,,,

์ŠคํŠธ๋ฆผ์˜ ํŠน์ง•

๐Ÿ‘‰๋žŒ๋‹ค์‹์œผ๋กœ ์ฒ˜๋ฆฌ ์ฝ”๋“œ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
List<Student> list = Arrays.asList(
    new Student("์ฐจ๋ฏผํ˜œ",100), new Student("์—˜์‚ฌ", 99)
);

// java8 ์ด์ „ iterator๋ฅผ ํ†ตํ•œ for loop
for(Student student : list){
    System.out.println(student.getName() +  "score : " + student.getScore());
}

// java8 ์ดํ›„ Stream์„ ์ด์šฉํ•œ for loop
Stream<Student> stream = list.stream();
stream.forEach(s -> {
    System.out.println(s.getName() + " ์ ์ˆ˜ : "+s.getScore());
});
๐Ÿ‘‰๋‚ด๋ถ€ ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•˜๋ฏ€๋กœ ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ๊ฐ€ ์‰ฝ๋‹ค
List<String> list = Arrays.asList(
    "ํ™๊ธธ๋™", "์œค๋‘์ค€", "๋žŒ๋‹ค์‹", "๋ฐ•๋ณ‘๋ ฌ", "๊น€์ž๋ฐ”"
);

// ์ˆœ์ฐจ์ฒ˜๋ฆฌ
Stream<String> stream = list.stream();
stream.forEach(ParallelExample :: print);		
System.out.println();
// print  >> ํ™๊ธธ๋™, ์œค๋‘์ค€, ๋žŒ๋‹ค์‹, ๋ฐ•๋ณ‘๋ ฌ, ๊น€์ž๋ฐ”

// ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ
Stream<String> parallelStream = list.parallelStream();
parallelStream.forEach(ParallelExample :: print);
// print  >> ๋žŒ๋‹ค์‹, ๊น€์ž๋ฐ”, ๋ฐ•๋ณ‘๋ ฌ, ์œค๋‘์ค€, ํ™๊ธธ๋™
๐Ÿ‘‰์ŠคํŠธ๋ฆผ์€ ์ค‘๊ฐ„์ฒ˜๋ฆฌ์™€ ์ตœ์ข… ์ฒ˜๋ฆฌ๋ฅผ ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ค‘๊ฐ„์ฒ˜๋ฆฌ :: ๋งคํ•‘, ํ•„ํ„ฐ๋ง, ์ •๋ ฌ์„ ์ˆ˜ํ–‰

  • ์ตœ์ข…์ฒ˜๋ฆฌ :: ๋ฐ˜๋ณต, ์นด์šดํŒ…, ํ‰๊ท , ์ดํ•ฉ ๋“ฑ์˜ ์ง‘๊ณ„์ฒ˜๋ฆฌ๋ฅผ ์ˆ˜ํ–‰

List<Student> studentList = Arrays.asList(
    new Student("ํ™๊ธธ๋™", 60),
    new Student("์ด์ˆœ์‹ ", 95),
    new Student("์„ธ์ข…๋Œ€์™•", 100)
);

double avg = studentList.stream()
    // ์ค‘๊ฐ„์ฒ˜๋ฆฌ > ํ•™์ƒ๊ฐ์ฒด๋ฅผ ์ ์ˆ˜๋กœ ๋งคํ•‘
    .mapToInt(Student :: getScore)
    // ์ตœ์ข…์ฒ˜๋ฆฌ > ํ‰๊ท ์ ์ˆ˜
    .average()
    .getAsDouble();
๐Ÿ‘‰ ๋‹ค์–‘ํ•œ ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ ์ŠคํŠธ๋ฆผ ์ฝ๊ธฐ
// 1. ์ปฌ๋ ‰์…˜์œผ๋กœ๋ถ€ํ„ฐ ์ŠคํŠธ๋ฆผ ์–ป๊ธฐ
List<Student> studentList = Arrays.asList(
    new Student("ํ™๊ธธ๋™", 60),
    new Student("์ด์ˆœ์‹ ", 95),
    new Student("์„ธ์ข…๋Œ€์™•", 100)
);

Stream<Student> stream = studentList.stream();
stream.forEach(s -> System.out.println(s.getName()));

// 2. ๋ฐฐ์—ด๋กœ๋ถ€ํ„ฐ ์ŠคํŠธ๋ฆผ ์–ป๊ธฐ
// 2.1 String ๋ฐฐ์—ด
String[] strArray = {"ํ™๊ธธ๋™" ,"์ด์ˆœ์‹ ", "๊ฐ•๊ฐ์ฐฌ"};
Stream<String> stStream = Arrays.stream(strArray);
stStream.forEach(s -> System.out.println(s));

// 2-2. int ๋ฐฐ์—ด
int[] intArray = {1,23,54,5678,10};
IntStream intStream = Arrays.stream(intArray);
intStream.forEach(s -> System.out.println(s));

// 3. ์ˆซ์ž ๋ฒ”์œ„๋กœ๋ถ€ํ„ฐ ์ŠคํŠธ๋ฆผ ์–ป๊ธฐ
IntStream intRangeStream = IntStream.rangeClosed(1, 100);   // cf) range(1,100) 1~99๊นŒ์ง€
intRangeStream.forEach(a -> sum +=a);
System.out.println("sum : "+sum);

// 4. ํŒŒ์ผ๋กœ๋ถ€ํ„ฐ ์ŠคํŠธ๋ฆผ ์–ป๊ธฐ
Path path = Paths.get("src/main/java/study/stream/type/lineExample.txt");
Stream<String> fileStream ;

// 4-1. Files.lines() ๋ฉ”์†Œ๋“œ ์ด์šฉ
fileStream = Files.lines(path, Charset.defaultCharset());
fileStream.forEach(System.out :: println);
System.out.println();

// 4-2.BufferedReader.lines() ๋ฉ”์†Œ๋“œ ์ด์šฉ
File file = path.toFile();
FileReader fileReader = new FileReader(file);
BufferedReader br = new BufferedReader(fileReader);
fileStream = br.lines();
fileStream.forEach(System.out :: println);

// 5. ๋””๋ ‰ํ† ๋ฆฌ๋กœ๋ถ€ํ„ฐ ์ŠคํŠธ๋ฆผ ์–ป๊ธฐ
Path dirPath= Paths.get("D:\\IDE\\workspace");
Stream<Path> dirStream = Files.list(dirPath);
dirStream.forEach(p -> System.out.println(p.getFileName())) ;

์ŠคํŠธ๋ฆผ ํŒŒ์ดํ”„๋ผ์ธ

๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•ด์„œ ์ถ•์†Œํ•˜๋Š” ๊ฒƒ์„ ์ผ๋ฐ˜์ ์œผ๋กœ ๋ฆฌ๋•์…˜(Reduction)์ด๋ผ๊ณ  ํ•˜๋Š”๋ฐ,

๋ฐ์ดํ„ฐ์˜ ํ•ฉ๊ณ„, ํ‰๊ท ๊ฐ’, ์ตœ๋Œ€๊ฐ’, ์ตœ์†Œ๊ฐ’ ๋“ฑ์ด ๋Œ€ํ‘œ์ ์ธ ๋ฆฌ๋•์…˜์˜ ๊ฒฐ๊ณผ๋ฌผ์ด๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์ปฌ๋ ‰์…˜์˜ ์š”์†Œ๋ฅผ ๋ฆฌ๋•์…˜์˜ ๊ฒฐ๊ณผ๋ฌผ๋กœ ๋ฐ”๋กœ ์ง‘๊ณ„ ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ

-> ์ง‘๊ณ„ํ•˜๊ธฐ ์ข‹๋„๋ก ํ•„ํ„ฐ๋ง, ๋งคํ•‘, ์ •๋ ฌ, ๊ทธ๋ฃนํ•‘ ๋“ฑ์˜ ์ค‘๊ฐ„์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.

๐Ÿ‘‰๋‚จ์ž ํšŒ์›๋“ค์˜ ํ‰๊ท ๋‚˜์ด๋ฅผ ๊ตฌํ•˜๋Š” ์˜ˆ์ œ
// ๋กœ์ปฌ๋ณ€์ˆ˜ ์ƒ๋žตํ›„ ๋žŒ๋‹ค์‹ ํ‘œํ˜„
double avgAge = list.stream()               // ์˜ค๋ฆฌ์ง€๋„ ์ŠคํŠธ๋ฆผ
    .filter(s -> s.getSex() == Member.MALE) // ์ค‘๊ฐ„์ฒ˜๋ฆฌ1 : ๋‚จ์žํšŒ์›๋งŒ ํ•„ํ„ฐ๋ง
    .mapToInt(Member::getAge)               // ์ค‘๊ฐ„์ฒ˜๋ฆฌ2 : ๋‚จ์žํšŒ์› ๋‚˜์ด ํ•„ํ„ฐ๋ง
    .average()                              // ์ตœ์ข…์ฒ˜๋ฆฌ : ํ•ฉ๊ณ„๊ตฌํ•˜๊ธฐ
    .getAsDouble();
๐Ÿ‘‰์ค‘๊ฐ„ ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ์™€ ์ตœ์ข… ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ
  • ์ค‘๊ฐ„์ฒ˜๋ฆฌ : ๋ฆฌํ„ดํƒ€์ž…์ด ์ŠคํŠธ๋ฆผ

  • ์ตœ์ข…์ฒ˜๋ฆฌ : ๋ฆฌํ„ดํƒ€์ž…๋ฆฌ ๊ธฐ๋ณธํƒ€์ž…์ด๊ฑฐ๋‚˜ OptinalXXX

    ์ข…๋ฅ˜์ฒ˜๋ฆฌ๋ฆฌํ„ดํƒ€์ž…method
    ์ค‘๊ฐ„์ฒ˜๋ฆฌํ•„ํ„ฐ๋งStream
    IntStream
    LongStream
    DoubleStream
    distinct()
    filter()
    ๋งคํ•‘Stream
    IntStream
    LongStream
    DoubleStream
    flatMap()
    flatMapToInt()...
    map()
    mapToInt()...
    asDoubleStream()...
    boxed()
    ์ •๋ ฌStream
    IntStream
    LongStream
    DoubleStream
    sorted()
    ๋ฃจํ•‘Stream
    IntStream
    LongStream
    DoubleStream
    peek()
    ์ตœ์ข…์ฒ˜๋ฆฌ๋งค์นญBooleanallMatch()
    anyMatch()
    noneMatch()
    ์ง‘๊ณ„long
    OptinalXXX
    count()
    findFirst() / max() / min() / average() / reduce()/ sum()
    ๋ฃจํ•‘voidforEach()
    ์ˆ˜์ง‘Rcollect()

ํ•„ํ„ฐ๋ง(distinct() , filter())

ํ•„ํ„ฐ๋ง์€ ์ค‘๊ฐ„ ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์œผ๋กœ ์š”์†Œ๋ฅผ ๊ฑธ๋ž˜๋Š” ์—ญํ• 

  • distinct() : ์ค‘๋ณต์ œ๊ฑฐ
  • filter() : ๋งค๊ฐœ๊ฐ’์œผ๋กœ ์ฃผ์–ด์ง„ Predicate๊ฐ€ true๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ์š”์†Œ๋งŒ ํ•„ํ„ฐ๋ง
// ์ค‘๋ณต์ œ๊ฑฐ
Stream<String> stream = list.stream().distinct();
stream.forEach(System.out :: print);

System.out.println();

// ์„ฑ์ด '์‹ '์”จ๋ฉด์„œ ์ค‘๋ณต์ œ๊ฑฐ
Stream<String> sinStream = list.stream()
                                .distinct()
                                .filter(s-> s.startsWith("์‹ "));
sinStream.forEach(System.out :: print);

๋งคํ•‘(flatMapXXX(), mapXXX(), asXXXStream(), boxed())

๋งคํ•‘์€ ์ค‘๊ฐ„ ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์œผ๋กœ ์ŠคํŠธ๋ฆผ์˜ ์š”์†Œ๋ฅผ ๋‹ค๋ฅธ์š”์†Œ๋กœ ๋Œ€์ฒดํ•˜๋Š” ์ž‘์—…์ด๋‹ค.

๐Ÿ‘‰flatMapXXX() ๋ฉ”์†Œ๋“œ

  • ์š”์†Œ๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ๋ณต์ˆ˜๊ฐœ์˜ ์š”์†Œ๋“ค๋กœ ๊ตฌ์„ฑ๋œ ์ƒˆ๋กœ์šด ์ŠคํŠธ๋ฆผ์„ ๋ฆฌํ„ด
List<String> inputList = Arrays.asList("java8 lambda", "stream mapping");

// ๋ณต์ˆ˜๊ฐœ์˜ ์š”์†Œ๋กœ ๋Œ€์ฒด
inputList.stream()
  	  	.flatMap(data -> Arrays.stream(data.split(" ")))
    	.forEach(System.out :: println);
// result > 
java8
lambda
stream
mapping


// String ๋ฐฐ์—ด์„ , ๋‹จ์œ„๋กœ ์งœ๋ฅธ ํ›„ int[]๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
List<String> inputList2 = Arrays.asList("1,2,3","4,5,67","8,9,20");
inputList2.stream()
        .flatMapToInt(data-> {
            String[] strArr = data.split(",");
            int[] intArr = new int[strArr.length];
            for(int i=0;i<intArr.length;i++){
                intArr[i] = Integer.parseInt(strArr[i].trim());
            }
            return Arrays.stream(intArr);
        })
        .forEach(System.out::println);
// result >
1
2
3
4
5
67
8
9
20

๐Ÿ‘‰mapXXX() ๋ฉ”์†Œ๋“œ

  • ์š”์†Œ๋ฅผ ๋Œ€์ฒดํ•˜๋Š” ์š”์†Œ๋กœ ๊ตฌ์„ฑ๋œ ์ƒˆ๋กœ์šด ์ŠคํŠธ๋ฆผ์„ ๋ฆฌํ„ด
List<Student> studentList = Arrays.asList(
                new Student("ํ™๊ธธ๋™", 10),
                new Student("๊น€๋‚˜๋‚˜", 20),
                new Student("์ฐจ์•ˆ๋‚˜", 30)
        );

studentList.stream()
    .mapToInt(s-> s.getScore())
    .forEach(System.out :: println);

๐Ÿ‘‰asDoubleStream(), asLongStream(), boxed() ๋ฉ”์†Œ๋“œ

  • asDoubleStream() : IntStream์˜ int์š”์†Œ

โ€‹ LongStream์˜ ong ์š”์†Œ๋ฅผ double์š”์†Œ๋กœ ํƒ€์ž…๋ณ€ํ™˜ํ•ด์„œ DoubleStream์„ ์ƒ์„ฑ

  • asLongStream() : IntStream์˜ int์š”์†Œ๋ฅผ long์š”์†Œ ํƒ€์ž…์œผ๋กœ ํƒ€์ž…๋ณ€ํ™˜ํ•˜์—ฌ LongStream์„ ์ƒ์„ฑ
  • boxed() : int,long,double ์š”์†Œ๋ฅผ Integer, Long, Double์š”์†Œ๋กœ ๋ฐ•์‹ฑํ•ด์„œ Stream์„ ์ƒ์„ฑ
int[] intArray = {1,2,3,4,5};

// int -> double ๋กœ ๋ณ€ํ™˜
IntStream stream = Arrays.stream(intArray);
stream.asDoubleStream().forEach(System.out :: println);

// boxed (int -> Integer)
stream = Arrays.stream(intArray);
stream
    .boxed()
    .forEach(System.out :: println);

โ€‹

์ •๋ ฌ(sorted())

์ŠคํŠธ๋ฆผ์€ ์š”์†Œ๊ฐ€ ์ตœ์ข… ์ฒ˜๋ฆฌ๋˜๊ธฐ ์ „์— ์ค‘๊ฐ„ ๋‹จ๊ณ„์—์„œ ์š”์†Œ๋ฅผ ์ •๋ ฌํ•ด์„œ ์ตœ์ข… ์ฒ˜๋ฆฌ ์ˆœ์„œ๋ฅผ ๋ณ€๊ฒฝ

โ€ป๊ฐ์ฒด ์š”์†Œ์ผ ๊ฒฝ์šฐ์—๋Š” ํด๋ž˜์Šค๊ฐ€ Comparable์„ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์œผ๋ฉด sorted()๋ฉ”์†Œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ClassCastException์ด ๋ฐœ์ƒํ•œ๋‹ค. ๋”ฐ๋ผ์„œ Comparable์ด ๊ตฌํ˜„๋œ ์š”์†Œ์—์„œ ์‚ฌ์šฉ๊ฐ€๋Šฅํ•˜๋‹ค.

๐Ÿ‘‰Comparable ๊ตฌํ˜„ ํด๋ž˜์Šค

public class Student implements Comparable<Student> {
    private String name;
    private int score;

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getScore() {
        return score;
    }

    @Override
    public int compareTo(Student o) {
        return Integer.compare(score, o.getScore());
    }
}
๐Ÿ‘‰sorted()๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•œ ์ •๋ ฌ
// ์ˆซ์ž ์š”์†Œ์ธ ๊ฒฝ์šฐ
IntStream intStream = Arrays.stream(new int[]{5,3,2,1,4});
intStream.sorted()
    .forEach(System.out :: println);

// ๊ฐ์ฒด ์š”์†Œ์ผ ๊ฒฝ์šฐ
List<Student> list = Arrays.asList(
    new Student("๊น€๋‚˜๋‚˜", 90),
    new Student("์—˜์‚ฌ", 99),
    new Student("์•ˆ๋‚˜", 20)
);
list.stream()
    .sorted()
    .forEach(s-> System.out.println(s.getScore()));

// ์—ญ์ˆœ
list.stream()
    .sorted(Comparator.reverseOrder())
    .forEach(s-> System.out.println(s.getScore()));

๋ฃจํ•‘(peek(), forEach())

์š”์†Œ ์ „์ฒด๋ฅผ ๋ฐ˜๋ณตํ•˜๋Š”๊ฒƒ.

  • peek() : ์ค‘๊ฐ„์ฒ˜๋ฆฌ ๋‹จ๊ณ„์˜ ๋ฉ”์†Œ๋“œ์ด๋ฏ€๋กœ ,๋ฐ˜๋“œ์‹œ ์ตœ์ข…์ฒ˜๋ฆฌ ๋‹จ๊ณ„์˜ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
  • forEach() : ์ตœ์ข…์ฒ˜๋ฆฌ ๋‹จ๊ณ„์˜ ๋ฉ”์†Œ๋“œ, ํ•ด๋‹น ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ์ดํ›„ sum()๊ฐ™์€ ์ตœ์ข…์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœํ•˜๋ฉด ์•ˆ๋œ๋‹ค
int[] intArr = {1,2,3,4,5};

// peek()๋ฅผ ๋งˆ์ง€๋ง‰์— ํ˜ธ์ถœํ•œ ๊ฒฝ์šฐ
Arrays.stream(intArr)
    .filter(a -> a% 2 == 0)
    .peek(a-> System.out.println(a));    // ๋™์ž‘ํ•˜์ง€ ์•Š์Œ

// ์ตœ์ข… ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ๋ฅผ ๋งˆ์ง€๋ง‰์— ํ˜ธ์ถœํ•œ ๊ฒฝ์šฐ
int total = Arrays.stream(intArr)
    .filter(a -> a% 2 == 0)
    .peek(a-> System.out.println(a))    // ๋™์ž‘ํ•จ
    .sum();
System.out.println("total : "+total);

// forEach()๋ฅผ ๋งˆ์ง€๋ง‰์— ํ˜ธ์ถœํ•œ ๊ฒฝ์šฐ
Arrays.stream(intArr)
    .filter(a -> a% 2 == 0)
    .forEach(System.out :: println);

๋งค์นญ(allMatch(), anyMatch(), noneMatch())

์ŠคํŠธ๋ฆผํด๋ž˜์Šค๋Š” ์ตœ์ข… ์ฒ˜๋ฆฌ ๋‹จ๊ณ„์—์„œ ์š”์†Œ๋“ค์ด ํŠน์ • ์กฐ๊ฑด์— ๋งŒ์กฑํ•˜๋Š”์ง€ ์กฐ์‚ฌํ•  ์ˆ˜ ์žˆ๋„๋ก ์ œ๊ณต

  • allMatch() : ๋ชจ๋“  ์š”์†Œ๋“ค์ด ๋งค๊ฐœ๊ฐ’์œผ๋กœ ์ฃผ์–ด์ง„ Predicate์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€
  • anyMatch() : ์ตœ์†Œํ•œ ํ•œ ๊ฐœ์˜ ์š”์†Œ๊ฐ€ ๋งค๊ฐœ๊ฐ’์œผ๋กœ ์ฃผ์–ด์ง„ Predicate์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š”์ง€
  • noneMatch() : ๋ชจ๋“  ์š”์†Œ๋“ค์ด ๋งค๊ฐœ๊ฐ’์œผ๋กœ ์ฃผ์–ด์ง„ Predicate์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š”์ง€
int[] intArr = {2,4,5};

boolean result = Arrays.stream(intArr)
    .allMatch(a -> a%2 ==0);
// print > false

boolean result1 = Arrays.stream(intArr)
    .anyMatch(a -> a%3 ==0);
// print > false

boolean result2 = Arrays.stream(intArr)
    .noneMatch(a -> a%3 ==0);
// print > false

๊ธฐ๋ณธ์ง‘๊ณ„(sum(), count(), average(), max(), min())

์ตœ์ข…์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์œผ๋กœ ์š”์†Œ๋“ค์„ ์ฒ˜๋ฆฌํ•ด์„œ ์นด์šดํŒ…, ํ•ฉ๊ณ„, ํ‰๊ท ๊ฐ’, ์ตœ๋Œ€๊ฐ’, ์ตœ์†Œ๊ฐ’๋“ฑ๊ณผ ๊ฐ™์ด ํ•˜๋‚˜์˜ ๊ฐ’์œผ๋กœ ์‚ฐ์ถœ

์ง‘๊ณ„๋Š” ๋Œ€๋Ÿ‰์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•ด์„œ ์ถ•์†Œํ•˜๋Š” ๋ฆฌ๋•์…˜(reduction)์ด๋ผ๊ณ  ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

// ๊ฐœ์ˆ˜
long count = Arrays.stream(new int[]{1,2,3,4,5})
    .filter(n-> n%2 ==0)
    .count();
// print > 2

// ํ•ฉ๊ณ„
long sum = Arrays.stream(new int[]{1,2,3,4,5})
    .filter(n-> n%2 ==0)
    .sum();
// print > 6

// ํ‰๊ท ๊ฐ’
double avg = Arrays.stream(new int[]{1,2,3,4,5})
    .filter(n-> n%2 ==0)
    .average()
    .getAsDouble();
// print > 3.0

// ์ตœ๋Œ€๊ฐ’
int max = Arrays.stream(new int[]{1,2,3,4,5})
    .filter(n-> n%2 ==0)
    .max()
    .getAsInt();
// print > 4

// ์ตœ์†Œ๊ฐ’
int min = Arrays.stream(new int[]{1,2,3,4,5})
    .filter(n-> n%2 ==0)
    .min()
    .getAsInt();
// print > 2

๐Ÿ‘‰Optinal ํด๋ž˜์Šค

๋‹จ์ˆœํžˆ ์ง‘๊ณ„ ๊ฐ’๋งŒ ์ €์žฅํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ, ์ง‘๊ณ„ ๊ฐ’์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ ๋””ํดํŠธ ๊ฐ’์„ ์„ค์ •ํ•  ์ˆ˜ ์žˆ๊ณ ,

์ง‘๊ณ„ ๊ฐ’์„ ์ฒ˜๋ฆฌํ•˜๋Š” Consumer๋„ ๋“ฑ๋กํ•  ์ˆ˜ ์žˆ๋‹ค.

List<Integer> list = new ArrayList<>();

// ์ปฌ๋ ‰์…˜์— ์š”์†Œ๊ฐ€ ์ถ”๊ฐ€๋˜์ง€ ์•Š์€ ์ƒํƒœ -> NoSuchElementException ์˜ˆ์™ธ๋ฐœ์ƒ
double avg = list.stream()
    .mapToInt(Integer :: intValue)
    .average()
    .getAsDouble();
System.out.println(avg);

// ๋ฐฉ๋ฒ•1) Optinal.isPresent() ๋ฉ”์†Œ๋“œ๋กœ ํ‰๊ท ๊ฐ’ ์—ฌ๋ถ€๋ฅผ ํ™•์ธ.
OptionalDouble optional = list.stream()
    .mapToInt(Integer::intValue)
    .average();
if(optional.isPresent()){
    System.out.println("(method1)avg : " + optional.getAsDouble());
}else {
    System.out.println("(method1)avg : 0.0");
}

// ๋ฐฉ๋ฒ•2) orElse() ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ default๊ฐ’์„ ์„ค์ •.
double avg = list.stream()
    .mapToInt(Integer::intValue)
    .average()
    .orElse(0.0);
System.out.println("(method2)avg : " + avg);

// ๋ฐฉ๋ฒ•3) ํ‰๊ท ๊ฐ’์ด ์žˆ์„ ๊ฒฝ์šฐ์—๋งŒ
list.stream()
    .mapToInt(Integer::intValue)
    .average()
    .ifPresent(a-> System.out.println("(method3) avg : "+a));

์ปค์Šคํ…€ ์ง‘๊ณ„(reduce())

๋‹ค์–‘ํ•œ ์ง‘๊ณ„ ๊ฒฐ๊ณผ๋ฌผ์„ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋„๋ก reduce()๋ฉ”์†Œ๋“œ ์ œ๊ณต

List<Student> studentList = Arrays.asList(
    new Student("๊น€๋‚˜๋‚˜", 82),
    new Student("๋‚˜๋‹ค๋‚˜", 90),
    new Student("์‹ ํšจ๋ฒ”", 57)
);

// ํ•™์ƒ๋“ค์˜ ์ด์  ๊ตฌํ•˜๊ธฐ
// ๋ฐฉ๋ฒ•1) sum()์ด์šฉ
int sum1 = studentList.stream()
    .mapToInt(Student::getScore)
    .sum();
// ๋ฐฉ๋ฒ•2) reduce(Operator) ์ด์šฉ, ์ŠคํŠธ๋ฆผ ์š”์†Œ ์—†๋Š” ๊ฒฝ์šฐ NoSuchElementException ๋ฐœ์ƒ
int sum2 = studentList.stream()
    .map(Student::getScore)
    .reduce((a,b) -> a+b)
    .get();
// ๋ฐฉ๋ฒ•3) reduce(identity, Operator) ์ด์šฉ, ์ŠคํŠธ๋ฆผ ์š”์†Œ ์—†๋Š” ๊ฒฝ์šฐ, 0.0
int sum3 = studentList.stream()
    .map(Student::getScore)
    .reduce(0, (a,b) -> a+b);

์ˆ˜์ง‘(collect())

์ŠคํŠธ๋ฆผ์€ ์š”์†Œ๋“ค์„ ํ•„ํ„ฐ๋ง ๋˜๋Š” ๋งคํ•‘ํ•œ ํ›„ ์š”์†Œ๋“ค์„ ์ˆ˜์ง‘ํ•˜๋Š” ์ตœ์ข… ์ฒ˜๋ฆฌ ๋ฉ”์†Œ๋“œ์ธ collect()๋ฅผ ์ œ๊ณต

์ด ๋ฉ”์†Œ๋“œ๋ฅผ ์ด์šฉํ•˜๋ฉด ํ•„์š”ํ•œ ์š”์†Œ๋งŒ ์ปฌ๋ ‰์…˜์œผ๋กœ ๋‹ด์„ ์ˆ˜ ์žˆ๊ณ , ์š”์†Œ๋“ค์„ ๊ทธ๋ฃนํ•„ํ•œ ํ›„ ์ง‘๊ณ„ํ•  ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ‘‰ํ•„ํ„ฐ๋งํ•œ ์š”์†Œ ์ˆ˜์ง‘
List<Student> list = Arrays.asList(
    new Student("๊น€๋‚˜๋‚˜", 10, Student.Sex.FEMALE),
    new Student("๊ฐ•์ฐฌํฌ", 35, Student.Sex.MALE),
    new Student("ํ™ฉ์ง„์ด", 98, Student.Sex.FEMALE),
    new Student("๋ฐ•์ง€์„ฑ", 50, Student.Sex.MALE)
);

// ์ „์ฒด ํ•™์ƒ ์ค‘ ๋‚จํ•™์ƒ๋งŒ ํ•„ํ„ฐ๋งํ•ด์„œ ๋ณ„๋„์˜ List ์ƒ์„ฑํ•˜๊ธฐ
Stream<Student> stream = list.stream();
Stream<Student> maleStream = stream.filter(s->s.getSex()== Student.Sex.MALE);
// List์— Student๋ฅผ ์ˆ˜์ง‘ํ•˜๋Š” Collector๋ฅผ ์–ป๊ธฐ
Collector<Student, ? , List<Student>> collector = Collectors.toList();
// Stream์— collect()๋ฉ”์†Œ๋“œ๋กœ Student๋ฅผ ์ˆ˜์ง‘ํ•ด์„œ ์ƒˆ๋กœ์šด List๋ฅผ ์–ป๋Š”๋‹ค.
List<Student> maleList = maleStream.collect(collector);

// ๋ณ€์ˆ˜ ์ƒ๋žต ๋ฒ„์ „
List<Student> maleScoreList = list.stream()
                            .filter(s-> s.getSex() == Student.Sex.MALE)
                            .collect(Collectors.toList());
maleScoreList.stream().forEach(s-> System.out.println(s.getScore()));
๐Ÿ‘‰์‚ฌ์šฉ์ž ์ •์˜ ์ปจํ…Œ์ด๋„ˆ์— ์ˆ˜์ง‘ํ•˜๊ธฐ

collect(Supplier, BiConsumer<R, ? super T>, BiConsumer<R,R>)

  • Supplier : ์š”์†Œ๋“ค์ด ์ˆ˜์ง‘๋  ์ปจํ…Œ์ด๋„ˆ(R)์„ ์ƒ์„ฑ

  • BiConsumer<R, ? super T> : ์ปจํ…Œ์ด๋„ˆ ๊ฐ์ฒด(R)์— ์š”์†Œ(T)๋ฅผ ์ˆ˜์ง‘ํ•˜๋Š” ์—ญํ• 

  • BiConsumer<R,R> : ์ปจํ…Œ์ด๋„ˆ ๊ฐ์ฒด(R)๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ์—ญํ• .

private List<Student> list;     // ์š”์†Œ๋ฅผ ์ €์žฅํ•  ์ปฌ๋ ‰์…˜

public MaleStudent(){           // ์ƒ์„ฑ์ž
    list = new ArrayList<>();
    System.out.println("["+ Thread.currentThread().getName()+"] MaleStudent");
}

public void accumulate(Student student){    //์š”์†Œ๋ฅผ ์ˆ˜์ง‘ํ•˜๋Š” ๋ฉ”์†Œ๋“œ
    list.add(student);
    System.out.println("["+ Thread.currentThread().getName()+"] accumulate()");
}

public void combine(MaleStudent other){ // ๋‘ MaleStudent๋ฅผ ๊ฒฐํ•ฉํ•˜๋Š” ๋ฉ”์†Œ๋“œ (๋ณ‘๋ ฌ์ฒ˜๋ฆฌ์—๋งŒ ํ˜ธ์ถœ)
    list.addAll(other.getList());
    System.out.println("["+ Thread.currentThread().getName()+"] combine()");
}

public List<Student> getList(){             // ์š”์†Œ๊ฐ€ ์ €์žฅ๋  ์ปฌ๋ ‰์…˜์„ ๋ฆฌํ„ดํ„ด
    return list;    
}
  • ์ŠคํŠธ๋ฆผ์—์„œ ์ฝ์€ ๋‚จํ•™์ƒ์„ MaleStudent์— ์ˆ˜์ง‘ํ•˜๊ธฐ
// 1.์ „์ฒด ํ•™์ƒ List์—์„œ Stream ์–ป๊ธฐ
Stream<Student> stream = list.stream();
// 2.๋‚จํ•™์ƒ๋งŒ ํ•„ํ„ฐ๋งํ•ด์„œ Stream ์–ป๊ธฐ
Stream<Student> maleStream = stream.filter(s->s.getSex()== Student.Sex.MALE);

// 3.MaleStudent๋ฅผ ๊ณต๊ธ‰ํ•˜๋Š” Supplier๋ฅผ ์–ป๋Š”๋‹ค.
Supplier<MaleStudent> supplier = () -> new MaleStudent();
// 4.MaleStudent์™€ Student๋ฅผ ๋งค๊ฐœ๊ฐ’์œผ๋กœ ๋ฐ›์•„์„œ MaleStudent์˜ accumulate()๋ฉ”์†Œ๋“œ๋กœ Student๋ฅผ ์ˆ˜์ง‘ํ•˜๋Š” BiConsumer๋ฅผ ์–ป๋Š”๋‹ค
BiConsumer<MaleStudent, Student> accmulator = (ms, s) -> ms.accumulate(s);
// 5. ๋‘ ๊ฐœ์˜ MaleStudent๋ฅผ ๋งค๊ฐœ๊ฐ’์œผ๋กœ ๋ฐ›์•„ combine()๋ฉ”์†Œ๋“œ๋กœ ๊ฒฐํ•ฉํ•˜๋Š” BiConsumer๋ฅผ ์–ป๋Š”๋‹ค.
BiConsumer<MaleStudent, MaleStudent> combiner = (ms1, ms2) -> ms1.combine(ms2);
// 6. supplier๊ฐ€ ์ œ๊ณตํ•˜๋Š” MaleStudent์— accumulator๊ฐ€ Student๋ฅผ ์ˆ˜์ง‘ํ•ด์„œ ์ตœ์ข… ์ฒ˜๋ฆฌ๋œ MaleStudent๋ฅผ ์–ป๋Š”๋‹ค.
MaleStudent maleStudent = maleStream.collect(supplier, accmulator, combiner);

// ๋ฉ”์†Œ๋“œ ์ฐธ์กฐ ๋ฒ„์ „
MaleStudent maleStudent2 = list.stream()
    .filter(s->s.getSex()== Student.Sex.MALE)
    .collect(MaleStudent::new, MaleStudent::accumulate, MaleStudent::combine);

maleStudent2.getList().stream()
    .forEach(s-> System.out.println(s.getName()));

// result >
[main]
[main] accumulate()
[main] accumulate()
๊ฐ•์ฐฌํฌ
๋ฐ•์ง€์„ฑ
๐Ÿ‘‰์š”์†Œ๋ฅผ ๊ทธ๋ฃนํ•‘ํ•ด์„œ ์ˆ˜์ง‘

Collector<T, ?, Map<K, List>

  • T๋ฅผ K๋กœ ๋งคํ•‘ํ•˜๊ณ , K ํ‚ค์— ์ €์žฅ๋œ List์— T๋ฅผ ์ €์žฅํ•œ Map ์ƒ์„ฑ
  • groupBy(Function<T,K> classifier)
List<Student> list = Arrays.asList(
    new Student("๊น€๋‚˜๋‚˜", 10, Student.Sex.FEMALE, Student.City.Seoul),
    new Student("๊ฐ•์ฐฌํฌ", 35, Student.Sex.MALE, Student.City.Seoul),
    new Student("ํ™ฉ์ง„์ด", 98, Student.Sex.FEMALE, Student.City.Busan),
    new Student("๋ฐ•์ง€์„ฑ", 50, Student.Sex.MALE, Student.City.Seoul)
);

/**
  *  ํ•™์ƒ๋“ค์„ ์„ฑ๋ณ„๋กœ ๊ทธ๋ฃนํ•‘ํ•˜๊ณ , ๊ฐ™์€ ๊ทธ๋ฃน์— ์†ํ•˜๋Š” ํ•™์ƒ List๋ฅผ ์ƒ์„ฑํ•œ ๋’ค,
  *  ์„ฑ๋ณ„์„ ํ‚ค๋กœ, ํ•™์ƒList๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” Map์„ ์ƒ์„ฑํ•œ๋‹ค.
  */
// 1. ์ „์ฒดํ•™์ƒ List์—์„œ Stream ์–ป๊ธฐ
Stream<Student> stream = list.stream();
// 2. Student๋ฅผ Student.Sex๋กœ ๋งคํ•‘ํ•˜๋Š” Function์„ ์–ป๋Š”๋‹ค
Function<Student, Student.Sex> classifier = Student::getSex;
// 3. Student.Sex๊ฐ€ ํ‚ค๊ฐ€ ๋˜๊ณ , ๊ทธ๋ฃนํ•‘ ๋œ List<Student>๊ฐ€ ๊ฐ’์ธ Map์„ ์ƒ์„ฑํ•˜๋Š” Collector๋ฅผ ์–ป๋Š”๋‹ค
Collector<Student, ? , Map<Student.Sex, List<Student>>> collector = Collectors.groupingBy(classifier);
// 4. Stream์˜ collect() ๋ฉ”์†Œ๋“œ๋กœ Student๋ฅผ Student.Sex๋ณ„๋กœ ๊ทธ๋ฃนํŒฝํ•ด์„œ Map์„ ์–ป๋Š”๋‹ค.
Map<Student.Sex, List<Student>> mapBySex = stream.collect(collector);

// ๋ณ€์ˆ˜ ์ œ๊ฑฐ ๋ฒ„์ „
Map<Student.Sex, List<Student>> mapBySex1 = list.stream()
    .collect(Collectors.groupingBy(Student :: getSex));

mapBySex1.get(Student.Sex.MALE).stream()
    .forEach(s-> System.out.println(s.getName()));
// result > ๊ฐ•์ฐฌํฌ ๋ฐ•์ง€์„ฑ

/**
  * ํ•™์ƒ๋“ค์„ ๊ฑฐ์ฃผ ๋„์‹œ๋ณ„๋กœ ๊ทธ๋ฃนํ•‘ํ•˜๊ณ , ๊ฐ™์€ ๊ทธ๋ฃน์— ์†ํ•˜๋Š” ํ•™์ƒ๋“ค์˜ ์ด๋ฆ„์„ List๋กœ ์ƒ์„ฑํ•œ ํ›„
  * ๊ฑฐ์ฃผ ๋„์‹œ๋ฅผ ํ‚ค๋กœ, ์ด๋ฆ„ List๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” Map์„ ์ƒ์„ฑ
  */
// 1. ์ „์ฒดํ•™์ƒ List์—์„œ Stream ์–ป๊ธฐ
Stream<Student> cityStream = list.stream();
// 2. Student๋ฅผ Student.City๋กœ ๋งคํ•‘ํ•˜๋Š” Function์„ ์–ป๋Š”๋‹ค
Function<Student, Student.City> cityClassifier = Student :: getCity;
// 3. Student๋ฅผ Student.Name๋กœ ๋งคํ•‘ํ•˜๋Š” Function์„ ์–ป๋Š”๋‹ค
Function<Student, String> mapper = Student :: getName;
// 4. Student์˜ ์ด๋ฆ„์„ List์— ์ˆ˜์ง‘ํ•˜๋Š” Collerctor๋ฅผ ์–ป๋Š”๋‹ค.
Collector<String, ? , List<String>> collector1 = Collectors.toList();
Collector<Student, ?,  List<String>> collector2 = Collectors.mapping(mapper, collector1);
// 5. Student.City๊ฐ€ ํ‚ค๊ณ  ๊ทธ๋ฃนํ•‘๋œ ์ด๋ฆ„ List๊ฐ€ ๊ฐ’์ธ Map์„ ์ƒ์„ฑํ•˜๋Š” Collector๋ฅผ ์–ป๋Š”๋‹ค.
Collector<Student, ? , Map<Student.City, List<String>>> collector3 = Collectors.groupingBy(cityClassifier, collector2);
// 6. Stream์˜ collect๋ฉ”์†Œ๋“œ๋กœ Student๋ฅผ Student.City๋ณ„๋กœ ๊ทธ๋ฃนํ•‘ํ•ด์„œ Map์„ ์–ป๋Š”๋‹ค.
Map<Student.City, List<String>> mapByCity = cityStream.collect(collector3);

// ๋ณ€์ˆ˜ ์ƒ๋žต ๋ฒ„์ „
Map<Student.City, List<String>> mapByCity2 = list.stream()
    .collect(
    Collectors.groupingBy(
        Student::getCity, Collectors.mapping(Student::getName, Collectors.toList())
    )
);
mapByCity2.get(Student.City.Busan).stream()
    .forEach(s-> System.out.println(s));
// result > ํ™ฉ์ง„์ด
๐Ÿ‘‰ ๊ทธ๋ฃนํ•‘ ํ›„ ๋งคํ•‘ ๋ฐ ์ง‘๊ณ„
/**
  * ํ•™์ƒ๋“ค์„ ์„ฑ๋ณ„๋กœ ๊ทธ๋ฃนํ•‘ํ•œ ๋‹ค์Œ ๊ฐ™์€ ๊ทธ๋ฃน์— ์†ํ•˜๋Š” ํ•™์ƒ๋“ค์˜ ํ‰๊ท  ์ ์ˆ˜๋ฅผ ๊ตฌํ•˜๊ณ ,
  * ์„ฑ๋ณ„์„ ํ‚ค๋กœ, ํ‰๊ท  ์ ์ˆ˜๋ฅผ ๊ฐ’์œผ๋กœ ๊ฐ–๋Š” Map์„ ์ƒ์„ฑํ•œ๋‹ค.
  */
Stream<Student> stream = list.stream();
Function<Student, Student.Sex> classifier = Student::getSex;
// Student๋ฅผ ์ ์ˆ˜๋กœ ๋งคํ•‘ํ•˜๋Š” ToDoubleFunction์„ ์–ป๋Š”๋‹ค
ToDoubleFunction<Student> mapper = Student::getScore;
// ํ•™์ƒ ์ ์ˆ˜์˜ ํ‰๊ท ์„ ์‚ฐ์ถœํ•˜๋Š” Collector๋ฅผ ์–ป๋Š”๋‹ค
Collector<Student, ? , Double> collector1 = Collectors.averagingDouble(mapper);
// Student.Sex๊ฐ€ ํ‚ค๊ณ , ํ‰๊ท  ์ ์ˆ˜๊ฐ€ Double์ธ ๊ฐ’์ธ Map์„ ์ƒ์„ฑํ•˜๋Š” Collector๋ฅผ ์–ป๋Š”๋‹ค.
Collector<Student, ? , Map<Student.Sex, Double>> collector2 = Collectors.groupingBy(classifier, collector1);
// Stream์˜ collect() ๋ฉ”์†Œ๋“œ๋กœ Student๋ฅผ Student.Sex๋ณ„๋กœ ๊ทธ๋ฃนํ•‘ํ•˜์—ฌ Map์„ ์–ป๋Š”๋‹ค.
Map<Student.Sex, Double> mapByScore = stream.collect(collector2);

// ๋ณ€์ˆ˜ ์ œ๊ฑฐ ๋ฒ„์ „
Map<Student.Sex, Double> map = list.stream()
    .collect(
    Collectors.groupingBy(
        Student::getSex,
        Collectors.averagingDouble(Student::getScore)
    )
);

System.out.println(map.get(Student.Sex.MALE));

๋ณ‘๋ ฌ์ฒ˜๋ฆฌ

๋™์‹œ์„ฑ(Concurrency()์™€ ๋ณ‘๋ ฌ์„ฑ(Parallelism)

๋™์‹œ์„ฑ :๋ฉ€ํ‹ฐ์ž‘์—…์„ ์œ„ํ•ด ๋ฉ€ํ‹ฐ์Šค๋ ˆ๋“œ๊ฐ€ ๋ฒˆ๊ฐˆ์•„๊ฐ€๋ฉฐ ์‹คํ–‰ํ•˜๋Š” ์„ฑ์งˆ

๋ณ‘๋ ฌ์„ฑ : ๋ฉ€ํ‹ฐ ์ž‘์—…์„ ์œ„ํ•ด ๋ฉ€ํ‹ฐ ์ฝ”์–ด๋ฅผ ์ด์šฉํ•ด์„œ ๋™์‹œ์— ์‹คํ–‰ํ•˜๋Š” ์„ฑ์งˆ

๐Ÿ‘‰๋ณ‘๋ ฌ์ŠคํŠธ๋ฆผ ์ƒ์„ฑ
// ๋ฉ”์†Œ๋“œ ์ฐธ์กฐ ๋ฒ„์ „
MaleStudent maleStudent2 = list.parallelStream()
    .filter(s->s.getSex()== Student.Sex.MALE)
    .collect(MaleStudent::new, MaleStudent::accumulate, MaleStudent::combine);

maleStudent2.getList().stream()
    .forEach(s-> System.out.println(s.getName()));

// result >
[main] MaleStudent
[main] MaleStudent
[ForkJoinPool.commonPool-worker-3] MaleStudent
[ForkJoinPool.commonPool-worker-5] MaleStudent
[ForkJoinPool.commonPool-worker-3] accumulate()
[main] accumulate()
[ForkJoinPool.commonPool-worker-3] combine()
[main] combine()
[main] combine()
๊ฐ•์ฐฌํฌ
๋ฐ•์ง€์„ฑ

๋ณ‘๋ ฌ ์ฒ˜๋ฆฌ ์„ฑ๋Šฅ

๐Ÿ‘‰์š”์†Œ์˜ ์ˆ˜์™€ ์š”์†Œ๋‹น ์ฒ˜๋ฆฌ ์‹œ๊ฐ„

์ปฌ๋ ‰์…˜์— ์š”์†Œ์˜ ์ˆ˜๊ฐ€ ์ ๊ณ  ์š”์†Œ๋‹น ์ฒ˜๋ฆฌ ์‹œ๊ฐ„์ด ์งง์œผ๋ฉด ์ˆœ์ฐจ์ฒ˜๋ฆฌ๊ฐ€ ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ๋ณด๋‹ค ๋น ๋ฅด๋‹ค

๐Ÿ‘‰์ŠคํŠธ๋ฆผ ์†Œ์Šค์˜ ์ข…๋ฅ˜

ArrayList, ๋ฐฐ์—ด์€ ์ธ๋ฑ์Šค๋กœ ์š”์†Œ๋ฅผ ๊ด€๋ฆฌํ•˜๊ธฐ ๋–„๋ฌธ์— ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ์‹œ ์‹œ๊ฐ„์ด ์ ˆ์•ฝ๋œ๋‹ค.

๋ฐ˜๋ฉด์— HashSet, TreeSet, LinkedList๋Š” ์ƒ๋Œ€์ ์œผ๋กœ ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ๊ฐ€ ๋Šฆ๋‹ค

๐Ÿ‘‰์ฝ”์–ด(Core)์˜ ์ˆ˜

์‹ฑ๊ธ€์ฝ”์–ด์ผ์ˆ˜๋ก ์ˆœ์ฐจ์ฒ˜๋ฆฌ๊ฐ€ ๋น ๋ฅด๋‹ค. ๊ณง, ์ฝ”์–ด์˜ ์ˆ˜๊ฐ€ ๋งŽ์„์ˆ˜๋ก ๋ณ‘๋ ฌ์ฒ˜๋ฆฌ ์ฒ˜๋ฆฌ์†๋„๊ฐ€ ๋นจ๋ผ์ง„๋‹ค.


โ€ป [์ด๊ฒƒ์ด ์ž๋ฐ”๋‹ค ์‹ ์šฉ๊ถŒJava ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ •๋ณต2] ํ•ด๋‹น ์ฑ…์„ ์ฐธ๊ณ ํ•˜๋ฉฐ ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค.

profile
Java Web Developer๐Ÿ˜Š

0๊ฐœ์˜ ๋Œ“๊ธ€