Tuesday, June 29, 2010

Example programs of Collection

ArrayList Example :

import java.util.ArrayList;

public class ArrayListExample {

public static void main(String[] args) {

ArrayList arlist=new ArrayList();

// it is return type of ArrayList

arlist.add("First Element"); // adding element in ArrayList
arlist.add("Second Element");
arlist.add("Third Element");
arlist.add("forth Element");
arlist.add("fifth Element");

// add element with index for fix order

arlist.add(2, "Fixed Order of Element");

// arlist.size() inform number of elements in ArrayList
System.out.println("ArrayList Size :"+arlist.size());

// get elements of ArrayList
for(int i=0;i hs=new HashSet();

// duplicate element is not permitted
hs.add("b");
hs.add("a");
hs.add("c");
hs.add("d");
hs.add("d");

Iterator it=hs.iterator();

while(it.hasNext())
{
String value =(String)it.next();

System.out.println("Value :"+value);
}

//find size of hashSet

System.out.println("Size :"+hs.size());

// Remove element from hashSet :
hs.remove("d");

// To remove all object from hashSet
hs.clear();
}
}


HashTableExample :

import java.util.Hashtable;
import java.util.Enumeration;

public class HashTableExample {

public static void main(String[] args) {

Hashtable hTable=new Hashtable();

//adding or set items in Hashtable by put method key and value pair
hTable.put(new Integer(2), "Two");
hTable.put(new Integer(1), "One");
hTable.put(new Integer(4), "Four");
hTable.put(new Integer(3), "Three");
hTable.put(new Integer(5), "Five");

// Get Hashtable Enumeration to get key and value
Enumeration em=hTable.keys();

while(em.hasMoreElements())
{
//nextElement is used to get key of Hashtable
int key = (Integer)em.nextElement();

//get is used to get value of key in Hashtable
String value=(String)hTable.get(key);

System.out.println("Key :"+key+" value :"+value);
}
}
}




HashMapExample :

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapExample {

public static void main(String[] args) {

HashMap hm=new HashMap();

// adding or set elements in HashMap by put method key and value pair
hm.put(new Integer(2), "Two");
hm.put(new Integer(1), "One");
hm.put(new Integer(3), "Three");
hm.put(new Integer(4), "Four");

// Get hashmap in Set interface to get key and value
Set s=hm.entrySet();

// Move next key and value of HashMap by iterator
Iterator it=s.iterator();

while(it.hasNext())
{
// key=value separator this by Map.Entry to get key and value
Map.Entry m =(Map.Entry)it.next();

// getKey is used to get key of HashMap
int key=(Integer)m.getKey();

// getValue is used to get value of key in HashMap
String value=(String)m.getValue();

System.out.println("Key :"+key);
System.out.println("value :"+value);
}
}
}

TreeSetExample :

import java.util.Iterator;
import java.util.TreeSet;

public class TreeSetExample {

public static void main(String[] args) {

// TreeSet return in ordered elements
TreeSet ts=new TreeSet();

ts.add("b");
ts.add("a");
ts.add("d");
ts.add("c");

// get element in Iterator
Iterator it=ts.iterator();

// get descending order of elements
//Iterator it=ts.descendingIterator();

while(it.hasNext())
{
String value=(String)it.next();

System.out.println("Value :"+value);
}
}
}


No comments:

Post a Comment