After knowing the meaning and purpose of Collections framework, let us know the basic interfaces, derived classes and their properties. Java data structures are nothing but subclasses of Collection and Map interfaces. Following hierarchy gives all the interfaces of Collections framework.
Map and SortedMap form a separate hierarchy and not connected with Collection interface. Bu still, they are treated as part of collections framework. It is done so to get interoperability with collections and legacy classes. Observe, interfaces Map and SortedMap are not derived from Collection interface. Collection being at the top of the hierarchy, it gives generic methods for all the derived classes.
Note: Framework name is collections and the interface name is Collection. Do not get confused; later, we get one more class called Collections.
Description of fundamental interfaces of Collections Hierarchy
A table is given showing the core interfaces with their implemented classes with brief description.
Interface | Brief Description | Derived Classes |
---|---|---|
Collection | It forms the root interface and comes with methods that define basic operations and makes all DS as one unit (called by a common name) | Majority of all DS come under this interface |
Set | Subclasses of this interface have a common feature of not allowing duplicate elements | HashSet, LinkedHashSet |
SortedSet | Subclasses of this interface come with a common feature of printing the elements in ascending order implicitly. Being sub interface of Set, it allows only unique elements | TreeSet |
List | Subclasses allow duplicate elements | LinkedList, ArrayList, Vector |
Map | Allows the subclasses to store key/value pairs. Does not allow duplicate keys. | HashMap, LinkedHashMap, Hashtable |
SortedMap | Prints elements in ascending order of keys. Being a sub interface of Map, it inherits the property of not allowing the duplicate keys. | TreeMap |
Table: Description of Main interfaces of Collections Hierarchy
The elements of List can be accessed with their index numbers.
Collection classes store elements of single entities, but Map stores key/value pairs; to store a value, a key must be supplied along. SortedSet and SortedMap print their stored elements in ascending order (known as natural order) by default. The order can be customized explicitly using Comparator interface or Comparable interface.
Following figure displays the main interfaces with relation to their derived classes.
As it can be observed, there comes some abstract classes in between interfaces and concrete classes (DS classes). They add some extra features to subclasses other than inherited from interfaces.
Following table gives the properties of derived classes.
Data Structure | Interface | Duplicates | Methods available | DS that inherits |
---|---|---|---|---|
HashSet | Set | Unique elements | equals(), hashCode() | Hashtable |
LinkedHashSet | Set | Unique elements | equals(), hashCode() | Hashtable, doubly-linked list |
TreeSet | SortedSet | Unique elements | equals(), compareTo() | Balanced Tree |
ArrayList | List | Duplicates allowed | equals() | Resizable array |
LinkedList | List | Duplicates allowed | equals() | Linked list |
Vector | List | Duplicates allowed | equals() | Resizable array |
HashMap | Map | Unique keys | equals() and hashCode() | Hash table |
LinkedHashMap | Map | Unique keys | equals() and hashCode() | Hash table and doubly-linked list |
Hashtable | Map | Unique keys | equals(), hashCode() | Hash table |
TreeMap | SortedMap | Unique keys | equals(), compareTo() | Tree Map |
There are two distinct hierarchies monitored by Collection and Map. Map is also placed in collections framework to have interoperability.
Basic Features of Main Interfaces of Collections Hierarchy
Core collection interfaces are the base to Java Collections framework. They include the interfaces Collection, Set, List, Queue and Map on which all the data structures are built.
- Collection is the root interface for all the hierarchy (except Map).
- Set interface unique feature is that it does not accept duplicate elements. That is, no two elements will be the same.
- SortedSet interface is derived from Set interface and adds one more feature that the elements are arranged in sorted order by default.
- List interface permits duplicate elements.
- Queue interface holds elements and returns in FIFO order.
- Map adds the elements in key/value pairs. Duplicate keys are not allowed, but duplicate values are allowed. One key can map one value only.
- SortedMap interface is a particular case of Map. Keys are sorted by default.
Collection is an interface and collections are the classes derived from Collection class. Moreover, there is a class called Collections.
http://way2java.com/collections/java-interface-collection/
http://way2java.com/collections/java-collections-framework/
http://way2java.com/collections/collections-api-methods/
Hi
What is the difference between HashMap and HashTable? I’m not getting any difference between these two. Please explain.
See this link:
http://way2java.com/collections/hashtable/hashtable-vs-hashmap/
If the ListIterator Interface implements the Iterator Interface, It should Implement the basic methods of the List Interface right !
Then why ListIterator Interface is not used to iterate through the set??
Iterator interface is not derived from List interface.
Thanks for giving great explanation. I have two questions
where the iterator and list iterator come into the above hierarchy?
where is the comparator and comparable interface in the above hierarchy?
They are not part of hierarchy as they are not DS but used to print the elements.
Can you please explain me why the listIterator() method is put in List Interface but not the Collection interface?If the same method had been put in the Collection interface then both the Set and List could be use it.
ListIterator is meant for to use by List classes only. Set is not permitted to use. Set should use Iterator only.
Sir,I know that Set is not permitted to use ListIterator interface.But why question is why?why only List has permission to use ListIterator interface not the Set..
listiterator is designed for only list.
Collections “Interface->abstract class->concrete class” hierarchy is great.
I would request you to please also post the Map (Interface->abstract class–>concrete class) hierarchy. It would be really nice that way to learn map hierarchy as well.
Good suggestion. I will incorporate like this shortly.
Great !
Q>what is the interface name in collection framework which does not allow duplicate value and follow natural sorting order?
option are:
1>Set
2>Map
3>Collection
4>List
It is TreeSet, a subclass Set inteface. So that answer can be 1.
Set implementations does not allow duplicate values. SortedSet, a sub interface of Set prints in natural order implicitly.
can u explain why Map is not under collection heirachy
All the subclasses of Collection interface stores values in single entities. But Map stores in key/value pairs. For this reason, it is places separately and not under Collection interface.