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


16.2 스트림의 종류




16.2.1 컬렉션으로부터 스트림 얻기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package sec02.stream_kind;
 
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
 
public class FromCollectionExample {
    public static void main(String[] args) {
        List<Student> studentList = Arrays.asList(
            new Student("홍길동"10),
            new Student("신용권"20),
            new Student("유미선"30)
        );
        
        Stream<Student> stream = studentList.stream();
        stream.forEach(s -> System.out.println(s.getName()));
    }
}
cs


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package sec02.stream_kind;
 
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

 




16.2.2 배열로부터 스트림 얻기


 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package sec02.stream_kind;
 
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;
 
public class FromArrayExample {
    public static void main(String[] args) {
        String[] strArray = { "홍길동""신용권""김미나"};
        Stream<String> strStream = Arrays.stream(strArray);
        strStream.forEach(a -> System.out.print(a + ","));
        System.out.println();
        
        int[] intArray = { 12345 };
        IntStream intStream = Arrays.stream(intArray);
        intStream.forEach(a -> System.out.print(a + ","));
        System.out.println();
    }
}
cs

 



16.2.3 숫자 범위로부터 스트림 얻기

 

1
2
3
4
5
6
7
8
9
10
11
12
13
package sec02.stream_kind;
 
import java.util.stream.IntStream;
 
public class FromIntRangeExample {
    public static int sum;
    
    public static void main(String[] args) {
        IntStream stream = IntStream.rangeClosed(1,  100);
        stream.forEach(a -> sum += a);
        System.out.println("총합: " + sum);
    }
}
cs

 



16.2.4 파일로부터 스트림 얻기

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
26
27
28
29
30
31
32
package sec02.stream_kind;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
 
public class FromFileContentExample {
    public static void main(String[] args) throws IOException {
        Path path = Paths.get("src/sec02/stream_kind/linedata.txt"); // 파일의 경로 정보를 가지고 있는 Path 객체 생성
        Stream<String> stream;
        
        //Files.lines() 메소드 이용
        stream = Files.lines(path, Charset.defaultCharset()); // 운영체제의 기본 문자셋
        stream.forEach( System.out :: println );
        stream.close();
        System.out.println();
        
        //BufferedReader의 lines() 메소드 이용
        File file = path.toFile();
        FileReader fileReader = new FileReader(file);
        BufferedReader br = new BufferedReader(fileReader);
        stream = br.lines();
        stream.forEach( System.out :: println );
        stream.close();
    }
}
cs


linedata.txt

1
2
3
4
5
Java8에서 추가된 새로운 기능
1. 람다식
2. 메소드 참조
3. 디폴트 메소드와 정적 메소드
4. 새로운 API 패키지
cs



16.2.5 디렉토리로부터 스트림 얻기




 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package sec02.stream_kind;
 
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
 
public class FromDirectoryExample {
    public static void main(String[] args) throws IOException {
        Path path = Paths.get("C:/JavaProgramming/source");
        Stream<Path> stream = Files.list(path);
        stream.forEach( p -> System.out.println(p.getFileName()) );
        // p -> 서브 디렉토리 또는 파일에 해당하는 Path 객체
        // p.getFileName() 서브 디텍토리 이름 또는 파일 이름 리턴
    }
}
cs

 



Posted by 너래쟁이
: