import java.util.HashMap; public class TestHashMap { public static void main(String[] args) { // Declaring HashMap HashMap<String, String> hashMap = new HashMap<String, String>(); // Inserting key value in hashmap hashMap.put("S1", "Mars"); hashMap.put("S2", "Venus"); hashMap.put("S3", "Earth"); hashMap.put("S4", "Mercury"); hashMap.put("S5", "Jupiter"); hashMap.put("S6", "Saturn"); System.out.println("Printing key value of hashmap"); printHashMap(hashMap); // Remove element from hash map hashMap.remove("S1"); hashMap.remove("S2"); System.out.println("Printing key value of hashmap"); printHashMap(hashMap); } private static void printHashMap(HashMap<String, String> hashMap) { for (Object key : hashMap.keySet()) { System.out.println("key : " + key + ", value : " + hashMap.get(key)); } } } Output: Printing key value of hashmap key : S3, value : Earth key : S4, value : Mercury key : S5, value : Jupiter key : S6, value : Saturn key : S2, value : Venus key : S1, value : Mars Printing key value of hashmap key : S3, value : Earth key : S4, value : Mercury key : S5, value : Jupiter key : S6, value : Saturn
-
Blogger Comment
-
Facebook Comment
Subscribe to:
Post Comments
(
Atom
)
0 comments :
Post a Comment