[Java learning notes] the sub-interface of Map-difference between HashMap, map and hashmap
Elements stored in the HashMap set must overwrite the hashCode and equals methods (similar to HashSet)
1 import java. util. hashMap; 2 import java. util. iterator; 3 4 import cn. itcast. p2.bean. student; 5 6 public class HashMapDemo {7 8 public static void main (String [] args) {9/* 10 * store the Student object and the Student's home location in the map set by key and value 11 */12 HashMap <Student, String> hm = new HashMap <Student, string> (); 13 14 hm. put (new Student ("lisi", 38), "Beijing"); 15 hm. put (new Student ("zhaoliu", 24), "Shanghai"); 16 hm. put (new Student ("xiaoqiang", 31), "Shenyang"); 17 hm. put (new Student ("wangcai", 38), "Dalian"); 18 hm. put (new Student ("zhaoliu", 24), "Tieling"); 19 20 Iterator <Student> it = hm. keySet (). iterator (); 21 while (it. hasNext () 22 {23 Student key = it. next (); 24 String value = hm. get (key); 25 System. out. println (key. getName () + ":" + key. getAge () + "--" + value); 26} 27 28} 29 30}