I ran across some code recently at work (recreated to be similar to what I am dealing with) similar to the code below
Is there a way I can rework the code below to use one data structure (with performance in mind)?
Here is some code to illustrate what I mean:
public class ObjectMapper {
private Map<UUID,Integer> uuidMap;
private Map<Integer,UUID> indexMap;
public ObjectMapper(){
uuidMap = new HashMap<UUID,Integer>();
indexMap = new HashMap<Integer,UUID>();
}
public void addMapping(int index, UUID uuid){
uuidMap.put(uuid, index);
indexMap.put(index, uuid);
}
.
.
.
public Integer getIndexByUUID(UUID uuid){
return uuidMap.get(uuid);
}
public UUID getUUIDByIndex(Integer index){
return indexMap.get(index);
}
}
This is answered here with the recommendation to use BiMap from Google Collections
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With