Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two Way Mapping using single Data Structure

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);
    }


}
like image 609
mainstringargs Avatar asked Jan 24 '23 15:01

mainstringargs


1 Answers

This is answered here with the recommendation to use BiMap from Google Collections

like image 125
Yishai Avatar answered Jan 31 '23 06:01

Yishai