fluff_thorrent
fluff_thorrent

Reputation: 123

How do I convert a Map of <String, List<Object>> into a Map of <Object, List<String>>?

Problem description

I have an old Java-programming textbook which I'm going through, and I've got stuck on a problem.

I need to convert this:

Map<String, List<Clock>> timeTableSortedByDestination = 
    new HashMap<String, List<Clock>>();

Into this:

Map<Clock, List<String>> timeTableSortedByDepartTime = 
    new HashMap<Clock, List<String>>();

In one method:

static Map<Clock, List<String>> flipMap(Map<String, List<Clock>> originalMap) {
    // implementation
}

I'm thinking of going through the original Map with loops and if's, but is there a smoother or simpler way to work through it? The book deals only in "source-code" Java (no Javascript).

Answer Update

Okay, so I went with James Grammatikos suggestion, works perfect. Here's what I wrote:

static Map<Clock, List<String>> flipMap(Map<String, List<Clock>> originalMap) {

    Map<Clock, List<String>> newMap = new HashMap<Clock, List<String>>();

    for(String s : originalMap.keySet()) {
       for(Clock c : originalMap.get(s)) {
           newMap.put(c, new ArrayList<String>());
           }
       }

    for(Clock c : newMap.keySet()) {
        for(String s : originalMap.keySet()) {

            if(originalMap.get(s).contains(c)) {
                List<String> temp = newMap.get(c);
                temp.add(s);

                newMap.put(c, temp);
            }
        }
    }
    return newMap;
}

Thank you for all the help!

Upvotes: 1

Views: 3789

Answers (2)

James Grammatikos
James Grammatikos

Reputation: 480

Just iterate through all the clocks, build a new map with the clocks as keys, then iterate through the original map again. I replaced clocks with objects here because i was too lazy to define a clock class

Map<String,List<Object>> timeTableSortedByDestination = new HashMap<String,List<Object>>();
Map<Object,List<String>> timeTableSortedByDepartTime = new HashMap<Object,List<String>>();
List<String> temp;    

// dump all of the objects into the new map
for(String s : timeTableSortedByDestination.keySet()){
    for(Object o : timeTableSortedByDestination.get(s)){
        timeTableSortedByDepartTime.put(o, new ArrayList<String>());
    }
}

//iterating through each list in the original map
for(Object o : timeTableSortedByDepartTime.keySet()){
    for(String s : timeTableSortedByDestination.keySet()){
        // if the object was in that list
        if(timeTableSortedByDestination.get(s).contains(o)){
            temp = timeTableSortedByDepartTime.get(o);
            temp.add(s);
            // add the corresponding string to the list in the new map
            timeTableSortedByDepartTime.put(o, temp);
        }
    }
}

Upvotes: 1

John Strickler
John Strickler

Reputation: 25421

It sounds like you want to group a List to a Map. You haven't provided enough detail to solve your particular problem. Here's a generic utility method:

public interface GroupingExpression<T, U> {
    T groupBy(U item);
}

public static <T, U> Map<T, List<U>> group(List<U> list, GroupingExpression<T, U> groupingExpression) {

    Map<T, List<U>> groupedMap = new LinkedHashMap<T, List<U>>();

    for(U item : list) {

        T key = groupingExpression.groupBy(item);

        List<U> keyedList = groupedMap.get(key);

        if(keyedList == null) {
            keyedList = new ArrayList<U>();
            groupedMap.put(key, keyedList);
        }

        keyedList.add(item);
    }

    return groupedMap;
}

Upvotes: 1

Related Questions