Converting string arrays into Map

前端 未结 5 620
没有蜡笔的小新
没有蜡笔的小新 2020-11-29 13:46

I have two string arrays keys and values

String[] keys = {a,b,c,d};

String[] values = {1,2,3,4};

What is the fastest way to convert them i

5条回答
  •  [愿得一人]
    2020-11-29 14:03

    Constant time lookup from the start

    If you are looking for a Map that retrieves the value associated with a key in constant time (meaning without having to look at most values), then you cannot do much faster, because the arrays need to be processed.

    However, you can use a utility already written that way : com.google.common.collect.Maps.uniqueIndex

    Instantaneous conversion, Linear time lookup

    If you are ok with a Map that searches the array for the key every time, then you can create the Map instantly using your two arrays, by defining a new class that implements the Map interface :

    class TwoArrayMap implements Map {
    
       private final String[] keys;
       private final String[] values;
       // If you want to enable to add more key value pairs to your map, and
       // want to make the process faster, you could use ArrayLists instead of arrays
    
       public TwoArrayMap(String[] array1, String[] array2){
           if(array1 == null || array2 == null || array2.length < array1.length)
              throw new IllegalArgumentException();
           keys = array1;
           values = array2;
           // Alternatively, you could want to clone the arrays, to 
           // make sure they are not modified, using array1.clone(), etc
       }
    
       public String get(String key){
    
           for(int i=0; i myMap = new TwoArrayMap(keys, values);
    


    Lazy conversion, constant time lookup after conversion

    Another approach would be to do it "lazily", meaning modify the above class, so that it keeps a reference to a HashMap internally, and fills it only when it is looking up elements :

    class TwoArrayMap implements Map {
    
       private final Map hashmap;
       private int maxIndexAlreadyTransferred = -1;
    
       private final String[] keys;
       private final String[] values;
    
       public TwoArrayMap(String[] array1, String[] array2){
           if(array1 == null || array2 == null || array2.length < array1.length)
              throw new IllegalArgumentException();
           hashmap = new HashMap<>();
           keys = array1;
           values = array2;
           // Alternatively, you could want to clone the arrays, to 
           // make sure they are not modified, using array1.clone(), etc
       }
    
       public String get(String key){
    
           if(hashmap.containsKey(key))
                return hashmap.get(key);
    
           String k, value;
           while( maxIndexAlreadyTransferred + 1 < keys.length ){
                 k = keys[ maxIndexAlreadyTransferred + 1 ];
                 value = values[ maxIndexAlreadyTransferred +1 ];
                 if(!hashmap.containsKey(k))
                     hashmap.put( k, value );
                 maxIndexAlreadyTransferred++;
                 if(key == null && k == null || key != null && key.equals(k) )
                    return value;
           }
           return null;                     
       }
    
       public String put(String key, String Value) {
            hashmap.put(key, value);
       }
    
    }
    

    This solution would mean :

    • an instantaneous creation of your new object
    • linear time lookup for the first times you will query it, until everything is transferred
    • constant time lookup after that, behaving as a hash table

提交回复
热议问题