Computer Science‎ > ‎

Introduction to Java Collections



An Introduction to the JAVA COLLECTIONS FRAMEWORK



It contains classes which are basic implementation of Data Structure algorithms.

A collection is a group of objects, Which can be passed as a single unit to a method

or returned from method. It is efficient and faster to use collection class to transfer

or return  bulk data.


Collection have following features:-

  •   Sequenced or random or index based can contain duplicate values or non duplication unsorted or sorted

  •   Collection framework has following three interfaces, Which are further implemented as per data Structure.

    • List -> sequential, allows duplicate value(Vector, ArrayList,Queue,Dequeue)

    •   set -> sequential, No duplicate value (Treeset)

    •   Map -> index based          (HashMap Properties)  


Vector/Arraylist:-


These are dynamic array i.e. they can grow or shrink their size at runtime.


Vector()

Vector(int size)

Vector(int size,int capacity)

Vector(Collection ob)


Methods:


void addElement(object)

void insertElementAt(int,object)

void remove(object)

void removeElement(index)

void removeAll()

int size()

int capacity()

void trimToSize()

Object firstElement()

Object lastElement()

Object elementAt(index)

boolean contains(object)

int indexOf(object,index)

Object[]  toArray()


Arraylist is Generic while Vector is not.


Arraylist:


It is a collection class, Subclass of list, implementation of Dynamic array, it is Generic:


ArrayList() -> can contain any type of object

ArrayList<Type>() -> can contain only specific type of object

ArrayList(Collection ob)


Methods


boolean isempty()

int size()

int capacity()

void trimToSize()

void add(Object)

void set(Index,object)

object get(index)

boolean contains(object)

object[] toArray()


Collection classes can contain only explicit(reference) type data.Therefore upto JDK 1.4 wrapper Classes being used to convert value type(primitive) data into reference type.

From JDK 1.5 onwards autoboxing is applied by JVM.


Example:- Write a Page to implement vector, to hold objects, retrieve, display, manipulate vector elements.


import java.util.*;

class MyVector

{

public static void main(String args[])

{

 Vector v=new Vector();

 v.addElement(“anil”); // To add an Element

 v.addElement(“sunil”);// So here same goes

  v.addElement(“Kapil”);

 abc ab = new abc(5,6) //suppose any class

  v.addElement(ab);

 xyz xy=new xyz(); //suppose any class

 v.addElement(xy);

System.out.println(“size:”+v.size());

ab= new abc(2,3);

v.addElement(ab);

v.addElement(“Sunita”);

System.out.println(“size:”+v.size());

System.out.println(“Capacity:”+v.capacity());

if(O instanceOf String)

{

 String x=(String)O;

 System.out.println(x);

}


-> How to find/check which class does this object belongs.


Object instanceOf classtype=true

else

   if(O instanceOf abc)

   {

      abc ab1=(abc)O;

      ab1.add();

    }

  else

   {

     xyz xy=(xyz)O;

     xy.show();

   }

* for(int i=0;i<v.size();i++)

{

 Object O=v.elementAt(i);  // same procedure will be allowed u now have                             //better known

 --

 --

}*  

v.removeElementAt(0);// To remove an Element

v.removeElement(2);

System.out.println(“size:”+v.size());

Object O=v.elementAt(3);

System.out.println(O.toString());    

}

}


Class abc

{

 int a,b;

 abc(int x,int y)

 {

   a=x;

   b=y;

  }

void add()

{

 System.out.println(a+b);

}

Class xyz

{

 void show()

 {

   System.out.println(“This is String”);

 }

}

}



Treeset:-


It is a collection class, implementation of set interface, faster,stores data in  sorted order, used to store large Volume of data, where searching is fast & efficient.

TreeSet()

TreeSet<Type>()

TreeSet(Collection)




Methods


int size()

boolean isEmpty()

Object first() -> minimum value

Object last() -> maximum value

Object add(object)

Object remove(object)

SortedSet tailSet(Object)->tailset from given value

SortedSet headSet(Object)->headset from given value

SortedSet subSet(object,object)


