Collections in Java

Collections in java is a framework to store and manipulate a collection or group of objects. A java collection represents a single group of objects. The framework provides with several operations that can be performed on this collection, such as searching, sorting, insertion, deletion and so on.

Collection is the root interface of the collection hierarchy while Collections is a class consisting of static methods which operate on or return collections.

Collection framework provides two interface hierarchies – Collection Hierarchy and the Map Hierarchy

Collection Hierarchy

collection hierarchy

Methods provided by the Collection Interface:

public boolean add(Object element) – is used to insert an element into a collection.
public boolean addAll(collection c) – is used to insert the specified collection elements in the invoking collection.
public boolean remove(Object element) – is used to delete an element from a collection.
public boolean removeAll(Collection c) – is used to delete all the elements of specified collection from the invoking collection.

public boolean retainAll(Collection c) – is used to retain all the elements of invoking collection that exist in the specified collection.

public int size() – returns the total number of elements in the collection.
public void clear() – removes all the elements from the collection.
public boolean contains(object element) – is used to search an element.
public boolean containsAll(Collection c) – is used to search the specified collection in this collection.
public Iterator iterator() – returns an iterator.

public Object[] toArray() – converts collection into array.
public boolean isEmpty() – checks if a collection is empty.
public boolean equals(Object element) – matches two collection.
public int hashCode() – returns the hashcode number for collection.

Map Hierarchy

A Map contains values based on the key i.e. key and value pair. Each pair is known as an Entry. Map contains only unique keys.

Map Hierarchy

Methods provided by the Map Interface:

public Object put(object key,Object value) – is used to insert an entry in this map.
public void putAll(Map map) – is used to insert the specified map in this map.
public Object remove(object key) – is used to delete an entry for the specified key.
public Object get(Object key) – is used to return the value for the specified key.
public boolean containsKey(Object key) – is used to search the specified key from this map.
public boolean containsValue(Object value) – is used to search the specified value from this map.
public Set keySet() – returns the Set view containing all the keys.
public Set entrySet() – returns the Set view containing all the keys and values.

Important points to note:

ArrayList – High Performing, non-synchronized, non thread-safe. Used preferably over Vectors. Efficient at list iterations and random element access.

Vectors – Low Performing, synchronized and thread-safe.

LinkedList – Provides sequential access, produces ordered list, efficient at inserting and deleting elements to the list.

HashMap – Unordered keys, non-synchronized, non thread-safe. Can contain a single null key and multiple null values. Fail-fast iterator. Used preferable over Hashtable. Can be synchronized using Map m = Collections.synchronizedMap(hashMap);

LinkedHashMap – Maintains the order of entries in which they were added.

Hashtable – Slower, Synchronized and thread-safe. Cannot contain null. Fail-safe enumerator.

TreeMap – Sorted based on the keys. The object keys must be comparable to each other. If the keys are of type String, there is no explicit need to implement Comparable interface, since String implements Comparable. But, if the key is of user-defined type like Employee, Student, etc., then we need to explicitly make the object comparable, by implementing the respective user-defined class with Comparable, else ClassCastException, as it tries to cast the concerned object to Comparable for sorting the TreeMap.

Sets – No duplicates, stores only objects and not in the form of key value pair. HashSet slower than HashMap.

Receive our updates to your inbox

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.