Remove value from
Java Hash Map
import
java.util.HashMap;
public class
RemoveValueFromHashMapExample
{
public static void
main(String[] args)
{
HashMap hMap = new
HashMap();
hMap.put("1","One");
hMap.put("2","Two");
hMap.put("3","Three");
Object obj =
hMap.remove("2");
System.out.println(obj
+ " Removed from HashMap");
}
}
Simple Java Hash Map
import
java.util.HashMap;
public class
JavaHashMapExample
{
public static void
main(String[] args)
{
HashMap hMap = new
HashMap();
hMap.put("One",
new Integer(1));
hMap.put("Two",
new Integer(2));
Object obj =
hMap.get("One");
System.out.println(obj);
}
}
Check if a particular
element exists in Java Hash Set
import
java.util.HashSet;
public class
CheckElementHashSetExample
{
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"));
boolean blnExists =
hSet.contains(new Integer("3"));
System.out.println("3
exists in HashSet ? : " + blnExists);
}
}
Copy all elements of
Java Hash Set to an Object Array
import
java.util.HashSet;
public class
CopyElementsOfHashSetToArrayExample
{
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"));
Object[] objArray =
hSet.toArray();
System.out.println("HashSet
elements are copied into an Array.
Now Array
Contains..");
for(int index=0; index
< objArray.length ; index++)
System.out.println(objArray[index]);
}
}
Get Size of Java Hash
Set
import
java.util.HashSet;
public class
GetSizeOfJavaHashSetExample
{
public static void main(String[] args)
{
HashSet hSet = new
HashSet();System.out.println("Size of HashSet : " + hSet.size());
hSet.add(new
Integer("1"));
hSet.add(new
Integer("2"));
hSet.add(new
Integer("3"));
System.out.println("Size
of HashSet after addition : " + hSet.size());
hSet.remove(new
Integer("1"));
System.out.println("Size
of HashSet after removal : " + hSet.size());
}
}
Iterate through
elements of Java Hash Set
import
java.util.HashSet;
import
java.util.Iterator;
public class
IterateThroughElementsOfHashSetExample
{
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"));
Iterator itr =
hSet.iterator();
System.out.println("HashSet
contains : ");
while(itr.hasNext())
System.out.println(itr.next());
}
}
Remove all elements
from Java Hash Set
import
java.util.HashSet;
public class
RemoveAllElementsFromHashSetExample
{
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 before removal : " +
hSet);
hSet.clear();
System.out.println("HashSet
after removal : " + hSet);
System.out.println("Is
HashSet empty ? " + hSet.isEmpty());
}
}
Remove specified
element from Java Hash Set
import java.util.HashSet;
public class
RemoveSpecifiedElementFromHashSetExample
{
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
before removal : " + hSet);
boolean blnRemoved =
hSet.remove(new Integer("2"));
System.out.println("Was
2 removed from HashSet ? " + blnRemoved);
System.out.println("HashSet
after removal : " + hSet);
}
}
No comments:
Post a Comment