0

I have the below expression

Map<String, String> institutionList = new LinkedHashMap<String, String>();
institutionService.list().forEach(institution -> institutionList.put(institution.getCode(),institution.getName()));

I tried like below.

institutionService.list().stream().collect(Collectors.toMap(‌​Institution::getCode‌​, Institution::getName));

enter image description here

enter image description here

But still error. how to convert this into stream() & map() with lambda?

Rajeshkumar
  • 815
  • 12
  • 35
  • 1
    try `institutionService.list().stream().collect(Collectors.toMap(Institution::getCode, Institution::getName));`. based on [this example](https://stackoverflow.com/questions/20363719/java-8-listv-into-mapk-v) – XtremeBaumer Jul 14 '17 at 09:51
  • 1
    the `forEach` is still cleaner and faster than `stream` – Laazo Jul 14 '17 at 10:00
  • Hint: look up information on the characters \u200c and \u200b mentioned in the error message. – Klitos Kyriacou Jul 14 '17 at 10:40
  • 1
    `\u200b and \u200b` is are "zero-width-space" non printing characters . You should remove – soorapadman Jul 14 '17 at 10:46
  • See the red underline in your code below the "(I" at the word "Institution"? Delete those two (actually four) characters and re-type them in. You might also want to figure out where you copied them from, because non-printing characters are a nuisance. – phatfingers Jul 14 '17 at 10:58

1 Answers1

0

An example which is working just fine:

Map<String, String> p = new HashMap<String, String>();
List<String> values = new ArrayList<String>(Arrays.asList("2", "4", "1"));
p = values.stream().collect(Collectors.toMap(String::toString, String::toString));
System.out.println(p);

results in

{1=1, 2=2, 4=4}

If we transfer it to your problem, the code might look like this:

I have the below expression

Map<String, String> institutionList = new LinkedHashMap<String, String>();
institutionList = institutionService.list().stream().collect(Collectors.toMap(‌​Institution::getCode‌​, Institution::getName));

In this case, I assume that your service gives values of class Institution

chrki
  • 6,143
  • 6
  • 35
  • 55
XtremeBaumer
  • 6,275
  • 3
  • 19
  • 65