Simple Java Hash Set
import
java.util.HashSet;
public class
SimpleHashSetExample
{
public static void
main(String[] args)
{
HashSet hSet = new
HashSet();
hSet.add(new
Integer("1"));
hSet.add(new
Integer("2"));
hSet.add(new
Integer("3"));
System.out.println("HashSet
contains.." + hSet);
}
}
Check if a particular
key exists in Java Hash table
import
java.util.Hashtable;
public class
CheckKeyOfHashtableExample
{
public static void
main(String[] args)
{
Hashtable ht = new
Hashtable();
ht.put("1","One");
ht.put("2","Two");
ht.put("3","Three");
boolean blnExists =
ht.containsKey("2");
System.out.println("2
exists in Hashtable ? : " + blnExists);
}
}
Check if a particular
value exists in Java Hash table
import
java.util.Hashtable;
public class
CheckValueOfHashtableExample
{
public static void
main(String[] args)
{
Hashtable ht = new
Hashtable();
ht.put("1","One");
ht.put("2","Two");
ht.put("3","Three");
boolean blnExists =
ht.contains("Two");
System.out.println("Two
exists in Hashtable ? : " + blnExists);
}
}
Create Java Hash table
from Hash Map
import
java.util.Enumeration;
import
java.util.Hashtable;
import
java.util.HashMap;
public class
CreateHashtableFromHashMap
{
public static void
main(String[] args)
{
HashMap hMap = new
HashMap();
hMap.put("1","One");
hMap.put("2","Two");
hMap.put("3","Three");
Hashtable ht = new
Hashtable();
ht.put("1","This
value would be Replaced !!");
ht.put("4","Four");
System.out.println("Hashtable
contents before copy");
Enumeration e =
ht.elements();
while(e.hasMoreElements())
System.out.println(e.nextElement());
ht.putAll(hMap);
System.out.println("Hashtable
contents after copy");
e = ht.elements();
while(e.hasMoreElements())
System.out.println(e.nextElement());
}
}
Get Collection of
Values from Java Hash table
import
java.util.Enumeration;
import
java.util.Iterator;
import
java.util.Hashtable;
import
java.util.Collection;
public class
GetCollectionOfValuesFromHashtableExample
{
public static void
main(String[] args)
{
Hashtable ht = new
Hashtable();
ht.put("1","One");
ht.put("2","Two");
ht.put("3","Three");
Collection c =
ht.values();
System.out.println("Values
of Collection created from Hashtable are :");
Iterator itr =
c.iterator();
while(itr.hasNext())
System.out.println(itr.next());
c.remove("One");
System.out.println("Hashtable
values after removal from Collection are :");
Enumeration e =
ht.elements();
while(e.hasMoreElements())
System.out.println(e.nextElement());
}
}
Get Set view of Keys
from Java Hash table
import
java.util.Enumeration;
import
java.util.Iterator;
import
java.util.Hashtable;
import java.util.Set;
public class
GetSetViewOfKeysFromHashtableExample
{
public static void
main(String[] args)
{
Hashtable ht = new
Hashtable();
ht.put("1","One");
ht.put("2","Two");
ht.put("3","Three");
Set st = ht.keySet();
System.out.println("Set
created from Hashtable Keys contains :");
Iterator itr =
st.iterator();
while(itr.hasNext())
System.out.println(itr.next());
st.remove("2");
System.out.println("Hashtable
keys after removal from Set are :");
Enumeration e =
ht.keys();
while(e.hasMoreElements())
System.out.println(e.nextElement());
}
}
Get Size of Java Hash
table
import
java.util.Hashtable;
public class
GetSizeOfHashtableExample
{
public static void main(String[] args)
{
Hashtable ht = new
Hashtable();
System.out.println("Size
of Hashtable : " + ht.size());
ht.put("1","One");
ht.put("2","Two");
ht.put("3","Three");
System.out.println("Size
of Hashtable after addition : " + ht.size());
Object obj =
ht.remove("2");
System.out.println("Size
of Hashtable after removal : " + ht.size());
}
}
Iterate through keys
of Java Hash table
import
java.util.Hashtable;
import
java.util.Enumeration;
public class
IterateThroughKeysOfHashtableExample
{
public static void
main(String[] args)
{
Hashtable ht = new
Hashtable();
ht.put("1","One");
ht.put("2","Two");
ht.put("3","Three");
Enumeration e =
ht.keys();
while(e.hasMoreElements())
System.out.println(e.nextElement());
}
}
Iterate through
values of Java Hash table
import
java.util.Hashtable;
import
java.util.Enumeration;
public class
IterateValuesOfHashtableExample
{
public static void
main(String[] args)
{
Hashtable ht = new
Hashtable();
ht.put("1","One");
ht.put("2","Two");
ht.put("3","Three");
Enumeration e = ht.elements();
while(e.hasMoreElements())
System.out.println(e.nextElement());
}
}
Remove all values
from Java Hash table
import
java.util.Hashtable;
public class
EmptyHashtableExample
{
public static void
main(String[] args)
{
Hashtable ht = new Hashtable();
ht.put("1","One");
ht.put("2","Two");
ht.put("3","Three");
ht.clear();
System.out.println("Total
key value pairs in Hashtable are : " + ht.size());
}
}
Remove value from
Java Hash table
import
java.util.Enumeration;
import
java.util.Hashtable;
public class
RemoveValueFromHashtableExample
{
public static void
main(String[] args)
{
Hashtable ht = new
Hashtable();
ht.put("1","One");
ht.put("2","Two");
ht.put("3","Three");
Object obj =
ht.remove("2");
System.out.println(obj
+ " Removed from Hashtable");
Enumeration e =
ht.elements();
while(e.hasMoreElements())
System.out.println(e.nextElement());
}
}
Simple Java Hash
table
import
java.util.Hashtable;
public class
SimpleHashtableExample
{
public static void
main(String[] args)
{
Hashtable ht = new
Hashtable();
ht.put("One",
new Integer(1));
ht.put("Two",
new Integer(2));
Object obj =
ht.get("One");
System.out.println(obj);
}
}
Iterate through a
Collection using Java Iterator
import
java.util.Iterator;
import
java.util.ArrayList;
public class
JavaIteratorExample
{
public static void
main(String[] args)
{
ArrayList aList = new
ArrayList();
aList.add("1");
aList.add("2");
aList.add("3");
aList.add("4");
aList.add("5");
Iterator itr =
aList.iterator();
while(itr.hasNext())
System.out.println(itr.next());
}
}
Iterate through
elements Java Vector using Iterator
import
java.util.Vector;
import
java.util.Iterator;
public class
IterateThroughVectorUsingIteratorExample
{
public static void
main(String[] args)
{
Vector v = new
Vector();
v.add("1");
v.add("2");
v.add("3");
v.add("4");
v.add("5");
Iterator itr =
v.iterator();
System.out.println("Iterating
through Vector elements...");
while(itr.hasNext())
System.out.println(itr.next());
}
}
No comments:
Post a Comment