How to find Sum of Odd and even number using stream.

Q. How to find Sum of Odd and even number using stream.

Ans :-

SOLUTION:- 

package v1;

import java.util.Arrays;
import java.util.List;

public class SomOfOddAndEven {

public static void main(String[] args) {
Integer[] number= {1,2,3,4,5,6,7,8,9,10};
List<Integer> list=Arrays.asList(number);
int sumOfEven=list.stream().filter(i->i%2==0).mapToInt(i->i).sum();
System.out.println("Sum of Even numbers:"+sumOfEven);
int sumOfOdd=list.stream().filter(i->i%2!=0).mapToInt(i->i).sum();
System.out.println("Sum of Odd numbers:"+sumOfOdd);

}

}


****************************************************************

Comments

Popular posts from this blog

How to create React project using command prompt?

How to achieve abstraction?

Interview Question? How to find square of each element in this Array {2,4,5,6,8} using map ?