Interfaces of Collections in JAVA

Collection Interfaces

 
A simple collection of interface and class hierarchies is given below.
 
The-interface-and-class
 

Interfaces

 
There are two groups of interfaces.
  1. Collections
  2. Maps 
Here is the graphical representation of Collection's interfaces
 
CollectionInterfaces
 
Here is the graphical representation of Map's interfaces
 
MapInterfaces
 

Iterable Interface

 
In Java 5 there is now an iterable interface (java.lang.Iterable). It is the root interface of the Java collection Classes. The collection interface extends iterable, so all subtypes of Collection also implement the iterable interface.
 
The Syntax of the Iterable interface is given below
  1. public interface Iterable<T>
A collection provide an iterator which allows a sequential access to the element of collection. An iterator can be obtained by calling the following method of collection interface:
  1. public Iterator iterator()
or
  1. public interface Iterable<T> {  
  2.   public Iterator<T> iterator();      
  3. }
Methods of Iterator
  1. public boolean hasNext()  
  2. public Object next()  
  3. public Object remove()  

ListIterator Interface

 
This is the subinterface of iterator.which allows the programmer to travel in the list in either direction and make modifications to the underlying list.
 
Methods of ListIterator
  1. public boolean hasNext()  
  2. public boolean hasPrevious()  
  3. public Object next()  
  4. public Object previous()  
  5. public Int nextIndex()  
  6. public Int previousIndex()  
  7. public void remove()  
  8. public void add(Object o)  
  9. public void set(Object o)  

Collection Interface

  
The Collection interface (java.util.Collection) is one of the root interfaces of the Java collection classes. Though you do not instantiate a collection directly, but rather a subtype of collection, you may often treat these subtypes uniformly as a collection.
 
Collection myCollection = new HashSet();
 
The Collection interface can be a generic  like this:
 
Collection <String> myCollection = new HashSet<String>();
 
An example of collection interface is given below:
  1. import java.util.Collection;  
  2. import java.util.Iterator;  
  3. public class MyCollection  
  4. {  
  5.   public static void doSomething(Collection collection)   
  6.   {  
  7.     Iterator iterator = collection.iterator();  
  8.     while(iterator.hasNext())  
  9.    {  
  10.       Object object = iterator.next();  
  11.       //do something to object here...  
  12.     }  
  13.   }  
  14. }   

Collection Subtypes

 
The following interfaces (collection types) extends the collection interface:
  • List
  • Set
  • SortedSet
  • NavigableSet
  • Queue
  • Deque
A description of these interfaces is given below.
 
The collection interface is a group of objects, with duplicates allowed.
 
Set extends Collection but forbids duplicates.
 
List extends Collection and also allows duplicates and introduces positional indexing.
 
Map extends neither Set nor Collection
 
Interfaces Description Concrete Classes
Collection A basic interface that defines the normal operation that allows a collection of objects to be maintained or handle as single elements.  
Set (Unique Element no order)
 
 
A set of unique elements . HashSet
 
LinkedHashSet
SortedSet (Unique element but in sorted order)
A set in which the elements are stored  in some order. TreeSet
List (Duplicate elements but in insertion order) A sequence of elements that need not be unique. Vector
 
ArrayList
 
LinkedList
Map (Unique Keys) A basic interface that defines operation for maintaining mapping of keys to values. HashMap
 
LinkedHashMap
 
HashTable
SortedMap (Unique keys but in sorted order) This interface maintaining mapping in sorted in key order. TreeMap
 

Queue

 
A Queue is a collection for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, removal, and inspection operations. 
  1. public interface Queue<E> extends Collection<E>   
  2. {  
  3.     E element();  
  4.     boolean offer(E e);  
  5.     E peek();  
  6.     E poll();  
  7.     E remove();  
  8. }  

Deque

 
The Deque interface is a subtype of the Queue interface. It represents a queue where you can insert and remove elements from both ends of the queue.
 
Thus, "Deque" is short for "Double Ended Queue" and is pronounced "deck",like a deck of cards. NavigableSet The NavigableSet interface is a subtype of the SortedSet interface. It behaves like a SortedSet with the exception you have navigation methods available in addition to the sorting mechanisms of the SortedSet.
 

Map interfaces

 
The Map interface represents a mapping between a key and a value. the Map interface is not a subtype of the collection interface.
 
Therefore it behaves a bit different from the rest of the collection types.
 
Example of Map Interface
 
Step 1 - Create a Java project and give the name 
 
Step 1
 
Step 2 - Here the name is Collection Demo.
 
Step 2
 
Step 3 - Create the Class by clicking class,a new window is opened, and give the name of the class.
 
Step 3
 
Step 4 - Here the name of the class is DemoMap. In this program we insert some values in to the HashMap by using put() method. The syntax of put() method is defined below. 
 
Object put (Object key, Object value)
 
We also use size()method in this program. This method find out the size of  the HAshMap. Here we also use iterator.  
  1. import java.util.*;  
  2. import java.util.Map.Entry;  
  3. public class DemoMap {  
  4.     public static void main(String args[]) {  
  5.         HashMap dm = new HashMap();  
  6.         System.out.println(dm.size());  
  7.         dm.put("1001""aa");  
  8.         dm.put("1002""bb");  
  9.         dm.put("1003""cc");  
  10.         dm.put("1004""dd");  
  11.         dm.put("1005""ee");  
  12.         Set s = dm.entrySet();  
  13.         Iterator en = s.iterator();  
  14.         while (en.hasNext()) {  
  15.             Map.Entry e = (Map.Entry) en.next();  
  16.             String k = (String) e.getKey();  
  17.             String v = (String) e.getValue();  
  18.             System.out.println(k + "=" + v);  
  19.         }  
  20.         System.out.println(dm.size());  
  21.     }  
  22. }  
Step 5 - Compile and run the program. The output of the program is given below.
 
OutputDemoMap