본문 바로가기
java

스트림의 최종연산

by improve 2024. 2. 15.
package org.example;

import java.util.stream.IntStream;

public class Main10 {
    public static void main(String[] args) {
        int sum = IntStream.of(1,3,5,7,9)
                            .sum();
        System.out.println("sum = "+sum);

        long count = IntStream.of(1,3,5,7,9)
                                .count();
        System.out.println("count = "+count);

        IntStream.of(1,3,5,7,9)
                .average()
                .ifPresent(value -> System.out.println("average = "+value));

        IntStream.of(1,3,5,7,9)
                .min()
                .ifPresent(value -> System.out.println("min = "+value));

        IntStream.of(1,3,5,7,9)
                .max()
                .ifPresent(value -> System.out.println("max = "+value));

        double avg = IntStream.of(1,3,5,7,9)
                            .average().orElse(0);
        System.out.println(avg);



    }
}

 

sum은 합

count는 갯수

average 는 평균

min 는 최솟값

max 는 최댓값

'java' 카테고리의 다른 글

시각과 날짜 코드  (0) 2024.02.16
최종연산 2  (0) 2024.02.16
스트림 2  (0) 2024.02.15
필터링 과 맵핑  (0) 2024.02.15
스트림의 이해와 생성  (1) 2024.02.15