thurmc
thurmc

Reputation: 525

General merger of two hashmaps

In java if I have two objects A and B and both contain class variables for a reference id and A also has a class variable type, B has a class variable location. I am trying to construct a map with key as type and value as location. Currently I'm doing this by constructing two separate maps, one map (Map1) links reference id to type and is constructed by iterating through a list of objects of type A, the other map (Map2) links reference id to location and is constructed by iterating through a list of objects of type B. The maps are then merged by iterating through the keySet of Map1 and finding the value for the reference id, putting it as the key in a new map, and then getting the value of the location from Map2 and using it as the value for the type. Implementation is shown below. My question is: is there a more efficient way to do this algorithm? This doesn't seem like the best implementation. Sorry for the ambiguity - hopefully the code makes the question more clear.

Map<String, String> referenceIdToType = new HashMap<String, String>();
Map<String, String> referenceIdToLocation = new HashMap<String, String>();

for(Info info : infoList) {
     referenceIdToType.put(info.getReferenceId(), info.getType());
}
for(Location loc : locationList) {
     referenceIdToLocation.put(loc.getReferenceId(), loc.getLocation());
}

Map<String, String> typeToLocation = new HashMap<String, String>();
for(String referenceId : referenceIdToType.keySet()) {
    typeToLocation.put(referenceIdToType.get(referenceId), referenceIdToLocation.get(referenceId));
}

Upvotes: 1

Views: 300

Answers (3)

You can optimize it some by removing one of the HashMaps. You only need to make a HashMap for one of your lists. Then you build your final HashMap by looping through the second list, using the other list's HasMap to get the matching value.

Map<String, String> referenceIdToLocation = new HashMap<String, String>();

for(Location loc : locationList) {
     referenceIdToLocation.put(loc.getReferenceId(), loc.getLocation());
}

Map<String, String> typeToLocation = new HashMap<String, String>();
for(Info info : infoList) {
    typeToLocation.put(info.getType(), referenceIdToLocation.get(info.getReferenceId()));
}

Upvotes: 1

Stephen C
Stephen C

Reputation: 719561

My question is: is there a more efficient way to do this algorithm?

I don't think there is a more efficient way to perform that operation. I can't even think of a better representation / implementation for the final typeToLocation mapping, unless there is something special about the keys/values that allows you to take shortcuts.

(By the way, I wouldn't have called the operation you are performing "merging". From a mathematical standpoint it is more like a "composition" of mappings, though it is not strictly even that. To me, "merging" maps is simply creating a union of their entries, and that's what I thought you meant originally ...)

Upvotes: 0

Hunter McMillen
Hunter McMillen

Reputation: 61540

Why not just look up the Location and Info objects by referenceId, then put them into a HashMap?

ArrayList<String> referenceIds = //all reference ids;

public Location getLocationByReferenceId(String referenceId)
{
    for(Location loc : locationList)
    {
        if(loc.getReferenceId().equals(referenceId))
            return loc;
    }
}

public Info getInfoByReferenceId(String referenceId)
{
    for(Info info : infoList)
    {
        if(info.getReferenceId().equals(referenceId))
            return info;
    }
}

Then you just need to create one Map and call getType() and getLocation()

Map<String, String> typeToLocation = new HashMap<String, String>();

for(String refID : referenceIds)
{
   Location loc = getLocationByReferenceId(refID);
   Info    info = getInfoByReferenceId(refID);

   typeToLocation.put(info.getType(), loc.getLocation());
}

I know this wasn't exactly what you were looking for, but I hope it helps.

Upvotes: 0

Related Questions