Collection in Java

Collections in java is a framework that provides an architecture to store and manipulate the group of objects.

Collection framework is a set of classes and interfaces which provide unified model (combine model) of working with different types of Collections. At the core of collection framework is an interface names Collection. This interface defines common functionality of all Collections. Java.util package contain classes and interfaces of collection framework. Classes like ArrayList, Vector,LinkedList, HashSet, TreeSet, LinkedHashSet, HashMap etc. use to add, reterive, remove, find and iterate the objects

Note :-A Collection object not stores the copies of the other object, it simply stores the references of the other objects. Main aim of collection object is to handle object only, not the primitive data type. So we can not store primitive data type in to Collection.

What is Collection in java

Collection represents a single unit of objects i.e. a group.

What is framework in java

  • provides ready made architecture.
  • represents set of classes and interface and is optional.

Collection Framework Hierarchy

The java.util package contains all the classes and interfaces for Collection framework.


Methods of Collection interface

There are many methods declared in the Collection interface. They are as follows:

Number Method Description
1 public boolean add(Object element) is used to insert an element in this collection.
2 public boolean addAll(collection c) is used to insert the specified collection elements in the invoking collection.
3 public boolean remove(Object element) is used to delete an element from this collection.
4 public boolean removeAll(Collection c) is used to delete all the elements of specified collection from the invoking collection
5 public public boolean retainAll(Collection c) is used to delete all the elements of invoking collection except the specified collection.
6 public int size() return the total number of elements in the collection
7 public void clear() removes the total no of element from the collection.
8 public boolean contains(object element) is used to search an element.
9 public boolean containsAll(Collection c) is used to search the specified collection in this collection.
10 public Iterator iterator() returns an iterator.
11 public Object[] toArray() converts collection into array.
12 public boolean isEmpty() checks if collection is empty.
13 public boolean equals(Object element) matches two collection.
14 public int hashCode() returns the hashcode number for collection.

Iterator interface

Iterator interface provides the facility of iterating the elements in forward direction only. Methods of Iterator interface There are only three methods in the Iterator interface. They are:

  1. public boolean hasNext() it returns true if iterator has more elements
  2. public object next() it returns the element and moves the cursor pointer to the next elemen
  3. void remove() it removes the last elements returned by the iterator. It is rarely used.

Most important Interview Question in collection

Java Collections Interview Questions
  • Difference between Iterator and ListIterator?
  • Difference between Vector and ArrayList?
  • Difference between Iterator and Enumeration?
  • Difference between Comparable and Comparator?.
  • Difference between Collection and Collections?
  • What is the role of equals() method of Object class in Collection framework?
  • What is the role of hashCode() methos of Object class in Collection framework?
  • What is the relation between equals() and hashCode() method?
  • What is the role of load factor in hasing?
  • What is complexity of a searching order in worst case in a List such as ArrayList, Vector, LinkedList, hashSet, HasMap, TreeSet, TreeMap?
  • What is the role of thread safety in Collection
  • What is type safe and why it is important?
  • How can a Collection is made synchronized?
  • How can a Collection is made unmodifiable?
  • How can a Collection is made singleton?
  • In which case ConcurrentModificationException is throws?
  • What is fail pass implementation, why Iterator is implemented to be fail?
  • How the implementation of Iterator and Enumeration is provided by Collection?

Topics in Java Collections Framework

Upgrade to the Latest Revision of Collections Framework

Collections Framework Enhancements in Java SE 7

Collections Framework Enhancements in Java SE 8