Matt
Matt

Reputation: 1812

Inputting Arrays into a Hashmap

I feel like this is a very simple question, but I couldn't find an answer.

Can you input an array object to the put method of a HashMap?

Example:

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. The arrays are not initialized as shown below, but contain unknown values (this is just easier for illustrating the result).

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

I want the key-value pairs of the HashMap to be:

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

Can this be achieved with something like map.put(keys, values)? I know this doesn't work, you should get an error like "The method put(Integer, String) in the type HashMap is not applicable for the 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]);
}

Upvotes: 5

Views: 11966

Answers (5)

Caesar Ralf
Caesar Ralf

Reputation: 2233

There isn't besides the good 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 the sake of readability :P Not the most performatic solution, but it's cool to read it :)

Anyway, prefer the good old for. I like to create this kind of constructs only for fun, it's not intended to be used in production code, as it makes lots of cycles just to avoid a little imperative code :)

Upvotes: 0

Sreenivas M
Sreenivas M

Reputation: 167

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

Upvotes: 0

Kevin Bowersox
Kevin Bowersox

Reputation: 94429

In my opinion you have already discovered a pretty 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]);
}

Upvotes: 2

asifsid88
asifsid88

Reputation: 4701

If you need this behavior then you do need to parse each and every element to put them into HashMap. The worst case complexity will be O(n) and you cannot reduce that. You need to touch all the elements to populate HashMap
Below code (As mentioned by you) is the only work-around

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

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272217

I can't imagine that

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

could be made much more efficient. If it's the sort of thing you're going to do often then I'd perhaps write a helper object around Map.

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

Upvotes: 5

Related Questions