Question: I'm using a `HashMap` and I haven't been able to get a straight answer on how the `get()` method works in the case of collisions.

Let's say `n > 1` objects get placed in the same **key**. Are they stored in a `LinkedList`? Are they overwritten so that only the last object placed in that key exists there anymore? Are they using some other collision method?

If they are placed in a `LinkedList`, is there a way to retrieve that entire list? If not, is there some other built in map for **Java** in which I can do this?

For my purposes, separate chaining would be ideal, as if there are collisions, I need to be able to look through the list and get information about all the objects in it. What would be the best way to do this in **Java**? 

Thanks for all your help!

Answer: 
> 
> Are they overwritten so that only the last object placed in that key exists there anymore?
> 
> 
> 

Yes, assuming you're putting multiple values with the same key (according to `Object.equals`, not `Object.hashCode`.) That's specified in the [`Map.put` javadoc](http://docs.oracle.com/javase/6/docs/api/java/util/Map.html#put%28K,%20V%29):

> 
> If the map previously contained a mapping for the key, the old value is replaced by the specified value.
> 
> 
> 

If you want to map a key to multiple values, you're probably better off using something like Guava's [`ListMultimap`](https://code.google.com/p/guava-libraries/wiki/NewCollectionTypesExplained#Multimap), [`ArrayListMultimap`](http://docs.guava-libraries.googlecode.com/git-history/release/javadoc/com/google/common/collect/ArrayListMultimap.html) in specific, which maps keys to lists of values. (Disclosure: I contribute to Guava.) If you can't tolerate a third-party library, then really you have to have a `Map<Key, List<Value>>`, though that can get a bit unwieldy.
> 
> Let's say n > 1 objects get placed in the same key. Are they stored in a linked list? Are they overwritten so that only the last object placed in that key exists there anymore? Are they using some other collision method?
> 
> 
> 

There could be single instance for the same key so the last one overrides the prior one

```
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1);
map.put("a", 2);// it overrides 1 and puts 2 there

```

chaining comes where there turns the same hash for different keys

---

**See**

* [Java papers hash table working](http://javapapers.com/core-java/java-hashtable/)