0
List<Map<String, Object>> list = jtemplate.queryForList(sql, rmid);

The above statement returns the following List:

[{date_part=3, count=1}, {date_part=11, count=1}, {date_part=10, count=2}]

Now, I want the output to be

{3=1, 11=1, 10=2}

I just want to extract the date_part's value and store it as the key in the map and count as the value of the map. I tried doing this but I am unable to get the desired result.

Map<Object, Object> hm2 = new HashMap<Object, Object>();
list.stream().flatMap(map -> map.entrySet().stream())
        .forEach(entry -> hm2.put(entry.getKey(), entry.getValue()));
System.out.println(hm2);

The output for the above code is :

{count=1, date_part=11}

1 Answers1

0

You can use Collectors.toMap to collect as Map:

Map<Object, Object> res = list.stream()
        .collect(Collectors.toMap(
                e -> e.get("date_part"),
                e -> e.get("count")));
Community
  • 1
  • 1
Eklavya
  • 17,618
  • 4
  • 28
  • 57