JAVA chapter16. 스트림과 병렬처리.

16.1 스트림 소개

16.1.1 반복자 스트림


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package sec01.stream_introduction;
 
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
 
public class IteratorVsStreamExample {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("홍길동""신용권""감자바");
        
        //Iterator 이용
        Iterator<String> iterator = list.iterator();
        while(iterator.hasNext()) {
            String name = iterator.next();
            System.out.println(name);
        }
        
        System.out.println();
        
        //Stream 이용
        Stream<String> stream = list.stream();
        stream.forEach( name -> System.out.println(name) );
    }
}
cs



16.1.2 스트림의 특징



 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package sec01.stream_introduction;
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
 
public class LambdaExpressionsExample {
    public static void main(String[] args) {
        List<Student> list = Arrays.asList(
                new Student("홍길동"90),
                new Student("신용권"92)
        );
        
        Stream<Student> stream = list.stream();
        stream.forEach(s -> {
            String name = s.getName();
            int score = s.getScore();
            System.out.println(name + "-" + score);
        });
    }
}
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package sec01.stream_introduction;
 
public class 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; }
}
cs







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package sec01.stream_introduction;
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
 
public class ParallelExample {    
    public static void main(String[] args) {
        List<String> list = Arrays.asList("홍길동""신용권""감자바""람다식""박병렬");
        
        //순차 처리
        Stream<String> stream = list.stream();
        stream.forEach(ParallelExample :: print); // stream.forEach(name->print(name))
        
        System.out.println();
        
        //병렬 처리
        Stream<String> parallelStream = list.parallelStream();
        parallelStream.forEach(ParallelExample :: print);
    }
    
    public static void print(String str) {
        System.out.println(str+ " : " + Thread.currentThread().getName());
    }    
}
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package sec01.stream_introduction;
 
import java.util.Arrays;
import java.util.List;
 
public class MapAndReduceExample {
    public static void main(String[] args) {
        List<Student> studentList = Arrays.asList(
                new Student("홍길동"10),
                new Student("신용권"20),
                new Student("유미선"30)
        );        
            
        double avg = studentList.stream()
            //중간처리(학생 객체를 점수로 매핑)
            .mapToInt(Student :: getScore)
            //최종 처리(평균 점수)
            .average()
            .getAsDouble();
        
        System.out.println("평균 점수: " + avg);
    }
}
cs

 

 











Posted by 너래쟁이
: