☰ Topics

Collections Framework

The java collections framework standardizes the way in which groups of objects are handled by our programs. The collection framework was designed to meet several goals.

  • The framework had to be high-performance. The implementations for the fundamental collections are highly efficient.
  • The framework had to allow different types of collections to work in a similar manner and with a high degree of interoperability.
  • Extending/or adapting a collection had to be easy.
Note:- None of the collection classes are synchronized, but all the legacy classes are synchronized.

The legacy classes defined by java.util are Dictionary, Hashtable, Properties, Stack, Vector

Legacy interface - Enumeration

The Enumeration Interface:

The Enumeration interface defines the methods by which we can enumerate i.e. obtain one at a time, the elements in a collection of objects.

hasMoreElement()- It must return true while there are still more elements to extract and false when all the elements have been enumerated.

nextElement- It returnd the next object in the enumeration as a generic object reference i.e. each call to nextElement() obtains the next object in the enumeration.

Vector

Vector implements dynammic array. It is similar to arraylist but with two differences
  • Vector is synchronized.
  • Vector may contain legacy method that are not the part of collection framework.
Vector proves to be very useful if we don't know the size of array in advance or we just need that can be changed over the lifetime of a program.
Note:- Enumeration interface or foreach loop is used to traverse the Vector.

Syntax:-

Vector <object-name>= new Vector(<size>,<increment-value>);
                        

Here, size is the initial size defined to the Vector, increment-value is the value by which the Vector gets incremented whenever the size of the Vector is full and we add more element to the Vector.

Example:
import java.util.*;
class exvector
{
	public void vec()
	{
		Vector v=new Vector(3,2);    //Here, 3 is the size and 2 is the increment value
		v.addElement(new Integer(5));  //To add 1st element
		v.addElement(new Integer(6));  //To add 2nd element
		System.out.println("Size="+v.size());     //To print size
		System.out.println("Capacity="+v.capacity());    //To print capacity
		v.addElement(new Double(5.5));   //To add 3rd element
		v.addElement(new Double(7.6));   //To add 4th element
		v.addElement(new Double(9.3));   //To add 5th element
		System.out.println("Current Size="+v.size());    //To print increased size
		System.out.println("Capacity="+v.capacity());    //To print increased capacity
		Enumeration e=v.elements();   //To enumerate the element of the vector, here foreach loop can also be used
		while(e.hasMoreElements())     //To check next element and move to next if it exists, otherwise return false
			System.out.println(e.nextElement());    //To print the value where the cursor reside i.e. the current pointer
	}
	public static void main(String[] arg)
	{
		exvector ob=new exvector();
		ob.vec();
		
	}
}
                            

OUTPUT:

E:\java\new_java>java exvector
Size=2
Capacity=3
Current Size=5
Capacity=5
5
6
5.5
7.6
9.3
                        

Stack

Stack is a subclass of Vector that implements a standard last-in, first-out stack (LIFO). Stack only defines the default constructor, which creates an empty stack.

push() is used to put an object on the top of the stack and pop() is used to remove and return the top element. An EmptyStackException is thrown if we call pop() when the invoking Stack is empty.
peek() - To return the top object.
empty() - Returns true if nothing is on the Stack.
search() - It determines whether an object exists on the Stack, and returns the number of pops that are required to bring it to the top of the Stack.

Example:
import java.util.*;
class exstack
{
	public void st()
	{
		Stack s=new Stack();
		s.push(5);
		System.out.println(s);
		s.push(new Integer(6));
		System.out.println(s);
		s.push(new Double(6.7));
		System.out.println(s);
		s.pop();
		System.out.println(s);
		s.push(new Integer(8));
		System.out.println(s);
	}
	public static void main(String[] arg)
	{
		exstack ob=new exstack();
		ob.st();
	}
}
                            

OUTPUT:

E:\java\new_java>java exstack
[5]
[5, 6]
[5, 6, 6.7]
[5, 6]
[5, 6, 8]
                        


Hashtable

Hashtable stores key/value pairs in a Hashtable. When using Hashtable, we specify an object that is used as a key, and the value that we want linked to that key. The key is then hashed, and the resulting Hashcode is used as the index at which the value is stored within the table.

A Hashtable is an array of list. Each list is known as bucket. The position of bucket is identified by calling hashcode() method. A Hashtable contains value based on key. It contains only unique elements. It is synchronized. It may not have any null key or value.

Example:
import java.util.*;
class exhashtable
{
	public void ht()
	{
		Hashtable<Integer,String> h=new Hashtable<Integer,String>();
		h.put(101,"Rahul");
		h.put(102,"Shyam");
		for(Map.Entry m:h.entrySet())     //To traverse the Hashtable, Map is a class and Entry is its property
			System.out.println(m.getKey()+"="+m.getValue());
	}
	public static void main(String[] arg)
	{
		exhashtable ob=new exhashtable();
		ob.ht();
	}
}
                            

OUTPUT:

E:\java\new_java>java exhashtable
102=Shyam
101=Rahul