Beginners Guide to Collection API

Posted on 13. Jul, 2010 by suz_java in General, Programming, Tutorial

What is Collection API?


Collection API
All classes and interface that form the collection framework are called data structure. Collections help programmers by giving groups of objects in JAVA. A collection can be defined as a set object. Collections in java are nothing but implementation of different data structures. Data structures can be visualized as container that group multiple elements and represent them as a single entity. The collection framework enables us to store, access, retrieve, and manipulate the data elements that they contain. Different classes and interface that are available in the collection framework are found in java.util package.
Collection interface is generally used to route collection and manipulate them it can be used to transmit a collection of object between methods.

Difference between array and vector


One Dimentional Array
When we work on java, it’s common to us to handle the Collection interface, but many times we forget the basic deference of some common method. I want to introduce some of the most common deference of array and vector.

  1. The size of array is static, while a vector is a dynamically resizable.
  2. Vector can store only object reference not any primitive data type. Unlike arrays, vector can contain any combination of object data type.
  3. Array is accessed directly through its integer index. The method elementAt(index)  of vector is used to retrieve a given reference at specified index.
/* example of vector class */
import java.util.*;
class VectorClass
{
	public static void main(String arg[])
	{
		Vector V=new Vector();
		V.addElement(new Integer(10));
		V.addElement(new Integer(20));
		V.addElement(new Integer(30));
		System.out.println(V);
		V.addElement(new Double(55.45);
		System.out.println(V);
		V.removeElement(new Integer(30));
		System.out.println(V);
			if(V.contains(new Integer(20)))
		System.out.println("20 is present " +V.indexOf(new Integer(20)));
			else
		System.out.println("20 is not present");
	}
}

Introduction to ArrayList


Array List

It is similar to vector, a dynamic resizable array. Both classes inheritance from AbstractList, and implement the interface list.
Array List is compared with vector both are dynamically resizable, both have some common method but it does not have legacy method that Vector has, like elementAt( ), addElement( ).

// example of ArrayList
import java.util.*;
class Alist
{
	public static void main(String arg[])
	{
		ArrayList a=new ArrayList(2);
		a.add(new Integer(10));
		a.add(new Integer(20));
		a.add(new Float(30.4));
		a.add(new Integer(45));
		System.out.println("contents of a:"+a);
		a.add(2,new Integer(5)); // it is insert into the 2nd position
		System.out.println("contents of a:"+a);
		a.remove(2);
		System.out.println("contents of a:"+a);
	}
}

Guide to LinkedList


Link List

It adds new functionality that vector did not have previously. It has method like addList( ), addlast( ) to add elements to start or end of the list. It has method like getList( ), getLast( ), removeFirst( ), removeLast( ). So, using the Link list we can handle more perfectly the data elements.

// Demonstrate LinkedList.
import java.util.*;
class LinkedListDemo
{
	public static void main(String args[])
	{
		// create a linked list
		LinkedList ll = new LinkedList();
		// add elements to the linked list
		ll.add("F");
		ll.add("B");
		ll.add("D");
		ll.add("E");
		ll.add("C");
		ll.addLast("Z");
		ll.addFirst("A");
		ll.add(1, "A2");
		System.out.println("Original contents of ll: " + ll);
		// remove elements from the linked list
		ll.remove("F");
		ll.remove(2);
		System.out.println("Contents of ll after deletion: " + ll);
		// remove first and last elements
		ll.removeFirst();
		ll.removeLast();
		System.out.println("ll after deleting first and last: " + ll);
		// get and set a value
		Object val = ll.get(2);
		ll.set(2, (String) val + " Changed");
		System.out.println("ll after change: " + ll);
	}
}

Hash Table


Hash Table
It is a data structure that maps keys to values.It is used where sequentially access is not necessary.

A hash table is not as array or vector.It allows developer a more specific means of accessing data other than index value. Keys and value can be made up or any non-null object. This class extends from the class dictionary and implements the interfaces. The creation of use of Hash Table is similar to Vector, instantiation of its result in an empty Hash Table, where elements are add using put with key and value arguments of type object. To retrieve object from a Hash Table, the object used as keys must implements method hashCode( ) and equals( ).

//Testting Hashtable behavior when objects representating the same
//entity are used as hastable keys
class Program
{
	static void Main(string[] args)
		{
			Hashtable col = new Hashtable();
			col["key1"] = "Value1";
			col["key2"] = "Value2";
			DisplayResults(col);
			col["key2"] = "Value2Updated";
			DisplayResults(col);
			System.Console.Read();
		}
	static void DisplayResults (Hashtable col)
		{
			Console.WriteLine();
			Console.WriteLine("Total Items: {0}",col.Count);
			foreach (DictionaryEntry entry in col)
				{
					Console.WriteLine(
						"Key[{0}], Hash[{1}], Value[{2}]",
						entry.Key,
						entry.Key.GetHashCode().ToString(),
						entry.Value);
				}
		}
}

Conclusion


So, I think from my few points of collection API  we can easily understand of it’s different method and their advantages  . You can understand  more clearly if you ran the given examples.

One Response to “Beginners Guide to Collection API”

  1. Alice

    28. Jul, 2010

    This is a nice tutorial . You explain all the difficult portion with the easiest way, thank you .

Leave a Reply

:D :) :o :metamags: :right: :code: :( :lol: ;) :arrow: :idea: :?: :!: :p