package com.kt4study.corejava.collection;
import java.util.HashMap;
/**
* @author kt4study.blogspot.co.uk
*
*/
public class HashMapExample {
public static HashMap<String, Integer> getHashMap() {
// Create An Instance Of HashMap
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
// Add String as Key and Integer as Value in HashMap.
hashMap.put("No Of Players In Football", 11);
hashMap.put("No Of Players In Chess", 1);
hashMap.put("No Of Players In Volleyball", 6);
hashMap.put(null, 20);
return hashMap;
}
public static void main(String[] args) {
HashMap<String, Integer> hashMap = HashMapExample.getHashMap();
// Print all the elements From HashMap
for (String key : hashMap.keySet()) {
System.out.println("Element from HashMap:: " + key + " - " + hashMap.get(key));
}
}
}
|
Output → HashMap can have "null" key and "null" value. All keys are unique in HashMap.
Element from HashMap:: null - 20
Element from HashMap:: No Of Players In Volleyball - 6
Element from HashMap:: No Of Players In Football - 11
Element from HashMap:: No Of Players In Chess - 1
No comments:
Post a Comment