문서의 선택한 두 판 사이의 차이를 보여줍니다.
다음 판 | 이전 판 | ||
프로그램:java:ㄴhomework:book_source:ch16 [2022/01/04 11:31] clayeryan@gmail.com 만듦 |
프로그램:java:ㄴhomework:book_source:ch16 [2025/06/27 16:07] (현재) |
||
---|---|---|---|
줄 1: | 줄 1: | ||
- | ====== | + | ====== |
+ | |||
+ | =====스트림 소개 ===== | ||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.Iterator; | ||
+ | import java.util.List; | ||
+ | |||
+ | public class IteratorEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | List< | ||
+ | " | ||
+ | System.out.println(" | ||
+ | for (int i=0; i< | ||
+ | System.out.println(list.get(i)); | ||
+ | } | ||
+ | System.out.println(" | ||
+ | Iterator< | ||
+ | while(it.hasNext()) { | ||
+ | System.out.println(it.next()); | ||
+ | } | ||
+ | System.out.println(" | ||
+ | list.stream().forEach(s -> | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | </ | ||
+ | |||
+ | =====스트림 생성하기 ===== | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.stream.Stream; | ||
+ | |||
+ | public class ArrayToStream { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 문자열 배열객체 생성 | ||
+ | String[] arr = new String[]{" | ||
+ | |||
+ | // 배열전체 Stream 객체로 변환 | ||
+ | Stream< | ||
+ | stream1.forEach(s -> System.out.print(s+" | ||
+ | System.out.println(); | ||
+ | |||
+ | // 인덱스 지정해서 변환 (2부터 5전까지) | ||
+ | // 두번째 인덱스는 포함되지 않음 | ||
+ | Stream< | ||
+ | stream2.forEach(s -> System.out.print(s+" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.List; | ||
+ | import java.util.stream.Stream; | ||
+ | |||
+ | public class CollectionToStream { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 문자열 배열을 컬렉션프레임워크 List로 변환 | ||
+ | List< | ||
+ | // List 객체를 stream()메서드를 이용해 Stream 객체로 생성 | ||
+ | Stream< | ||
+ | // 내부반복자를 이용해 출력 | ||
+ | stream.forEach(s -> System.out.println(s)); | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.stream.Stream; | ||
+ | |||
+ | public class StreamByBuilder { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // generate() 메서드로 Stream 객체 생성 | ||
+ | // 리미트 10개 생성 | ||
+ | Stream< | ||
+ | // 내부 반복자로 출력 | ||
+ | stream.forEach(s -> System.out.println(s)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.stream.Stream; | ||
+ | |||
+ | public class StreamByIterator { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // generate() 메서드로 Stream 객체 생성 | ||
+ | // 리미트 10개 생성 | ||
+ | Stream< | ||
+ | // 내부 반복자로 출력 | ||
+ | stream.forEach(s -> System.out.println(s)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | =====스트림의 종류===== | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.stream.IntStream; | ||
+ | |||
+ | public class IntStreamEx { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | int[] arr = {1, | ||
+ | |||
+ | System.out.println(" | ||
+ | IntStream intstream1 = Arrays.stream(arr); | ||
+ | intstream1.forEach(s -> System.out.print(s+" | ||
+ | System.out.println(); | ||
+ | |||
+ | System.out.println(" | ||
+ | IntStream intstream2 = IntStream.of(arr); | ||
+ | intstream2.forEach(s -> | ||
+ | System.out.println(); | ||
+ | |||
+ | // 두번째 매개변수 인덱스 포함안됨 | ||
+ | System.out.println(" | ||
+ | IntStream intstream3 = IntStream.range(1, | ||
+ | intstream3.forEach(s -> | ||
+ | System.out.println(); | ||
+ | |||
+ | System.out.println(" | ||
+ | IntStream intstream4 = IntStream.rangeClosed(1, | ||
+ | intstream4.forEach(s -> System.out.print(s+" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Random; | ||
+ | import java.util.stream.DoubleStream; | ||
+ | import java.util.stream.IntStream; | ||
+ | import java.util.stream.LongStream; | ||
+ | |||
+ | public class RandomToStream { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | System.out.println(" | ||
+ | // ints(3) 3개 | ||
+ | IntStream isr = new Random().ints(3); | ||
+ | isr.forEach(s -> System.out.println(s)); | ||
+ | |||
+ | // ints(갯수, | ||
+ | isr = new Random().ints(10, | ||
+ | isr.forEach(s -> System.out.println(s)); | ||
+ | System.out.println(); | ||
+ | |||
+ | System.out.println(" | ||
+ | // longs(갯수, | ||
+ | LongStream lsr = new Random().longs(3, | ||
+ | lsr.forEach(s -> System.out.println(s)); | ||
+ | System.out.println(); | ||
+ | |||
+ | System.out.println(" | ||
+ | // doubles(3) 3개 | ||
+ | DoubleStream dsr = new Random().doubles(3); | ||
+ | dsr.forEach(s -> | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.stream.IntStream; | ||
+ | |||
+ | public class StrToStream { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | String str = " | ||
+ | |||
+ | // Stream 객체 생성 | ||
+ | IntStream isr = str.chars(); | ||
+ | |||
+ | // System.out.print() | ||
+ | isr.forEach(s -> System.out.print((char)s)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.io.BufferedReader; | ||
+ | import java.io.File; | ||
+ | import java.io.FileReader; | ||
+ | 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 FileToStream { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | try { | ||
+ | // Paths.get() 메서드 사용 | ||
+ | Path path = Paths.get(" | ||
+ | Stream< | ||
+ | stream.forEach( s -> System.out.println(s)); | ||
+ | stream.close(); | ||
+ | System.out.println(); | ||
+ | |||
+ | // | ||
+ | File file = path.toFile(); | ||
+ | FileReader fr = new FileReader(file); | ||
+ | BufferedReader br = new BufferedReader(fr); | ||
+ | br.lines().forEach( s -> System.out.println(s)); | ||
+ | stream.close(); | ||
+ | } catch (Exception e) { | ||
+ | System.out.println(e.getMessage()); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.io.File; | ||
+ | import java.nio.file.Files; | ||
+ | import java.nio.file.Path; | ||
+ | import java.nio.file.Paths; | ||
+ | import java.util.stream.Stream; | ||
+ | |||
+ | public class DirectoryToStream { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | try { | ||
+ | System.out.println(" | ||
+ | // src 디렉토리로 Path 객체 생성 | ||
+ | Path path = Paths.get(" | ||
+ | // src 경로의 모든 디렉토리와 파일 Stream 객체로 생성 | ||
+ | Stream< | ||
+ | sr1.forEach(p -> System.out.println(p.getFileName())); | ||
+ | |||
+ | System.out.println(" | ||
+ | // src 디렉토리로 시작해서 10단계까지의 디렉토리 깊이까지 탐색 | ||
+ | Stream< | ||
+ | (p, basicFileAttributes) -> { | ||
+ | File file = p.toFile(); | ||
+ | // 디렉토리가 아니고 파일이름에 Stream이 포함된 파일명 리턴 | ||
+ | return !file.isDirectory() && file.getName().contains(" | ||
+ | }); | ||
+ | sr2.forEach( p -> System.out.println(p.getFileName())); | ||
+ | } catch (Exception e) { | ||
+ | System.out.println(e.getMessage()); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | =====스트림 가공하기===== | ||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.List; | ||
+ | |||
+ | public class FilterStream { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // List 객체 생성 | ||
+ | List< | ||
+ | " | ||
+ | |||
+ | // distinct() 메서드로 중복 제거 후 내부 반복자로 출력 | ||
+ | System.out.println(" | ||
+ | list.stream().distinct().forEach(n -> System.out.println(n)); | ||
+ | System.out.println(); | ||
+ | |||
+ | // " | ||
+ | System.out.println(" | ||
+ | list.stream().filter(n -> n.startsWith(" | ||
+ | .forEach(n -> System.out.println(n)); | ||
+ | System.out.println(); | ||
+ | |||
+ | // 중복제거 후 " | ||
+ | System.out.println(" | ||
+ | list.stream().distinct().filter(n -> n.startsWith(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.List; | ||
+ | import java.util.stream.DoubleStream; | ||
+ | import java.util.stream.IntStream; | ||
+ | |||
+ | public class StreamFlatMap { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 문자열을 공백으로 분리해서 매핑 | ||
+ | List< | ||
+ | " | ||
+ | list1.stream().flatMap(data -> Arrays.stream(data.split(" | ||
+ | .forEach(word -> System.out.println(word)); | ||
+ | System.out.println(); | ||
+ | |||
+ | // 문자열을 ,로 분리해서 double 자료형으로 변환해서 매핑 | ||
+ | List< | ||
+ | DoubleStream dsr = list2.stream().flatMapToDouble(data -> { | ||
+ | String[] strArr = data.split("," | ||
+ | double[] dArr = new double[strArr.length]; | ||
+ | for(int i=0; i< | ||
+ | dArr[i] = Double.parseDouble(strArr[i].trim()); | ||
+ | } | ||
+ | return Arrays.stream(dArr); | ||
+ | }); | ||
+ | dsr.forEach(n -> System.out.println(n)); | ||
+ | System.out.println(); | ||
+ | |||
+ | // 문자열을 ,로 분리해서 int 자료형으로 변환해서 매핑 | ||
+ | List< | ||
+ | IntStream isr = list3.stream().flatMapToInt(data -> { | ||
+ | String[] strArr = data.split("," | ||
+ | int[] intArr = new int[strArr.length]; | ||
+ | for(int i=0; i< | ||
+ | intArr[i] = Integer.parseInt(strArr[i].trim()); | ||
+ | } | ||
+ | return Arrays.stream(intArr); | ||
+ | }); | ||
+ | isr.forEach(n -> System.out.println(n)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.List; | ||
+ | import java.util.stream.DoubleStream; | ||
+ | import java.util.stream.IntStream; | ||
+ | |||
+ | public class StreamMap { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | List< | ||
+ | " | ||
+ | |||
+ | System.out.println(" | ||
+ | list.stream().mapToInt(s -> s.length()) | ||
+ | .forEach(len -> System.out.println(len)); | ||
+ | System.out.println(); | ||
+ | |||
+ | System.out.println(" | ||
+ | list.stream().mapToInt(String:: | ||
+ | .forEach(len -> System.out.println(len)); | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.Comparator; | ||
+ | import java.util.List; | ||
+ | |||
+ | public class StreamOrder { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | List< | ||
+ | |||
+ | System.out.println(" | ||
+ | list.stream().sorted().forEach(System.out:: | ||
+ | System.out.println(); | ||
+ | |||
+ | System.out.println(" | ||
+ | list.stream().sorted(Comparator.reverseOrder()).forEach(System.out:: | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | abstract class Shape implements Comparable< | ||
+ | |||
+ | // 필드 | ||
+ | int x, y; | ||
+ | |||
+ | // 생성자 | ||
+ | Shape() { | ||
+ | this(0, 0); | ||
+ | } | ||
+ | Shape(int x, int y) { | ||
+ | this.x = x; | ||
+ | this.y = y; | ||
+ | } | ||
+ | |||
+ | // 추상메서드 | ||
+ | abstract double area(); | ||
+ | abstract double length(); | ||
+ | |||
+ | // 일반 메서드 | ||
+ | public String getLocation() { | ||
+ | return " | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public int compareTo(Shape s) { | ||
+ | return (int)(this.area() - s.area()); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | public class Rectangle extends Shape { | ||
+ | |||
+ | // 필드 | ||
+ | int w,h; | ||
+ | |||
+ | // 생성자 | ||
+ | Rectangle() { | ||
+ | this(1, | ||
+ | } | ||
+ | Rectangle(int w, int h) { | ||
+ | this.w = w; | ||
+ | this.h = h; | ||
+ | } | ||
+ | |||
+ | // 메서드 재정의(오버라이딩) | ||
+ | @Override | ||
+ | double area() { | ||
+ | return (w * h); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | double length() { | ||
+ | return (w + h) * 2; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public String toString() { | ||
+ | return " | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | public class Circle extends Shape { | ||
+ | |||
+ | // 필드 | ||
+ | double r; | ||
+ | |||
+ | // 생성자 | ||
+ | Circle() { | ||
+ | this(1); | ||
+ | } | ||
+ | Circle(double r) { | ||
+ | this.r = r; | ||
+ | } | ||
+ | |||
+ | // 메서드 재정의(오버라이딩) | ||
+ | @Override | ||
+ | double area() { | ||
+ | return (r * r) * Math.PI; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | double length() { | ||
+ | return (r * 2) * Math.PI; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public String toString() { | ||
+ | return " | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.Comparator; | ||
+ | import java.util.List; | ||
+ | |||
+ | public class StreamOrder2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Shape s1 = new Rectangle(10, | ||
+ | Shape s2 = new Circle(10); | ||
+ | Shape s3 = new Rectangle(20, | ||
+ | Shape s4 = new Circle(11); | ||
+ | |||
+ | List< | ||
+ | |||
+ | System.out.println(" | ||
+ | list.stream().sorted().forEach(System.out:: | ||
+ | |||
+ | System.out.println(" | ||
+ | list.stream().sorted((a, | ||
+ | |||
+ | System.out.println(" | ||
+ | list.stream().sorted(Comparator.reverseOrder()).forEach(System.out:: | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.Comparator; | ||
+ | import java.util.List; | ||
+ | |||
+ | public class StreamOrder3 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Shape s1 = new Rectangle(10, | ||
+ | Shape s2 = new Circle(10); | ||
+ | Shape s3 = new Rectangle(20, | ||
+ | Shape s4 = new Circle(11); | ||
+ | |||
+ | List< | ||
+ | |||
+ | System.out.println(" | ||
+ | list.stream().forEach(System.out:: | ||
+ | |||
+ | System.out.println(" | ||
+ | System.out.println(" | ||
+ | list.stream().sorted(new Comparator< | ||
+ | @Override | ||
+ | public int compare(Shape s1, Shape s2) { | ||
+ | return (int)(s1.length() - s2.length()); | ||
+ | } | ||
+ | }).forEach(System.out:: | ||
+ | |||
+ | System.out.println(" | ||
+ | list.stream().sorted((a, | ||
+ | |||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.List; | ||
+ | |||
+ | public class StreamPeek { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Shape s1 = new Rectangle(10, | ||
+ | Shape s2 = new Circle(10); | ||
+ | Shape s3 = new Rectangle(20, | ||
+ | Shape s4 = new Circle(11); | ||
+ | |||
+ | List< | ||
+ | |||
+ | // 모든 요소들이 Shape의 인스턴스(객체)인지 조건 비교 | ||
+ | boolean result = list.stream().allMatch(a -> (a instanceof Shape)); | ||
+ | System.out.println(" | ||
+ | |||
+ | // 요소들 중 하나이상이 Rectangle 객체인지 조건 비교 | ||
+ | boolean result2 = list.stream().anyMatch(a -> (a instanceof Rectangle)); | ||
+ | System.out.println(" | ||
+ | |||
+ | // 모든 요소가 Circle의 객체인지 조건에 해당하지 않는지 비교 | ||
+ | boolean result3 = list.stream().noneMatch(a -> (a instanceof Circle)); | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.OptionalDouble; | ||
+ | import java.util.OptionalInt; | ||
+ | |||
+ | public class StreamOptional { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | int[] arr = new int[100]; | ||
+ | for(int i=0; | ||
+ | arr[i] = i+1; | ||
+ | } | ||
+ | |||
+ | // 리턴값 long | ||
+ | long count = Arrays.stream(arr).count(); | ||
+ | System.out.println(" | ||
+ | |||
+ | // 리턴값 int | ||
+ | int sum = Arrays.stream(arr).sum(); | ||
+ | System.out.println(" | ||
+ | |||
+ | OptionalInt first = Arrays.stream(arr).findFirst(); | ||
+ | System.out.println(" | ||
+ | |||
+ | OptionalInt max = Arrays.stream(arr).max(); | ||
+ | System.out.println(" | ||
+ | |||
+ | OptionalInt min = Arrays.stream(arr).min(); | ||
+ | System.out.println(" | ||
+ | |||
+ | OptionalDouble avg = Arrays.stream(arr).average(); | ||
+ | System.out.println(" | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.ArrayList; | ||
+ | import java.util.List; | ||
+ | import java.util.OptionalDouble; | ||
+ | import java.util.OptionalInt; | ||
+ | |||
+ | public class StreamOptionalNoElem { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 요소가 없는 빈 ArrayList 객체 생성 | ||
+ | List< | ||
+ | |||
+ | long count =list.stream().count(); | ||
+ | System.out.println(" | ||
+ | |||
+ | int sum =list.stream().mapToInt(Integer:: | ||
+ | System.out.println(" | ||
+ | |||
+ | OptionalDouble avg = list.stream().mapToInt(Integer:: | ||
+ | System.out.println(" | ||
+ | |||
+ | OptionalInt max =list.stream().mapToInt(Integer:: | ||
+ | System.out.println(" | ||
+ | |||
+ | OptionalInt min =list.stream().mapToInt(Integer:: | ||
+ | System.out.println(" | ||
+ | |||
+ | OptionalInt first =list.stream().mapToInt(Integer:: | ||
+ | System.out.println(" | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.ArrayList; | ||
+ | import java.util.List; | ||
+ | import java.util.OptionalDouble; | ||
+ | import java.util.OptionalInt; | ||
+ | |||
+ | public class StreamOptionalNoElem2 { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 요소가 없는 빈 ArrayList 객체 생성 | ||
+ | List< | ||
+ | |||
+ | long count =list.stream().count(); | ||
+ | System.out.println(" | ||
+ | |||
+ | int sum =list.stream().mapToInt(Integer:: | ||
+ | System.out.println(" | ||
+ | |||
+ | OptionalDouble avg = list.stream().mapToInt(Integer:: | ||
+ | // 요소가 존재하는 경우에만 평균 출력 | ||
+ | if(avg.isPresent()) { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | |||
+ | // 요소값이 없는 경우 기본값 설정 | ||
+ | int max =list.stream().mapToInt(Integer:: | ||
+ | System.out.println(" | ||
+ | |||
+ | // 요소값이 없는 경우 기본값 설정 | ||
+ | int min =list.stream().mapToInt(Integer:: | ||
+ | System.out.println(" | ||
+ | |||
+ | // 요소가 존재하면 실행 | ||
+ | list.stream().mapToInt(Integer:: | ||
+ | .findFirst().ifPresent(a -> System.out.println(" | ||
+ | |||
+ | |||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.List; | ||
+ | |||
+ | public class StreamReduce { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Shape s1 = new Rectangle(10, | ||
+ | Shape s2 = new Circle(10); | ||
+ | Shape s3 = new Rectangle(20, | ||
+ | Shape s4 = new Circle(11); | ||
+ | |||
+ | List< | ||
+ | |||
+ | double areaSum = list.stream().mapToDouble(Shape:: | ||
+ | System.out.println(" | ||
+ | areaSum = list.stream().mapToDouble(Shape:: | ||
+ | System.out.println(" | ||
+ | |||
+ | areaSum = list.stream().mapToDouble(Shape:: | ||
+ | .reduce(0, | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.List; | ||
+ | import java.util.Set; | ||
+ | import java.util.stream.Collectors; | ||
+ | |||
+ | public class StreamCollect { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Shape s1 = new Rectangle(10, | ||
+ | Shape s2 = new Circle(10); | ||
+ | Shape s3 = new Rectangle(20, | ||
+ | Shape s4 = new Circle(11); | ||
+ | |||
+ | List< | ||
+ | |||
+ | // 요소가 Rectangle 객체인 경우 collect 메서드로 List로 변환 | ||
+ | List< | ||
+ | rectList.stream().forEach(f -> System.out.println(f)); | ||
+ | System.out.println(); | ||
+ | |||
+ | // 요소가 Rectangle 객체인 경우 collect 메서드로 Set으로 변환 | ||
+ | Set< | ||
+ | rectSet.stream().forEach(f -> System.out.println(f)); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.Arrays; | ||
+ | import java.util.List; | ||
+ | import java.util.Map; | ||
+ | import java.util.stream.Collectors; | ||
+ | |||
+ | public class StreamGroupingBy { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | Shape s1 = new Rectangle(10, | ||
+ | Shape s2 = new Circle(10); | ||
+ | Shape s3 = new Rectangle(20, | ||
+ | Shape s4 = new Circle(11); | ||
+ | |||
+ | List< | ||
+ | |||
+ | try { | ||
+ | // 객체 타입으로 그룹핑 (Rectangle, Circle) | ||
+ | Map< | ||
+ | |||
+ | System.out.println(" | ||
+ | map.get(Class.forName(" | ||
+ | |||
+ | System.out.println(" | ||
+ | map.get(Class.forName(" | ||
+ | |||
+ | } catch (ClassNotFoundException e) { | ||
+ | System.out.println(e.getMessage()); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ===== 스트림 병렬처리 ===== | ||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.ArrayList; | ||
+ | import java.util.List; | ||
+ | |||
+ | public class StreamParallel { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | List< | ||
+ | for(int i=0; | ||
+ | list.add(i); | ||
+ | } | ||
+ | long start = System.nanoTime(); | ||
+ | // stream() 순차적 스트림 처리 | ||
+ | list.stream().forEach(a -> { | ||
+ | try { | ||
+ | Thread.sleep(1); | ||
+ | } catch (InterruptedException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | }); | ||
+ | |||
+ | long end = System.nanoTime(); | ||
+ | System.out.println(" | ||
+ | |||
+ | |||
+ | start = System.nanoTime(); | ||
+ | // parallelStream() 병렬 스트림 처리 | ||
+ | list.parallelStream(). forEach(a -> { | ||
+ | try { | ||
+ | Thread.sleep(1); | ||
+ | } catch (InterruptedException e) { | ||
+ | e.printStackTrace(); | ||
+ | } | ||
+ | }); | ||
+ | end = System.nanoTime(); | ||
+ | System.out.println(" | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||
+ | =====스트림 연결하기====== | ||
+ | |||
+ | <code java> | ||
+ | package chapter16; | ||
+ | |||
+ | import java.util.stream.Stream; | ||
+ | |||
+ | public class StreamConcat { | ||
+ | |||
+ | public static void main(String[] args) { | ||
+ | |||
+ | // 문자열 스트림 객체 | ||
+ | Stream< | ||
+ | // 정수 스트림 객체 | ||
+ | Stream< | ||
+ | |||
+ | // concat() 메서드로 두 스트림 객체 | ||
+ | Stream< | ||
+ | concat.forEach(a -> System.out.print(a + "," | ||
+ | |||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||