Collections in Java
A Collection is a group of individual objects represented as a single unit. Java provides Collection Framework which defines several classes and interfaces to represent a group of objects as a single unit.
The Collection interface (java.util.Collection) and Map interface (java.util.Map) are the two main “root” interfaces of Java collection classes.
Need for Collection Framework :
Before Collection Framework (or before JDK 1.2) was introduced, the standard methods for grouping Java objects (or collections) were Arrays or Vectors or Hashtables. All of these collections had no common interface.
Accessing elements of these Data Structures was a hassle as each had a different method (and syntax) for accessing its members:
// Java program to show whey collection framework was needed
import java.io.*;
import java.util.*;
class Test
{
public static void main (String[] args)
{
// Creating instances of array, vector and hashtable
int arr[] = new int[] {1, 2, 3, 4};
Vector<Integer> v = new Vector();
Hashtable<Integer, String> h = new Hashtable();
v.addElement(1);
v.addElement(2);
h.put(1,"geeks");
h.put(2,"4geeks");
// Array instance creation requires [], while Vector
// and hastable require ()
// Vector element insertion requires addElement(), but
// hashtable element insertion requires put()
// Accessing first element of array, vector and hashtable
System.out.println(arr[0]);
System.out.println(v.elementAt(0));
System.out.println(h.get(1));
// Array elements are accessed using [], vector elements
// using elementAt() and hashtable elements using get()
}
}
Output:
1 1 geek
As we can see, none of these collections (Array, Vector or Hashtable) implement a standard member access interface. It was very difficult for programmers to write algorithms that can work for all kinds of Collections. Another drawback being that most of the ‘Vector’ methods are final, meaning we cannot extend the ’Vector’ class to implement a similar kind of Collection.
Java developers decided to come up with a common interface to deal with the above mentioned problems and introduced the Collection Framework in JDK 1.2.
Both legacy Vectors and Hashtables were modified to conform to the Collection Framework.
Advantages of Collection Framework:
- Consistent API : The API has a basic set of interfaces like Collection, Set, List, or Map. All classes (ArrayList, LinkedList, Vector, etc) that implement these interfaces have somecommon set of methods.
- Reduces programming effort: A programmer doesn’t have to worry about the design of Collection, and he can focus on its best use in his program.
- Increases program speed and quality: Increases performance by providing high-performance implementations of useful data structures and algorithms.
Hierarchy of Collection Framework
Collection Map / / \ \ | / / \ \ | Set List Queue Dequeue SortedMap / / SortedSet Core Interfaces in Collections Note that this diagram only shows core interfaces.
Collection : Root interface with basic methods like add(), remove(), contains(), isEmpty(), addAll(), ... etc. Set : Doesn't allow duplicates. Example implementations of Set interface are HashSet (Hashing based) and TreeSet (balanced BST based). Note that TreeSet implements SortedSet. 不允许重复元素,HashSet用hashing实现,TreeSet用平衡二叉树实现 List : Can contain duplicates and elements are ordered. Example implementations are LinkedList (linked list based) and ArrayList (dynamic array based) 可以存储重复元素,元素是有序的,LinkedList用linked list实现,ArrayList用动态数组实现。 Queue : Typically order elements in FIFO order except exceptions like PriorityQueue. 先进先出 Deque : Elements can be inserted and removed at both ends. Allows both LIFO and FIFO. 双端队列 Map : Contains Key value pairs. Doesn't allow duplicates. Example implementation are HashMap and TreeMap. TreeMap implements SortedMap. 不允许重复元素,有HashMap和TreeMap。 The difference between Set and Map interface is that in Set we have only keys, whereas in Map, we have key, value pairs.
We will soon be discussing examples of interface implementations.