Wednesday, August 5, 2009

Java - Collections sample Code

package helloworld;

import java.util.*;

public class TestCollections {
public static void main(String[] args) {
TestCollections arh = new TestCollections();
arh.arrayEx();
arh.hashTableEx();
arh.arrayList();
arh.hashSet();
arh.hashMap();
}

public void arrayEx() {
String[] test = new String[6];
int[] b = { 0, 1, 2, 3, 4 };
String[] testStore = { "Gube", "Kavi", "siva", "arivu", "Bog" };
for (int i : b) {
// System.out.println(i + " " + testStore[i]);
test[i] = testStore[i];
}
}

public void hashTableEx() {
Hashtable testHash = new Hashtable();
testHash.put("Gubs", new String("Siva"));
testHash.put("Arivu", new String("Mom"));
Enumeration t;
t = testHash.keys();
while (t.hasMoreElements()) {
String str = t.nextElement();
System.out.println("Print Hash " + testHash.get(str));
}
}

public void arrayList() {
List l1 = new ArrayList();
l1.add("Gubs");
l1.add("Kavitha");
l1.add("Arivu");

System.out.println("Array List Elements : " + l1);
}

public void hashSet() {
Set h1 = new HashSet();
h1.add("Gubs");
h1.add("Kavi");
System.out.println("Hash Set Elements : " + h1);
}

public void hashMap() {
Map hm = new HashMap();
hm.put("Gubs", "Kavi");
hm.put("Siva", "Arivu");
System.out.println("Hash Map Elements : " + hm);
}
}




LinkedHashMap
TreeMap
HashMap
Set (unique objects no duplicates)
List (list of objects no need to specify size like array its growable)

Collections.binarySearch
Collections.emptyList()
Collections.emptyMap()
Collections.emptySet()

No comments :

// Below script tag for SyntaxHighLighter