import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
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
// 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
//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
No comments:
Post a Comment