Q. How to find Sum of Odd and even number using stream.
SOLUTION:-
import java.util.Arrays;
import java.util.List;
public class SumOfOddAndEven {
public static void main(String[] args) {
Integer[] number= {1,2,3,4,5,6,7,8,9,10};
List<Integer> list=Arrays.asList(number);
// Even Nomber
int sumOfEven=list.stream().filter(i->i%2==0).mapToInt(i->i).sum();
System.out.println(“Sum of Even numbers:”+sumOfEven);
// Odd Number
int sumOfOdd=list.stream().filter(i->i%2!=0).mapToInt(i->i).sum();
System.out.println(“Sum of Odd numbers:”+sumOfOdd);
}
}
Leave a Reply