Skip to content
Advertisement

Transform List into Map using only two keys and odd or even list indexes as values – Java 8 Stream

I would like to transform list into map using as key values only two string values. Then as values just list of strings containing elements from odd or even index positions from input list. Here is old fashion code:

JavaScript

How to transform this code into Java 8 using streams to get this result?

JavaScript

My current messy attempt require modifying elements of list, but definitly must be better option.

JavaScript

Or maybe someone help me with this incorrect solution?

JavaScript

which return this:

JavaScript

Advertisement

Answer

You were on the right track with streaming over the indexes:

JavaScript

If you are ok with having your map be indexed by a boolean you can use partitioningBy:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement