Back to Java Course

Java Course

If we want to represent a group of individual object as a single entities then we should go for collection.

Difference between Array and Collection

Array

  • Array are fixed in size.
  • Not have proper memory management.
  • Performance is relatively good.
  • No underlying data structure.
  • Readymade method support not available.
  • Primitive and object both type available.
  • Only Homogeneous object.

Collection

  • Collection are growable in size.
  • Good memory management.
  • Collection are not good in performance.
  • Both Homogeneous and Heterogeneous object allowed.
  • Readymade method available.
  • Only objects are allowed.
  • Standard data structure.

Difference between Collection and Collections

Collection is an interface, if we want to represent a group of individual object as a single entity then we should go for Collection.

Collections is an utility class present in java.util package. It defines several static utility methods for collection objects like a) sort b) EMPTY_LIST c) binarySearch d) reverse e) shuffle f) swap g) copy h) min i) max j) unmodifiableCollection k) synchronizedCollection

Note: forEach, removeIf, replaceAll, stream, and parallelStream are not methods of the Collections utility class — they are default methods defined on the Collection/List interfaces themselves (added in Java 8), so they're called directly on a collection object, e.g. list.forEach(...), not Collections.forEach(...).

Collection Hierarchy

screenshot of the app

Interfaces in Collections

  • Collection: the foundational interface of the Collections Framework.

screenshot of the app

  • List: extends Collection. Defines a collection that stores an ordered sequence of elements.
  • Queue: extends Collection. Defines a collection where insertion and removal each occur only at a single end. Typically elements are ordered as a FIFO queue, although this can be altered to accommodate priority queues or LIFO stacks.
  • Deque: extends Queue. Short for "double ended queue", it defines a queue where insertion and removal can occur at either end.
  • Set: extends Collection. Defines a collection that does not allow duplicate elements.
  • SortedSet: extends Set. Defines a set in which elements are sorted either according to their natural ascending order or by the use of a Comparator.
  • NavigableSet: extends SortedSet. Defines a sorted set which supports searching for elements based on closest matches. For instance, this interface defines the higher(E e) method, which returns the smallest element larger than the argument e.

Non-Abstract Classes in Collections

  • ArrayList: extends AbstractList, and implements the List interface. Similar to the Vector class described above, it offers a more fully featured, resizable alternative to Java's native arrays.
  • LinkedList: extends AbstractSequentialList, and implements the Deque interface. Creates a doubly Linked list. Linked lists are somewhat akin to arrays, but don't require continuous blocks of memory.
  • ArrayDeque: extends AbstractCollection, and implements the Deque interface. This is the class you'll most likely want to use for queues, double-ended or otherwise, though it can also be used for stacks.
  • PriorityQueue: extends AbstractQueue, and implements the Queue interface. As the name implies, you'll want to use it for queues that are ordered according to priority.
  • HashSet: extends AbstractSet, and implements the Set interface. It is a set whose elements are backed by a HashMap under the hood. Due to the unpredictable nature of hashing, its elements are not reliably ordered — thus, it is useful primarily for sets which do not need to be sorted.
  • LinkedHashSet: extends HashSet, and implements the Set interface. It is similar to HashSet, but also maintains a linked list of the entries in the order in which they were inserted, allowing you to iterate through them thusly.
  • TreeSet: extends AbstractSet, and implements the NavigableSet interface. It is similar to HashSet, but is backed by a TreeMap. This allows allows the set to be sorted, either in natural ascending ordering or by use of a Comparator.
  • EnumSet: extends AbstractSet. It is a set which is specially designed to work with enum types, and where all members of the set must come from the same enum.

Abstract Classes in Collections

  • AbstractCollection: Implements most of the Collection interface.
  • AbstractList: Extends AbstractCollection and implements most of the List interface.
  • AbstractSequentialList: Extends AbstractList. As the name implies, it is meant to facilitate sequential, rather than random, access of its elements.
  • AbstractQueue: Extends AbstractCollection and implements some of the Queue interface.
  • AbstractSet: Extends AbstractCollection and implements most of the Set interface.