How Various Java Collection Classes Work

Introduction

In this article we discuss how various Java Collection classes work.

HashSet class

HashSet extends AbstractSet and implements the Set interface. It creates a collection that uses a hash table (a hash table stores information by using a mechanism called hashing) for storage. It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

This class supports four constructors that are the following:

  • HashSet( )
  • HashSet(Collection cln)
  • HashSet(int capacity, float fillRatio)
  • HashSet(int capacity)

Example

import java.util.*;

class HashSetEx

  {

    public static void main(String args[])

      {

        HashSet arylst=new HashSet();

        arylst.add("Paul");

        arylst.add("John");

        arylst.add("Paul");

        arylst.add("San");

        Iterator itrtr=arylst.iterator();

        while(itrtr.hasNext())

          {

            System.out.println(itrtr.next());

          }

      }

  }

Output

Fig-1.jpg

LinkedHashSet class

  • It contains only unique elements like HashSet.
  • It extends HashSet class and implements Set interface.
  • Maintains insertion order.

Example

import java.util.*;

class LinkedHashSetEx

  {

    public static void main(String args[])

      {

        LinkedHashSet arylst=new LinkedHashSet();

        arylst.add("Paul");

        arylst.add("John");

        arylst.add("Paul");

        arylst.add("San");

        Iterator itrtr=arylst.iterator();

        while(itrtr.hasNext())

          {

            System.out.println(itrtr.next());

          }

      }

  }

Output

Fig-2.jpg

TreeSet class

  • This class implements NavigableSet interface that extends the SortedSet interface.
  • It contains only unique elements like HashSet.
  • Maintains ascending order.

Example

import java.util.*;

class TreeSetEx

  {

    public static void main(String args[])

      {

        TreeSet arylst=new TreeSet();

        arylst.add("Paul");

        arylst.add("John");

        arylst.add("Paul");

        arylst.add("San");

        Iterator itrtr=arylst.iterator();

        while(itrtr.hasNext())

          {

            System.out.println(itrtr.next());

          }

      }

  }

Output

fig-3.jpg

Queue Interface

It is basically used to order the elements in the FIFO (first in first out) manner.

Public Methods

Some public methods used in Queue Interface are:

  1. remove()
  2. poll()
  3. elements()
  4. peek()
  5. boolean add (object)
  6. boolean offer (object)

Priority Queue classes

The class provides the ability to use a queue, but it does not order the elements in a FIFO manner.

Example

import java.util.*;

class PriorityQueueEx

  {

    public static void main(String args[])

      {

        PriorityQueue que=new PriorityQueue();

        que.add("Paul");

        que.add("John");

        que.add("Dinesh");

        que.add("San");

        que.add("Rahul");

        System.out.println("head:" +que.element());

        System.out.println("head:" +que.peek());

        System.out.println("iterating the que elements:");

        Iterator itrtr=que.iterator();

        while(itrtr.hasNext())

          {

            System.out.println(itrtr.next());

          }

        que.remove();

        que.poll();

        System.out.println("after removing two elements of queue:");

        Iterator itrtr2=que.iterator();

        while(itrtr2.hasNext())

          {

            System.out.println(itrtr2.next());

          }

      }

  }

Output

fig-4.jpg

HashMap class

  • may have one null key and multiple null values.
  • contains values based on the key.
  • implements the Map interface and extends the AbstractMap class.
  • contains only unique elements.
  • maintains no order.

Example

import java.util.*;

class HashMapEx

  {

    public static void main(String args[])

      {

        HashMap hsmp=new HashMap();

        hsmp.put(10, "Paul");

        hsmp.put(11, "John");

        hsmp.put(12, "San");

        Set s=hsmp.entrySet();

        Iterator itrtr=s.iterator();

        while(itrtr.hasNext())

          {

            Map.Entry me=(Map.Entry)itrtr.next();

            System.out.println(me.getKey()+" " +me.getValue());

          }

      }

  }

Output

fig-5.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all