3

I already followed many links like : https://www.mkyong.com/java8/java-8-convert-list-to-map/ and Java 8 List<V> into Map<K, V>, but I did not come to know how to create Map<Integer, List<Integer>>

Assuming the fact that one Employee can work in many department, so I am trying to get that here

Here is my code:

List<Map<String, Object>> results = jdbcTemplate.queryForList(SQL, empId);

Map<Integer, List<Integer>> resultMap = new HashMap<>();

List<Integer> tempLst = new ArrayList<>();
for (Map<String, Object> map : results) {
    tempLst.add(Integer.valueOf(map.get("DEPARTMENT_ID").toString().trim()));
}
resultMap.put(empId,tempLst);

I tried something like below:

results.stream().collect(Collectors.toMap(currencyId, Arrays::asList))

But it doesn't serve my purpose, I also tried

results.stream().collect(groupingBy(currencyId, mapping(Integer.valueOf(map.get("EMP_ID").toString().trim()), toList())));
  • The latter should have worked. What are the data structures in use, can you detail them better in the question? Aside: `empId` and `currencyId` are confusing, which one are you working with? – Naman Apr 09 '19 at 06:31
  • I am getting Multiple markers at this line - Syntax error, insert ";" to complete Statement - grouping cannot be resolved to a variable - map cannot be resolved –  Apr 09 '19 at 06:34
  • The `for` loop code simply does `resultMap.put(empId, results.stream() .map(map -> Integer.valueOf(map.get("EMP_ID").toString().trim())) .collect(Collectors.toList()));` – Naman Apr 09 '19 at 06:35
  • Please show example data for what your list of maps looks like and what you expect your map of lists to look like. I don't think the fact that the original list of maps comes from a database is relevant to your problem (just trying to get to the [mcve]). – Jason Apr 09 '19 at 06:40
  • @Naman - Your suggestion is retirning the List . I simply want resultMap to be populated with right values –  Apr 09 '19 at 06:43
  • Does your original `for` loop work as expected? It looks a little strange that you want a map with empId as key and a list of empId as value, or is it a list of currencyId as value that you want? – Joakim Danielson Apr 09 '19 at 06:43
  • @Joakim Danielson - My original code is working as expected –  Apr 09 '19 at 06:44
  • Then what is wrong with keeping it as it is, this looks more like a question for https://codereview.stackexchange.com/ in my opinion. No point in changing simple and working code just for the sake of changing – Joakim Danielson Apr 09 '19 at 06:52
  • 1
    I'm voting to close this question as off-topic because it belongs to https://codereview.stackexchange.com/ – Joakim Danielson Apr 09 '19 at 06:53
  • 2
    I don’t understand your problem with @Naman’s code. “populating the map” means putting the `List`. Regardless of how the code looks like. You can create the map in a single statement, `Map> resultMap = Collections.singletonMap(empId, results.stream() .map(map -> Integer.valueOf(map.get("EMP_ID").toString().trim())) .collect(Collectors.toList()));`. In general, you should learn about the importance of the `parameter ->` part of lambda expressions. With that, you can fix your last code example to do the intended thing, though it’s still the same as above, putting a `List`… – Holger Apr 09 '19 at 07:33

0 Answers0