Search code examples
javalistcollectionshashmap

How to transform a List<Map<String, Object>> into Map<String, List<Object>> using Java 8


I have a list with a map inside and I need to turn it into a map with a list inside.

I have a List <Map <String, Object >> and I need to change it to Map<String, List <Object<Object>>>.

, of course, by passing the data contained in it, matching the keys to the values

There are objects of the Date class in the Object class, this structure was used because it is the result returned by jdbcTemplate. Now I need to change that to have a date list for each unique String key.

I have a method that takes two columns from a database and returns List<Map<String, Object >>. One column is the username and the second column is the dates on which he was on vacation. I need a data structure that will allow me to match the key (userId) with a list of his vacation dates.

public List<Map<String, Object>> getHoliday(String teamLeaderId) {
    String query = "SELECT  H.userid, date FROM Holiday H INNER JOIN Contact C on H.userid = C.UserId INNER JOIN Team T on C.TeamId = T.team_id WHERE team_leader = ? AND isApproved = 0";
    return this.getJdbcTemplate().queryForList(query, teamLeaderId);
}

I'm using Java 8.

Is it possible to do?


Solution

  • Its possible to do so using Collectors.toMap to convert the List to Map , Here a small exemple to how to do it.

    package org.example;
    
    import java.util.*;
    import java.util.stream.Collectors;
    
    public class App 
    {
        public static void main( String[] args )
        {
            ArrayList<Map<String, Object >> list =  new ArrayList <Map <String, Object >>(){{ // just to simulate the result
                for(int i = 0; i < 10; i++){
                    final Integer finalI = i + 1;
                    final Integer simulateID = (finalI % 3) + 1;
    
                    add(new HashMap<String, Object>(){{
                        Calendar cal = Calendar.getInstance();
                        cal.add(Calendar.DAY_OF_MONTH, finalI);
                        put("userid", simulateID.toString() );
                        put("date", cal.getTime());
                    }});
                }
            }};
    
            Map<String, ArrayList<Object>> map = list
            .stream()
            .collect(
                Collectors.toMap(
                        e -> (String) e.get("userid"),
                        e -> new ArrayList<Object>(){{ add(e.get("date")); }},
                        (v1, v2) -> {
                            v1.addAll(v2);
                            return v1;
                        }
                )
            );
    
            System.out.println(map);
        }
    }
    

    But before using something like that maybe is better take a look in this article to see if it fit to your needs https://arnaudroger.github.io/blog/2017/06/13/jdbc-template-one-to-many.html