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());
}
}
Get All Entries From
Zip File
import
java.io.IOException;
import
java.util.Enumeration;
import
java.util.zip.ZipEntry;
import
java.util.zip.ZipFile;
public class
GetAllEntries
{
public static void main(String args[])
{
try
{
ZipFile zipFile = new
ZipFile("c:/FileIO/WebFiles.zip");Enumeration e = zipFile.entries();
zipFile.close();
}
catch(IOException ioe)
{
System.out.println("Error
opening zip file" + ioe);
}
}
}
Check if a particular
key exists in Java Hash Map
import
java.util.HashMap;
public class
CheckKeyOfHashMapExample
{
public static void
main(String[] args)
{
HashMap hMap = new
HashMap();
hMap.put("1","One");
hMap.put("2","Two");
hMap.put("3","Three");
boolean blnExists = hMap.containsKey("3");
System.out.println("3
exists in HashMap ? : " + blnExists);
}
}
Check if a particular
value exists in Java Hash Map
import
java.util.HashMap;
public class
CheckValueOfHashMapExample
{
public static void
main(String[] args)
{
HashMap hMap = new
HashMap();
hMap.put("1","One");
hMap.put("2","Two");
hMap.put("3","Three");
boolean blnExists =
hMap.containsValue("Two");
System.out.println("Two
exists in HashMap ? : " + blnExists);
}
}
Get Set view of Keys
from Java Hash Map
import
java.util.Iterator;
import
java.util.HashMap;
import
java.util.Set;
public class
GetSetViewOfKeysFromHashMapExample
{
public static void
main(String[] args)
{
HashMap hMap = new
HashMap();
hMap.put("1","One");
hMap.put("2","Two");
hMap.put("3","Three");
Set st =
hMap.keySet();
System.out.println("Set
created from HashMap Keys contains :");
Iterator itr =
st.iterator();
while(itr.hasNext())
System.out.println(itr.next());
st.remove("2");
boolean blnExists =
hMap.containsKey("2");
System.out.println("Does
HashMap contain 2 ? " + blnExists);
}
}
Get Size of Java Hash
Map
import
java.util.HashMap;
public class
GetSizeOfHashMapExample
{
public static void
main(String[] args)
{
HashMap hMap = new
HashMap();
System.out.println("Size
of HashMap : " + hMap.size());
hMap.put("1","One");
hMap.put("2","Two");
hMap.put("3","Three");
System.out.println("Size
of HashMap after addition : " + hMap.size());
Object obj =
hMap.remove("2");
System.out.println("Size
of HashMap after removal : " + hMap.size());
}
}
Iterate through the
values of Java Hash Map
import
java.util.Collection;
import
java.util.HashMap;
import
java.util.Iterator;
public class
IterateValuesOfHashMapExample
{
public static void
main(String[] args)
{
HashMap hMap = new
HashMap();
hMap.put("1","One");
hMap.put("2","Two");
hMap.put("3","Three");
Collection c = hMap.values(); Iterator itr = c.iterator();
while(itr.hasNext())
System.out.println(itr.next());
}
}
Remove all values
from Java Hash Map
import
java.util.HashMap;
public class
EmptyHashMapExample
{
public static void main(String[] args)
{
HashMap hMap = new
HashMap();
hMap.put("1","One");
hMap.put("2","Two");
hMap.put("3","Three");
hMap.clear();
System.out.println("Total
key value pairs in HashMap are : " + hMap.size());
}
}
No comments:
Post a Comment