Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does LinkedHashMap class implement Map interface? [duplicate]

Class HashMap implements Map interface :

public class HashMap<K,V> extends AbstractMap<K,V>
implements Map<K,V>, Cloneable, Serializable

Class LinkedHashMap extends HashMap that means it should implement Map interface by default. Why is it explicitly implementing Map interface?

public class LinkedHashMap<K,V>
extends HashMap<K,V>
implements Map<K,V>
like image 794
Shobhit_Geek Avatar asked May 01 '15 11:05

Shobhit_Geek


People also ask

Does LinkedHashMap allow duplicates?

A LinkedHashMap cannot contain duplicate keys. LinkedHashMap can have null values and the null key. Unlike HashMap, the iteration order of the elements in a LinkedHashMap is predictable.

Does LinkedHashMap use doubly-linked list?

Answer: LinkedHashMap in Java is implemented as a combination of the HashTable and LinkedList. It implements the map interface. It has a predictable iteration order. It internally uses a doubly-linked list for entries.

How linked HashMap is implemented in Java?

How LinkedHashMap Work Internally? Hash: All the input keys are converted into a hash which is a shorter form of the key so that the search and insertion are faster. Key: Since this class extends HashMap, the data is stored in the form of a key-value pair. Therefore, this parameter is the key to the data.

Is LinkedHashMap instance of Map?

Constructs an insertion-ordered LinkedHashMap instance with the same mappings as the specified map. The LinkedHashMap instance is created with a default load factor (0.75) and an initial capacity sufficient to hold the mappings in the specified map.


4 Answers

You are right: dropping Map<K,V> from linked hash map's declaration would not change anything. Although LinkedHashMap<K,V> would implement Map<K,V> simply because it extends HashMap<K,V>, the fact that linked hash map derives from a regular hash map is an implementation detail, not a hard requirement.

Implementing the Map<K,V> interface, on the other hand, is a fundamental requirement. This requirement would not disappear if the designers decided to implement LinkedHashMap<K,V> from scratch, or to rely on some other base class, e.g. a linked list.

That is why the designers of LinkedHashMap<K,V> mentioned Map<K,V> explicitly: if at some later day a base class would change due to redesign, the interface would stay in place.

like image 52
Sergey Kalinichenko Avatar answered Sep 18 '22 06:09

Sergey Kalinichenko


Well, it is probably for the sake of clarity in the documentation, it adds nothing to the code. It might also be because LinkedHashMap extends HashMap is an implementation detail, what is really important to know is that LinkedHashMap is a Map.

like image 35
Dici Avatar answered Sep 20 '22 06:09

Dici


Dici has it right, for the most part.

A long time ago, some of the lesser Java compilers could get a bit confused, and occasionally I added (to my code) interfaces that should have been derived from the parent hierarchy. Perhaps that had a hand in putting the interface in multiple places too (for the Java libraries) but that is pure speculation.

like image 29
Edwin Buck Avatar answered Sep 20 '22 06:09

Edwin Buck


Practically there is no difference. In my opinion, it was always the case in the JDK classes (the same pattern exists for List and its subclasses). The designers probably still haven't removed this redundant implementation so that nothing breaks in case for example someone relies on reflection to get information about the subtypes. For instance imagine you define the following:

class MyCustomMap<K, V> extends LinkedHashMap<K, V> implements Map<K, V> {

}

Then the below snippet outputs different results with and without the implements:

Class<?>[] interfaces = MyCustomMap.class.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
    System.out.println(interfaces[i]);
}

Output:

interface java.util.Map

Changing the definition to:

class MyCustomMap<K, V> extends LinkedHashMap<K, V> {

}

then no interface would be printed.

like image 43
M A Avatar answered Sep 22 '22 06:09

M A