I get a stream of some custom objects and I would like to create a map Map<Integer, MyObject>
with index of each object as key. To give you a simple example:
Stream<String> myStream = Arrays.asList("one","two","three").stream();
Integer i = 0;
Map<Integer, String> result3 = myStream.collect(Collectors.toMap(x -> i++, x -> x));
Obviously, this doesn't compile because:
local variables referenced from a lambda expression must be final or effectively final
Is there a simple way to map elemnts of a stream to their indices so that the expected output for above example is something like:
{1=one, 2=two, 3=three}