import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.LinkedList;
public class CollectionSample {
public void arrayListSample()
{
System.out.println("========ARRAY LIST SAMPLE============");
ArrayList<String> ls =new ArrayList<String>();
System.out.println(ls.size() + "Intial size of the array");
ls.add("Bala");
ls.add("murali");
ls.add("krishna");
ls.add("mnmn");
ls.add("k");
ls.add("krishna"); // duplicate value
System.out.println(ls.size() + "Final size of the array");
ls.remove("mnmn");
ls.remove(1);
System.out.println(ls);
System.out.println("========ARRAY LIST SAMPLE End ============");
}
public void convertArrayList2Array()
{
System.out.println("========convertArrayList2Array ============");
ArrayList<Integer> ls = new ArrayList<Integer>();
ls.add(8);
ls.add(2);
ls.add(4);
ls.add(4);
ls.add(0,6);
ls.add(0,2);
System.out.println(ls.size());
System.out.println(ls);
Integer arr[] = new Integer[ls.size()];
arr = ls.toArray(arr);
int sum =0;
for(int i : arr)
{
sum =sum+ i;
}
System.out.println("Array SUM ="+sum);
System.out.println("========convertArrayList2Array End ============");
}
public void linkedListExample()
{
System.out.println("========linkedListExample ============");
LinkedList< String> ll = new LinkedList<String>();
System.out.println("==================="+ll.size());
ll.add("Bala");
ll.add("murali");
ll.add("krishna");
ll.add("K");
ll.add(0,"Mrs");
ll.add("krishna"); // duplicate value
System.out.println("==================="+ll.size());
System.out.println("Original contents of ll: " + ll);
ll.remove(0);
System.out.println("Original contents of ll: " + ll);
System.out.println("Remove first and last elements.");
ll.removeFirst();
System.out.println("Get and Set values");
String val = ll.get(2);
ll.set(2, val + " Changed");
System.out.println("Original contents of ll: " + ll);
}
public void hashSetExample()
{
System.out.println("========hashSetExample ============");
HashSet< String> hs = new HashSet<String>();
System.out.println("==================="+hs.size());
hs.add("Mrs");
hs.add("h");
hs.add("a");
hs.add("s");
hs.add("h");// duplicate value
hs.add("set"); // not method with index for add
System.out.println("==================="+hs.size());
System.out.println("==================="+hs);
hs.remove("set");
System.out.println("==================="+hs);
System.out.println("======hs.hashCode()============="+hs.hashCode());
}
public void linkedhashSetExample() //No duplicate and preserve order in which the values are entered
{
System.out.println("========linkedhashSetExample ============");
LinkedHashSet<String> lhs = new LinkedHashSet<String>();
System.out.println("==================="+lhs.size());
lhs.add("Mrs");
lhs.add("h");
lhs.add("a");
lhs.add("s");
lhs.add("h");// duplicate value
lhs.add("set"); // not method with index for add
System.out.println("==================="+lhs.size());
System.out.println("==================="+lhs);
lhs.remove("set");
System.out.println("==================="+lhs);
System.out.println("======hs.hashCode()============="+lhs.hashCode());
}
public static void main(String[] args)
{
CollectionSample obj = new CollectionSample();
obj.linkedhashSetExample();
}
}
No comments:
Post a Comment