Need a Java map/table with multiple keys to one value. Value is commonly altered

前端 未结 5 1316
旧巷少年郎
旧巷少年郎 2021-01-04 10:13

What I need is a collection which allows multiple keys to access a single object.

I need to apply frequent alterations to this object.

It also must be effici

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-04 10:57

    Your Question actually got me thinking of making this class to handle such a thing. I am currently working on a 2D game engine and your question completely made me think of exactly what I needed.

    By the way you've worded it, I believe what you want is;

    An object that holds keys and values, but you can also get the common keys values hold (I use this object specifically to cut down on cpu at the cost of using just a little more memory.)

    This Class' The K type is the Primary Key type. The T type is the HashSet Value Type.

    The way you implement and use this object is:

    MapValueSet mainmap = new  
    
    MapValueSet()
    HashSet tags = new HashSet();
           public void test(){
                ObjectType1 = new ObjectType1();
                ObjectType2 = new ObjectType2();
    
                tags.add(mainmap.put(ObjectType1,ObjectType2);
                mainmap.get(ObjectType1,Integer);
           } 
    

    You will need to hold the unique tags in a set or arraylist in any class you implement this because if you didn't you'd be storing entities and not know which one was which. So store the integer you get from the put() method into an arraylist or set, and iterate through that.

    You can check this Class's values if they exist, or which key objects the value is set to.

    Here is the Class MapValueSet;

    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.Map;
    
    public class MapValueSet {
    
            Indexer indxK = new Indexer();
            Indexer indxT = new Indexer();
    
            Map kTags = new HashMap();
            Map tTags = new HashMap();
    
            Map> MapK = new HashMap>();
    
            Map> MapT = new HashMap>();
    
    public int put(K k, T t){
        int tag = -1;
        if(!kTags.containsKey(k)){
            kTags.put(k, indxK.getNextTag());
        }
    
        if(!MapK.containsKey(kTags.get(k))){
            MapK.put(kTags.get(k), new HashSet()); 
        }
    
        if(!tTags.containsKey(t)){
            tTags.put(t, tag = indxT.getNextTag());
        }
    
        if(!MapT.containsKey(tTags.get(t))){
            MapT.put(tag = tTags.get(t), new HashSet());
        }       
            MapK.get(kTags.get(k)).add(tTags.get(t));
            MapT.get(tag = tTags.get(t)).add(kTags.get(k)); 
    
        return tag;
    }
    
           @SuppressWarnings("unchecked")
             public T get(K k, int tag){
                Object[] tArr = tTags.keySet().toArray();
                for(int i = 0; i < tArr.length; i++){
                  if(tTags.get((T)tArr[i])== tag){
                        return (T)tArr[i];
                 }
               }
               return null;
            }
    
            public boolean removeAtKey(K k, T t){
                    int kTag = -1;
                    int tTag = -1;
    
                    if(kTags.get(k) != null){
                    kTag = kTags.get(k);
                    }
    
                    if(tTags.get(t) != null){
                    tTag = tTags.get(t);
                    }
    
                    if(kTag == -1 || tTag == -1){
                            System.out.println("Keys are Blank at: removeAtKey(k,t)");
                            return false;
                    }
    
                    boolean removed = false;
    
                            if(MapK.get(kTag) != null){
                                    removed = MapK.get(kTag).remove(tTag);
                            }
                            if(MapT.get(tTag) != null){
                                    MapT.get(tTag).remove(kTag);
                            }
    
                            if(!MapK.containsKey(kTag)){
                                    kTags.remove(k);
                                    indxK.removeTag(kTag);
                            }
    
                            if(MapK.containsKey(kTag)){
                                    tTags.remove(t);
                                    indxT.removeTag(tTag); 
    
                            }
    
                    return removed;
            }
    
            public void removeAtValue(T t){
                    if(!tTags.containsKey(t)){
                            return;
                    }
                    Object[] keyArr = MapT.get(tTags.get(t)).toArray();
    
                    for(int i = 0; i < keyArr.length; i++){
                            MapK.get(keyArr[i]).remove(tTags.get(t));
                    }
    
                            indxT.removeTag(tTags.get(t));
                            MapT.remove(tTags.get(t));
                            tTags.remove(t);
            }
    
            public boolean mapContains(T t){
                    if(tTags.get(t) == null){
                            return false;
                    }
                    int tTag = tTags.get(t);
    
                    return MapT.get(tTag) != null && !MapT.get(tTag).isEmpty();
            }
    
            public boolean containsKey(K k){
    
                    if(kTags.get(k) == null){
                            return false;
                    }
    
                    return MapK.containsKey(kTags.get(k));
            }
    
            public boolean keyContains(K k, T t){
    
                    if(kTags.get(k) != null && tTags.get(t) != null){
                            return MapK.get(kTags.get(k)).contains(tTags.get(t));
                    }
    
                    return false;
    
            }
    
            @Override
            public String toString(){
    
                    String s = "";
    
                    s = s+ "Key      Map: " + MapK.toString() + "\n";
                    s = s+ "Value    Map: " + MapT.toString() + "\n";
                    s = s+ "KeyTag   Map: " + kTags.toString() + "\n";
                    s = s+ "ValueTag Map: " + tTags.toString() + "\n";
                    s = s+ "KeyTag   List: " + indxK.activeSet().toString() + "\n";
                    s = s+ "ValueTag List: " + indxT.activeSet().toString();
    
                    return s;              
            }
    
    
    }
    

提交回复
热议问题