Mon Oct 22, 2012 8:23 pm
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
public class ArraysWork {
public static void main(String[] arr) {
/* Create object of HashMap */
HashMap<String, String> hashMap = new HashMap<String, String>();
/* Add entries to hashMap (Keys,Values) */
hashMap.put("A", "Apple");
hashMap.put("B", "Book");
hashMap.put("C", "Car");
hashMap.put("D", "Dog");
hashMap.put("E", "Elephant");
hashMap.put("F", "Flower");
// Print the content of the hashMap
Set<Entry<String,String>> hashSet=hashMap.entrySet();
for(Entry entry:hashSet ) {
System.out.println("Key="+entry.getKey()+", Value="+entry.getValue());
}
System.out.println("-----------------------------");
// Print the size of hashMap
System.out.println("HashMap size="+hashMap.size());
// Checking and searching
if(hashMap.containsKey("X")) {
System.out.println("HashMap has a key X");
}else{
System.out.println("HashMap hasn't a key X");
}
// Remove the content of the hashMap
hashMap.clear();
// Check if the hashMap empty
if(hashMap.isEmpty())
{
System.out.println("The hashMap is empty");
}
}
}
Key=D, Value=Dog
Key=E, Value=Elephant
Key=F, Value=Flower
Key=A, Value=Apple
Key=B, Value=Book
Key=C, Value=Car
-----------------------------
HashMap size=6
HashMap hasn't a key X
The hashMap is empty
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com
Powered by phpBB © phpBB Group.