1

I have a list:

private List <String> list;

I want to convert it to a LinkedHashMap (to preserve order), such that the first two values in the map are a LinkedHashMap entry and so on until the list is a LinkedHashMap:

private LinkedHashMap<String, String> linked;

This is what I have come up with. Admittedly I am new to the Collectors implementations so bear with me:

 linked = list.stream()
 .collect(Collectors.toMap(
 Function.identity(),
 String::valueOf, //Used to be String::length
 LinkedHashMap::new));

this is giving me an error on the LinkedHashMap constructor line:

Cannot resolve constructor of LinkedHashMap

This is an example of what the list may look like:

zero
test0
one
test1
two
test2

and what I want the Map to look like:

zero:test0
one:test1
two:test2

Thanks

Youcef LAIDANI
60.2k21 gold badges110 silver badges177 bronze badges
asked Mar 5, 2019 at 20:37
5
  • String::length return Integer not String Commented Mar 5, 2019 at 20:39
  • Did you mean to declare linked as Map<String, Integer>? Commented Mar 5, 2019 at 20:39
  • Can you please give examples of what you want the LinkedHashMap to look like for different input lists? Commented Mar 5, 2019 at 20:40
  • I meant to declare Map<String, String> @ernest_k Commented Mar 5, 2019 at 20:45
  • please see OP @dnault Commented Mar 5, 2019 at 20:45

3 Answers 3

2

Why you complicate your code, a simple loop in your case solve the problem :

for (int i = 0; i < list.size(); i += 2) {
 linked.put(list.get(i), list.get(i + 1));
}

Quick, Ideone demo

Outputs

zero:test0
one:test1
two:test2
answered Mar 5, 2019 at 20:56

Comments

0

You missed merge function:

a merge function, used to resolve collisions between values associated with the same key, as supplied to Map.merge(Object, Object, BiFunction)

But to fill map with your expected values using stream api you can use forEach method and IntStream::iterate from java9

LinkedHashMap<String, String> linked = new LinkedHashMap<>();
IntStream.iterate(0, n -> n < list.size(), n -> n + 2)
 .forEach(i -> linked.put(list.get(i), list.get(i + 1)));
answered Mar 5, 2019 at 20:42

4 Comments

Hi thanks for the response. You're right, im missing the merge statement. How do i do that for <String, String>?
Hi thanks again. What is the error, Operator '+' cannot be applied to '<lambda parameter>', 'int it's not permitting lambda expressions in combination with mathematic operations ?
No, it's by itself. I get an error for the two mathematic operations n -> n < list.size() //Bad return type in lambda expression: boolean cannot be converted to in AND n -> n + 2 // Operator '+' cannot be applied to '<lambda parameter>'
@dk40149 method iterate is available from java9
0

This my attempt with java 8.

 IntStream.range(0, list.size())
 .mapToObj(index -> {
 if (index % 2 == 0) {
 return new AbstractMap.SimpleImmutableEntry<>(list.get(index), list.get(index + 1));
 }
 return null;
 }
 )
 .filter(Objects::nonNull)
 .collect(Collectors.toMap(AbstractMap.SimpleImmutableEntry::getKey, AbstractMap.SimpleImmutableEntry::getValue));
answered Mar 8, 2019 at 11:07

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.