import java.util.*;

class Mytree

{

public static void main(String[] args)

{

  TreeSet<String>ts=new TreeSet<String>();

   ts.add(“z”);// The way to add in tree

   ts.add(“p”);

   ts.add(“A”);

   ts.add(“C”);

   ts.add(“D”);

   System.out.println(ts);//-> {A,c,d,p,z}

   System.out.println(ts.first());//->A

   System.out.println(ts.last());//->z

   SortedSet ss=ts.tailSet(“D”);

   System.out.println(ss);

   ss=ts.headSet(“D”);//->AC

   System.out.println(ss);

 

}

}


HashMap:-


It is an implementation of map interface stores data in key/value pair key must be unique.


HashMap()

HashMap<Type1,Type2>()






Methods


void put(key,value)

Object get(Key)

void clear()

void remove(Key)

int size()

boolean isEmpty()

boolean containsKey(Key)

boolean containsValue(Value)

Array getValues()



import java.util.*;

public class HashMapTest {

  public static void main(String args[])

  {

         HashMap h=new HashMap();// make a student class

         Student s1=new Student(“aman”,1);

         Student s2=new Student(“Kaushal”,2);

         Student s3=new Student(“Anil”,3);

         //Adding elements (Student objects) where roll nos

        h.put(“one”,”s1”);

        h.put(“two”,s2);

        h.put(“six”,s3);

        if(b==true){

        System.out.println(“hashmap is empty”);

       } else{

                      int size=h.size();

                      System.out.println(“Hashmap size:”+size);

    }

         Student s=(Student)h.get(“two”);//calling students class print method s.print()

}

}


public class Student {

private String name;

private int rollNo;

private static int countStudents = 0;

// Standard Setters

public void setName (String name) {

this.name = name;

}

// Note the masking of class level variable rollNo

public void setRollNo (int rollNo) {

if (rollNo > 0) {

this.rollNo = rollNo;

}else {

this.rollNo = 100;

}

}

// Standard Getters

public String getName ( ) {

return name;

}

public int getRollNo ( ) {

return rollNo;

}

// gettter of static countStudents variable

public static int getCountStudents(){

return countStudents;

}

// Default Constructor

public Student() {

name = “not set”;

rollNo = 100;

countStudents += 1;

}

// parameterized Constructor for a new student

public Student(String name, int rollNo) {

setName(name); //call to setter of name

setRollNo(rollNo); //call to setter of rollNo

countStudents+=1;

}


// Copy Constructor for a new student

public Student(Student s) {

name = s.name;

rollNo = s.rollNo;

countStudents += 1;

}

// method used to display method on console


public void print () {

System.out.print("Student name: " +name);

System.out.println(", roll no: " +rollNo);

}

// overriding toString method of java.lang.Object class

public String toString(){

return “name: ” + name + “ RollNo: ” + rollNo;

}

// overriding finalize method of Object class

public void finalize(){

countStudents -= 1;

}

}






Queue:-


A queue supports the insert and remove operations using a FIFO discipline. By convention, we name the queue insert operation enqueue and the remove operation dequeue.



Queue iterator in Java

Queue.java


it  illustrates how to implement an Iterator when the items are stored in a linked list.



public Iterator iterator() { return new QueueIterator();  }

private class QueueIterator implements Iterator<Item> {
   Node current = first;
   
   public boolean hasNext()  { return current != null; }

   public Item next() {
       Item item = current.item;
       current = current.next;
       return item;
   }
}


It relies on a private nested subclass QueueIterator that implements the Iterator interface. The method iterator() creates an instance of type QueueIterator and returns it as an Iterator. This enforces the iteration abstraction since the client will only the items through the hasNext() and next() methods. The client has no access to the internals of the Queue or even the QueueIterator. It is the client's responsibility to only add elements to the list when no iterator is in action.


To take advantage of Java's enhanced foreach syntax, the data type must implement Java's Iterable interface.


public interface Iterable<Item> {
   Iterator<Item> iterator();
}


