functional programming - How can I print the non-filtered values in IntStream in Java 8 -
i new functional programming , these features in java 8. trying things intstream, can not 1 simple thing: if filter array of integers, how can modify , print out non-filtered values?
so, example, if have array [1,2,3,4] , filter uneven numbers , add 7 them, how can print remaining numbers? can done in 1 line of code?
here example code:
int[] numbers={1,2,3,4,8,9,6}; intstream.of(numbers) .filter(i -> % 2 != 0) .map(i -> + 7) .foreach(system.out::println);
for specific example provide, i'd add conditional element map operation, so:
intstream.of(1, 2, 3, 4, 8, 9, 6) .map(i -> % 2 != 0 ? + 7 : i) .foreach(system.out::println); each number i gets mapped i + 7 if it's odd, or else remains is. once number has been discarded stream, it's gone.
Comments
Post a Comment