Putting arrays into a hashmap

It seems to me that this is a very simple question, but I could not find an answer.

Can you inject an array object into the put method of a HashMap?

Example:

Let's say you have a HashMap:
HashMap<Integer, String> map = new HashMap <Integer, String>();

You have an array of integers and an array of strings conveniently given. Arrays are not initialized as shown below, but they contain unknown values ​​(this is just easier to illustrate the result).

int[] keys = {1, 3, 5, 7, 9};
String[] values = {"turtles", "are", "better", "than", "llamas"};

      

I want HashMap key-value pairs to be:

1, turtles
3, are
5, better
7, than
9, llamas

      

Could this be achieved with something like map.put(keys, values)

? I know it doesn't work, you should get an error like "The put (Integer, String) method in the HashMap type is not applicable for arguments (int [], String [])". I just want something more efficient, elegant, or compact than:

for (int i=0; i < keys.length; i++) {
    map.put(keys[i],values[i]);
}

      

+3


source to share


5 answers


I can't imagine that

for (int i=0; i < keys.length; i++) {
    map.put(keys[i],values[i]);
}

      



can be significantly more effective. If this is something you are going to do often, then I might write a helper around it Map

.

Note that Map.putAll () exists if the values ​​you want to add are already on the map.

+3


source


If you want this behavior, you need to parse each element in order to put them in the HashMap. The worst case will be O (n) and you cannot reduce that. You need to touch all elements to fill in the HashMap
Below code (as mentioned by you) is the only working part



for (int i=0; i < keys.length; i++) {
    map.put(keys[i],values[i]);
}

      

0


source


In my opinion, you've already found a fairly straight forward approach to this problem.

HashMap<Integer, String> map = new HashMap <Integer, String>();
int[] keys = {1, 3, 5, 7, 9};
String[] values = {"turtles", "are", "better", "than", "llamas"};

for(int i = 0; i < keys.length; i++){
    map.put(keys[i], values[i]);
}

      

0


source


There is no other way to directly put arrays in a Map. You need to iterate over each element of the array and insert a key, value pair into the map.

0


source


There is no other than the old old for

, but you can always implement your own solution. For a really verbose approach:

 public class MapFromArrayBuilder {
     private MapFromArrayBuilder() {}

     public static <K, V> Binder<K, V> map(K[] keys, V[] values) {
         return new Binder(keys, values);
     }   

     public static class Binder<K, V> {
         private final V[] values;
         private final K[] keys;

         public Binder(K[] keys, V[] values) { this.keys = keys; this.values = values; }

         public void into(Map<K, V> map) {
             for (int i = 0; i < keys.length; i++) {
                 map.put(keys[i], values[i]);
             }   
         }   
     }       

      

with it you could do something like

 public static void main(String[] args) {
     Map<Integer, String> leMap = new LinkedHashMap<Integer, String>();

     map(new Integer[] {0}, new String[] {"teste"}).into(leMap);

     for (Integer key : leMap.keySet()) {
         System.out.format("%d = %s\n", key, leMap.get(key));
     }   
 }  

      

But it creates objects just for readability: P Not the most efficient solution, but great to read.

In any case, prefer the good old one for

. I like to create constructs like this just for fun, but not meant to be used in production code as it does a lot of loops to avoid a little imperative code :)

0


source







All Articles