0

I have a list of Profile objects List<Profile> list.

Which I need to convert into a LinkedHashMap<String, String>.

Where object Profile is consisted of:

public class Profile {
 private String profileId;
 private String firstName;
 private String lastName;
}

I have tried the following:

Map<String, String> map = list.stream()
 .collect(Collectors.toMap(Profile::getFirstName, 
 Profile::getLastName));

But it did not work, I'm getting a compilation error:

Incompatible parameter types in method reference expression
Alexander Ivanchenko
29.2k6 gold badges29 silver badges53 bronze badges
asked Apr 26, 2022 at 8:53
12
  • What do you mean by "it did not work"? How did it not work? Any error messages? What is your expected output? How is the actual output different from the the expected output? Commented Apr 26, 2022 at 8:57
  • Could you please elaborate the key-value you expect in your map? Commented Apr 26, 2022 at 8:58
  • @UjwalKumar yes, for firstName key should be "firstName", for lastName should be "lastName" Commented Apr 26, 2022 at 8:59
  • Thank you for the clarification. Also what do you mean by "it did not work"? Did you not get any output or error that can be useful for us to debug your problem? Also I don't think keeping firstname as key is good idea since two people could have same first name and your stream api would give you an exception. Commented Apr 26, 2022 at 9:07
  • Well it doesnt work for example if I try to add profileId to it as well it complains reason: Incompatible parameter types in method reference expressio Commented Apr 26, 2022 at 9:17

1 Answer 1

3

Incompatible parameter types in method reference expression

Make sure that you're not using a list of row type as a stream source. I.e. check if the generic type parameter is missing: List list (it has to be List<Profile> list), otherwise all elements of the list as being of type Object and methods from the Profile class would not be accessible.


Collecting into a LinkedHashMap

By default, toMap provides you with a general purpose implementation of the Map (for now it's HashMap but it might change in the future).

In order to collect stream elements into a particular implementation of the Map interface, you need to use a flavor of Collectors.toMap() that expects four arguments:

  • keyMapper - a mapping function to produce keys,
  • valueMapper - a mapping function to produce values,
  • mergeFunction - function that is meant to resolve collisions between value associated with the same key,
  • mapFactory - a supplier providing a new empty Map into which the results will be inserted.

In the code below, mergeFunction isn't doing anything useful, it just has to be present in order to utilize the version of toMap() that allows to specify the mapFactory.

Map<String, String> map = list.stream()
 .collect(Collectors.toMap(
 Profile::getFirstName,
 Profile::getLastName,
 (left, right) -> left,
 LinkedHashMap::new
 ));

Note if there could be cases when more than one value gets associated with the same key, you need either to provide a proper implementation of mergeFunction (to peek a particular value or aggregate values, etc.), or use groupingBy() as a collector, which will allow to preserve all values associated with a particular key.

Philzen
4,7771 gold badge36 silver badges57 bronze badges
answered Apr 26, 2022 at 10:09

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.