That is, the data type must implement a method named iterator() that returns an Iterator to the underlying collection. Since our Queue ADT now includes such a method, we simply need to declare it as implementing the Iterable interface and we are ready to use the foreach notation.


public class Queue<Item> implements Iterable<Item>


Stack:-


The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.


Stack implementation with an array. Resizes by doubling and halving.


import java.util.Iterator;
import java.util.NoSuchElementException;

public class DoublingStack<Item> implements Iterable<Item> {
   private Item[] a;         
// array of items
   private int N = 0;        // number of elements on stack

   
// create an empty stack
   public DoublingStack() {
       a = (Item[]) new Object[2];
   }

   public boolean isEmpty() { return N == 0; }
   public int size()        { return N;      }



   // resize the underlying array holding the elements
   private void resize(int capacity) {
       assert(capacity >= N);
       Item[] temp = (Item[]) new Object[capacity];
       for (int i = 0; i < N; i++)
           temp[i] = a[i];
       a = temp;
   }

  
 // push a new item onto the stack
   public void push(Item item) {
       if (N == a.length) resize(2*a.length);   
 // double size of array if necessary
       a[N++] = item;                            // add item
   }

   
// delete and return the item most recently added
   public Item pop() {
       if (isEmpty()) { throw new RuntimeException("Stack underflow error"); }
       Item item = a[N-1];
       a[N-1] = null;                             
 // to avoid loitering
       N--;
       
// shrink size of array if necessary
       if (N > 0 && N == a.length/4) resize(a.length/2);
       return item;
   }


 
  // string representation (inefficient because of string concatenation)
   public String toString() {
       String s = "[ ";
       for (int i = 0; i < N; i++)
           s += a[i] + " ";
       s += "]";
       return s;
   }

   public Iterator<Item> iterator()  { return new ReverseArrayIterator();  }


   // an iterator, doesn't implement remove() since it's optional
   
private class ReverseArrayIterator implements Iterator<Item> {
       private int i = N;
       public boolean hasNext()  { return i > 0;                               }
       public void remove()      { throw new UnsupportedOperationException();  }

       public Item next() {
           if (!hasNext()) throw new NoSuchElementException();
           return a[--i];
       }
   }



 
   public static void main(String[] args) {
       DoublingStack<String> stack = new DoublingStack<String>();
       stack.push("Hello");
       stack.push("World");
       stack.push("how");
       stack.push("are");
       stack.push("you");

       for (String s : stack)
           System.out.println(s);

       System.out.println();

       while (!stack.isEmpty())
           System.out.println(stack.pop());
   }

}



Generics:


Generics are one of the most controversial Java language features. Generics allows a type or method to operate on objects of various types while providing compile-time type safety, making Java a fully statically typed language.


Generics add stability to your code by making more of your bugs detectable at  Compile time.


  • All generic method declarations have a type parameter section delimited by angle brackets (< and >) that precedes the method's return type ( < E > in the next example).

  • Each type parameter section contains one or more type parameters separated by commas. A type parameter, also known as a type variable, is an identifier that specifies a generic type name.

  • The type parameters can be used to declare the return type and act as placeholders for the types of the arguments passed to the generic method, which are known as actual type arguments.

  • A generic method's body is declared like that of any other method. Note that type parameters can represent only reference types not primitive types (like int, double and char).


Following example illustrate how we can print array of different type using a single Generic method:


public class GenericMethodTest
{
  // generic method printArray                         
  public static < E > void printArray( E[] inputArray )
  {
     // Display array elements              
        for ( E element : inputArray ){        
           System.out.printf( "%s ", element );
        }
        System.out.println();
   }

   public static void main( String args[] )
   {
       // Create arrays of Integer, Double and Character
       Integer[] intArray = { 1, 2, 3, 4, 5 };
       Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
       Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };

       System.out.println( "Array integerArray contains:" );
       printArray( intArray  );
// pass an Integer array

       System.out.println( "\nArray doubleArray contains:" );
       printArray( doubleArray );
// pass a Double array

       System.out.println( "\nArray characterArray contains:" );
       printArray( charArray );
// pass a Character array
   }
}