I have a arraylist
List<Integer> h = Arrays.asList(1,2,3,4,5,6);
I want to filter even numbers and then store it in a map with key is unique number starting from 1 and value being the filtered value using java8
output ```` 1-2 2-4 3-6 ```
I will be using filter and Collectors.toMap() in collect
Map<Integer,Integer> j = h.stream().filter(fe->fe%2==0).collect(Collectors.toMap(?,?));
what will come in ?
something like that
List<Integer> h = Arrays.asList(1,2,3,4,5,6);
AtomicInteger f = new AtomicInteger(0);
Map<Integer,Integer> j = h.stream()
.filter(fe-> fe%2==0)
.collect(Collectors.toMap(fs -> f.incrementAndGet(), fs -> fs));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments