I am trying sequential search using Java 8 streams and lambda expressions. Here is my code
List<Integer> list = Arrays.asList(10, 6, 16, 46, 5, 16, 7);
int search = 16;
list.stream().filter(p -> p == search).forEachOrdered(e -> System.out.println(list.indexOf(e)));
Output: 2
2
I know list.indexOf(e)
always prints the index of the first occurrence. How do I print all the indexes?
For a start, using Lambdas is not the solution to all problems... but, even then, as a for loop, you would write it:
Now, there's nothing particularly wrong with that, but note that the critical aspect here is the index, not the value. The index is the input, and the output of the 'loop'.
As a stream::
Produces output: