Get Some Free Products at your Door Step. Just feed ur Address in dis Link...!!

Thursday, October 27, 2011

java collections program

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.TreeSet;
public class collection
{
public static void main(String ar[])
{
collection c = new collection();
//c.arrayList();
//c.linkList();
//c.treeSet();
c.hashMap();
}
public void arrayList()
{
//Result will be Ordered as entered
ArrayList al = new ArrayList();
al.add("prem");
al.add("neeraj");
al.add("pranav");
al.add("mohit");
al.add("divya");

System.out.println("Size of Array" + al.size());
al.remove(1); //Remove element at 1 position in array
al.remove("mohit"); //Remove string element in array
System.out.println(al.get(2)); //Show String at position 2
al.set(2, "chauhan"); //set string chauhan at position 2
System.out.println(al.isEmpty());//check array for empty or not
System.out.println(al.indexOf("chauhan")); //return index of chauhan object
System.out.println(al); //Show all array elements
al.clear(); //clear the arraylist and make it blank
}
public void linkList()
{
//Result will be Ordered as entered
LinkedList ll = new LinkedList();
ll.add("prem");
ll.add("neeraj");
ll.add("pranav");
ll.add("mohit");
ll.add("divya");

System.out.println("Size of Array:" + ll.size());
ll.remove(1); //Remove element at 1 position in LinkedList
ll.remove("mohit"); //Remove string element in LinkedList
System.out.println(ll.get(2)); //Show String at position 2
ll.set(2, "chauhan"); //set string chauhan at position 2
System.out.println(ll.isEmpty());//check LinkedList for empty or not
System.out.println(ll.indexOf("chauhan")); //return index of chauhan object
ll.addFirst("First"); //add String First to 0 position of LinkedList
ll.addLast("Last"); //add String Last to last position of LinkedList
System.out.println(ll);
ll.removeFirst();
ll.removeLast();
System.out.println(ll); //Show lll array elements
ll.clear(); //clear the arraylist and make it blank
}
public void treeSet()
{
//Result will not be in Order as Entered
TreeSet ts = new TreeSet();
ts.add("prem");
ts.add("neeraj");
ts.add("pranav");
ts.add("mohit");
ts.add("divya");

System.out.println("Size of Array:" + ts.size());
//ts.remove(1);
ts.remove("mohit");
System.out.println(ts.isEmpty());
System.out.println(ts);
System.out.println(ts);
ts.clear();
}
public void hashSet()
{
//Result will be not in Order as entered
HashSet hs = new HashSet();
hs.add("prem");
hs.add("neeraj");
hs.add("pranav");
hs.add("mohit");
hs.add("divya");

System.out.println("Size of Array:" + hs.size());
// hs.remove(1);
hs.remove("mohit");
System.out.println(hs.isEmpty());
System.out.println(hs);
hs.clear();
}
public void hashMap()
{
HashMap hm = new HashMap();
//HashMap takes key and value pair (here String and Integer (I Chose)
hm.put("Prem", 35);
hm.put("Vaibhav", 34);
hm.put("Dolly", 36);
//Send Key to get function and get Value
System.out.println(hm.get("Prem")); //getting value by sending key
hm.remove("Vaibhav");
System.out.println(hm.keySet());//Show all keys in HashMap
System.out.println(hm.values());//Show all Values in HashMap
System.out.println(hm.entrySet());//Show all keys with corresponding values
hm.clear();
}
}

No comments:

Post a Comment