In this article, I will explain how to print only even numbers from integer array using streams in java.
1)Lets take numbers array and initialize with some values
2)Then Arrays.stream(numberArray) with give you stream object. There you have lots of methods. Since we need to filter only odd numbers we can apply filter method with predicate as x%2==1
3)Then on top of that result apply for each loop and print numbers.
int[] numbers = new int[]{2,1,3,5,6,7,8,9};Arrays.stream(numbers).filter(x->x%2==1) .forEach(x-> System.out.println(x));
Full tutorial here –
Leave a Reply