Collection (aka container) an object that groups multiple elements into a single unit. Collections are used to store, retrieve, manipulate, and communicate aggregate data. Hierarchy of Collection Framework
Important methods of Collection interface
- boolean add(E e)
Ensures that this collection contains the specified element (optional operation). - void clear()
Removes all of the elements from this collection (optional operation). - boolean isEmpty()
Returns true if this collection contains no elements. - Iterator<E> iterator()
Returns an iterator over the elements in this collection. - boolean remove(Object o)
Removes a single instance of the specified element from this collection, if it is present (optional operation). - int size()
Returns the number of elements in this collection.
List<E>, Set<E> & Queue<E> – Sub-interfaces of Collection<E>
In practice, we typically program on one of the sub-interfaces of the Collection interface: List<E>, Set<E>, or Queue<E>, which provide further specifications.
- List<E>: models a resizable linear array, which allows indexed access. List can contain duplicate elements. Frequently-used implementations of List include ArrayList, LinkedList, Vector and Stack.
- Set<E>: models a mathematical set, where no duplicate elements are allowed. Frequently-used implementations of Set are HashSet and LinkedHashSet. The sub-interface SortedSet<E> models an ordered and sorted set of elements, implemented by TreeSet.
- Queue<E>: models queues such as First-in-First-out (FIFO) queue and priority queue. It sub-interface Deque<E> models queues that can be operated on both ends. Implementations include PriorityQueue, ArrayDeque and LinkedList.
Map<K,V> Interface
The interface Map<K,V>, which takes two generic types K and V and read as Map of Key type K and Value type V, is used as a collection of of “key-value pairs”. No duplicate key is allowed. Frequently-used implementations include HashMap, Hashtable and LinkedHashMap. Its sub-interface SortedMap<K, V> models an ordered and sorted map, based on its key, implemented in TreeMap.
Take note that Map<K,V> is not a sub-interface of Collection<E>, as it involves a pair of objects for each element.
Generics allow us to pass type information, in the form of <type>, to the compiler, so that the compiler can perform all the necessary type-check during compilation to ensure type-safety at runtime.
For example, this statement with generics List<String> (read as List of Strings) and ArrayList<String> (read as ArrayList of Strings) informs the compiler that the List and ArrayList can hold only String object:
List<String> lst = new ArrayList<String>(); // read as List of Strings, ArrayList of Strings
In generics, instead of pass arguments, we pass type information inside the angle brackets <> to the compiler.
Iterator interface provides the facility of iterating the elements in forward direction only.
Important methods of Iterator interface
- boolean hasNext()
Returns true if the iteration has more elements. - E next()
Returns the next element in the iteration. - default void remove()
Removes from the underlying collection the last element returned by this iterator (optional operation).
ArrayList Class
Refer this post.
LinkedList Class
Refer this post.
Stack Class
Refer this post.
Queue Class
Refer this post.
Auto-Boxing & Auto-Unboxing (JDK 1.5)
A collection contains only objects. It cannot holds primitives (such as int and double). Although arrays can be used to hold primitives, they are not resizable.
To put a primitive into a collection (such as ArrayList), you have to wrap the primitive into an object using the corresponding wrapper class as shown below:
JDK 1.5 introduces a new feature called auto-boxing and auto-unboxing to resolve this problem, by delegating the compiler to do the job. For example:
// JDK 1.5
Integer intObj = 5566; // autobox from int to Integer
int i = intObj; // auto-unbox from Integer to int
Double doubleObj = 55.66; // autoboxing from double to Double
double d = doubleObj; // atuo-unbox from Double to double