2015년 9월 6일 일요일

[자바배열합구하기] IntStream, Arrays, forEach,람다식 이용

[자바배열합구하기] IntStream, Arrays, forEach,람다식 이용

package ojc;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;

public class OjcSample {
static int sum;
public static void main(String[] args) {
int[] myArr = {1,2,3,4,5,6,7,8,9,10}; 
int sum1 = IntStream.of(myArr).sum();
System.out.println("The sum is " + sum1);
int sum2 = Arrays.stream(myArr).sum(); 
System.out.println("The sum is " + sum2);

List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9,10);
list.stream().forEach((i) -> {
sum += i;
});
System.out.println("The sum is " + sum);
}
}

[결과]
The sum is 55
The sum is 55
The sum is 55

댓글 없음:

댓글 쓰기