Collection in java

The java Collection is a framework that helps in storing and manipulating a group of objects,Collections are dynamic and they can grow their size dynamically at run time as the elements are added to it.

The Collection is a root interface available in java.util.package.This is Implemented by three interfaces in Java Program.

  • List
  • Sets
  • Queue

All the above three are data structures that allow to store and manipulate objects. And each one of them has slight differences.

List –List allows duplicate objects.

Sets – Does not allow duplicate objects.

Queue–It is a First in First out Data structure.

 

Implementation classes of Lists:

  • ArrayList
  • Linked List
  • Vector

ArrayList

  • It provides us dynamic arrays.
  • It implements the List interface.
  • The Arraylist class will allow duplicate elements.
  • The Arraylist class maintains insertion order.

Linked List

  • It is a linear data structure.
  • Linkedlist class will allow duplicate elements.
  • Linkedlist class maintains insertion order

Vector

  • It is a synchronized class.
  • It is a legacy class.
  • It contains many legacy methods.

 

The implementation class of sets is HashSet

HashSet

  • Hashset stores the elements by using a mechanism called hashing.
  • Hashset does not allow duplicate elements.
  • Hashset class is non synchronized.

Hashset is implemented by another class called LinkedHashset.

 

LinkedHashset

  • LinkedHashSet maintains the order of element (*Note wherever you found a linked keyword, It will maintain insertion order of element)
  • LinkedHashSet class is non synchronized.

 

SortedSet is a child interface of sets.

  • Sortedset is an interface in collection.
  • All elements inserted into a sorted set must implement the Comparable interface.
  • Sortedset has some useful methods a few mentioned below.

 

  • Comparator()
  • Headset()
  • Tailset()
  • subset()

Sortedset is implemented a class called TreeSet.

 

Treeset

  • Treeset It will sort in ascending order.
  • TreeSet class does not allow duplicate elements.
  • TreeSet class is non synchronized.

 

Priority queue is the class that implements the queue interface.

PriorityQueue

  • It objects are processed based on their priority.
  • It does not allow null.
  • Objects added to PriorityQueue must be comparable.

 

Spread the love