# Generic Map in Java

 Jakob Jenkov Last update: 2015-09-02

Java's `Map` interface (`java.util.Map`) can be generified. In other words, you can set the specific type of both the keys and values in a generic `Map` instance. Here is an example:

```Map<Integer, String> set = new HashMap<Integer, String>;
```

This `Map` can now only accept `Integer` instances as keys, and `String` instances as values.

The generic type checks only exists at compile time. At runtime it is possible to tweak your code so that other instances can be inserted. This is a bad idea, though.

## Accessing a Generic Map

Adding and getting elements to a generic `Map` is done using the `put()` and `get()` methods, just like you have always done:

```Map<Integer, String> map = new HashMap<Integer, String>;

Integer key1   = new Integer(123);
String  value1 = "value 1";

map.put(key1, value1);

String value1_1 = map.get(key1);
```

So what is the big difference? Well, if you try to add a key, value pair that is not a `Integer, String` pair instance, to the `Map` in the example above, the compiler will complain. That's a pretty nice extra type check to have.

Also notice how it is not necessary to cast the `String` instance returned by the `get()` method. The compiler knows that this `Map` has `String` values, so casting is not necessary.

You can also use the new auto boxing features of Java 5 to make it easier to specify the `Integer` values, like this:

```Map<Integer, String> map = new HashMap<Integer, String>;

Integer key1   = 123;
String  value1 = "value 1";

map.put(key1, value1);

//or

map.put(123, value1);

String value1_1 = map.get(123);
```

## Iterating a Generic Map

A `Map` has two collections you can iterate. The key `Set` and the value `Set`. Most often you iterate the key `Set` and access the values for each key via the `Map.get()` method.

Here are two examples:

```Map<Integer, String> map = new HashMap<Integer, String>;

//... add key, value pairs to the Map

// iterate keys.
Iterator<Integer> keyIterator   = map.keySet().iterator();

while(keyIterator.hasNext()){
Integer aKey   = keyIterator.next();
String  aValue = map.get(aKey);
}

Iterator<String>  valueIterator = map.values().iterator();

while(valueIterator.hasNext()){
String aString = valueIterator.next();
}
```

Notice how it is not necessary to cast the object returned from the `iterator.next()` next call. Because the `Map` is generified (has a type), the compiler knows that it contains `Integer` instances for keys, and `String` instances for values. Therefore it is not necessary to cast the objects obtained from the `Map`, even if it comes from one of its `Iterator`'s.

You can also use the new for-loop, like this:

```Map<Integer, String> map = new HashMap<Integer, String>;

//... add key, value pairs to the Map

for(Integer aKey : map.keySet()) {
String aValue = map.get(aKey);
System.out.println("" + aKey + ":" + aValue);
}

for(String aValue : map.values()) {
System.out.println(aValue);
}
```

Notice how an `Integer` and a `String` variable is declared inside the parantheses of each for-loop. For each iteration (each element in the `Map`'s key set or value collection) this variable contains the current element (current Integer or String).

 Tweet Jakob Jenkov
Featured Videos High-Performance
Java Persistence
Close TOC All Trails Trail TOC Page TOC Previous Next
{"ft_lang_label":"__label__en","ft_lang_prob":0.61727417,"math_prob":0.9140851,"size":3147,"snap":"2023-40-2023-50","text_gpt3_token_len":759,"char_repetition_ratio":0.16353802,"word_repetition_ratio":0.118473895,"special_character_ratio":0.24976167,"punctuation_ratio":0.17563291,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95448893,"pos_list":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30],"im_url_duplicate_count":[null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-08T02:20:17Z\",\"WARC-Record-ID\":\"<urn:uuid:9e1c22d6-2f73-40d8-a53f-d0285a4a8a9d>\",\"Content-Length\":\"40015\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f5b3fac6-331a-43a0-89c7-b2a2f6d19a2e>\",\"WARC-Concurrent-To\":\"<urn:uuid:d4413631-5e7d-4992-83b8-4cddcc623cce>\",\"WARC-IP-Address\":\"157.90.167.190\",\"WARC-Target-URI\":\"https://jenkov.com/tutorials/java-generics/generic-map.html\",\"WARC-Payload-Digest\":\"sha1:6I66DIS6NHMWN4Z5ETDLT2ZKYFQKT6QN\",\"WARC-Block-Digest\":\"sha1:2VOB27CY7VVQNDAPWQTXDYNQY3J7UN6R\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100710.22_warc_CC-MAIN-20231208013411-20231208043411-00346.warc.gz\"}"}