I have recently faced an interview question where I was required to filter out odd and even numbers from a List
. The only catch was that I could not use 2 filters like the following:
List<Integer> nums = Arrays.asList(1,2,3,4,5);
List<Integer> odd = nums.stream().filter(n -> n%2 != 0).collect(Collectors.toList());
List<Integer> even = nums.stream().filter(n -> n%2 == 0).collect(Collectors.toList());
I said it was not possible since the final collect
method can only return 1 single list.
What am I missing?