/*map extended knowledge. The map collection is used because of the mapping relationship. The following data are programmed: "Yureban", "Zhangsan", "Yureban", "Lisi", "Jiuyeban", "Wangwu", "02" Zhaoliu "Note that a key corresponds to a value, so according to the above example, it should be thought that the" value "itself is also a collection to store many pairs of values that have a mapping relationship. */import java.util.*;class MapTest4 {public static void main (string[] args) {hashmap<string,hashmap<string, String>> hm=new hashmap<string,hashmap<string,string>> (); Hashmap<string,string> yure=new hashmap<string,string> (); Hashmap<string,string> jiuye=new hashmap<string,string> () yure.put ("n", "yure_stu_001"); Yure.put ("02 "," yure_stu_002 "), Jiuye.put (" jiuye_stu_01 "), Jiuye.put (" jiuye_stu_02 "," Hm.put "), Yure (" a ", yure); Hm.put (" Jiuye ", Jiuye),//print_1 (HM," Yure "),//sop ("--------"),//print_2 (HM," Jiuye ");p rint_all1 (HM);} public static void Print_all1 (Hashmap<string,hashmap<string,string>> hm) {set<map.entry<string, Hashmap<string,string>>> Set=hm.entryset (); iterator<map.entry<string,hashmap<string,string>>> It=set.iterator (); while (It.hasnext ()) {map.entry<string,hashmap<string,string>> me= It.next (); String Key=me.getkey (); Hashmap<string,string> Hash=me.getvalue (); Set<map.entry<string,string>> s=hash.entryset ();iterator<map.entry<string,string>> it1= S.iterator (); while (It1.hasnext ()) {map.entry<string,string> me1=it1.next (); String Sub_key=me1.getkey (); String sub_value=me1.getvalue (); SOP (key+ "..." +sub_key+ "..." +sub_value);}} public static void Print_all2 (Hashmap<string,hashmap<string,string>> hm) {set<string> set= Hm.keyset (); Get the Classroom name Iterator<string> It=set.iterator (); while (It.hasnext ()) {hashmap<string,string> Hash=hm.get ( It.next ()); Set<map.entry<string,string>> s=hash.entryset ();iterator<map.entry<string,string>> it1= S.iterator (); while (It1.hasnext ()) {map.entry<string,string> me=it1.next (); String Key=me.getkey (); String value=me.getvalue (); SOP ("Print---> +key+" ... "+valUE);}}} public static void Print_1 (Hashmap<string,hashmap<string,string>> hm,string) {hashmap<string, String> Hash=hm.get (guest); Set<string> set=hash.keyset ();iterator<string> it=set.iterator (); while (It.hasnext ()) {String key= It.next (); String Name=hash.get (key); SOP (key+ "..." +name);}} public static void Print_2 (Hashmap<string,hashmap<string,string>> hm,string) {hashmap<string, String> Hash=hm.get (guest); Set<map.entry<string,string>> set=hash.entryset ();iterator<map.entry<string,string>> it= Set.iterator (); while (It.hasnext ()) {map.entry<string,string> me=it.next (); String Key=me.getkey (); String value=me.getvalue (); SOP (key+ "..." +value);}} public static void Sop (Object obj) {System.out.println (obj);}}
/*map Extended Knowledge (cont.). The programming implementation stores data for the following structures: "Yureban" Student ("001", "Zhangsan") "Yureban" Student ("002", "Lisi") "Jiuyeban" Student ("001", "Wangwu" "Jiuyeban" Student ("002", "Zaholiu") Problem Solving Analysis: The class name can not be used as a key, the student object as a value in the map, because the key is repeated. However, you can construct a data structure that corresponds to a collection of class names that store information about multiple students. */import java.util.*;class student{private string no;private string name; Student (String no,string name) {this.no=no;this.name=name;} Public String Getno () {return no;} Public String GetName () {return name;} public boolean equals (Object obj) {if (!) ( obj instanceof Student)) throw new RuntimeException ("Not student object"); Student s= (Student) Obj;boolean flag=this.no.equals (s.no) && this.name.equals (s.name);//maptest5.sop ( this.no+ "..." +s.no);//maptest5.sop (flag); return flag;}} Class MapTest5 {public static void main (string[] args) {hashmap<string,arraylist<student>> hash=new HashMap <String,ArrayList<Student>> (); Arraylist<student> yure=new arraylist<student> () yure.add (New Student ("001", "zhangsan001"); Yure.add ( New Student ("0("zhangsan002")), Yure.add (New Student ("005", "zhangsan005")), Yure.add (New Student ("004", "zhangsan004")); Yure.add (New Student ("003", "zhangsan003")), Yure.add (New Student ("003", "zhangsan003")); Yure=quchong (yure); Hash.put ("Yure", yure); Arraylist<student> jiuye=new arraylist<student> () jiuye.add (New Student ("001", "lisi001"); Jiuye.add ( New Student ("002", "lisi002")), Jiuye.add (New Student ("005", "lisi005")), Jiuye.add (New Student ("004", "lisi004")); Jiuye.add (New Student ("003", "lisi003")), Jiuye.add (New Student ("004", "lisi004")), Jiuye=quchong (Jiuye); Hash.put (" Jiuye ", Jiuye);//sop (hash);p rint (hash);/*iterator it=yure.iterator (); while (It.hasnext ()) {Student s= (Student) It.next (), SOP (S.getno () + "..." +s.getname ()); */}public static arraylist<student> Quchong (arraylist<student> al) {arraylist<student> arrlist=new Arraylist<student> ();iterator<student> it=al.iterator (), while (It.hasnext ()) {Student s=it.next (); Arrlist.contains (s))) Arrlist.add (s);} return arrlist;} Public static void print (Hashmap<string,arraylist<student>> hm) {Set<map.entry<string,arraylist <Student>>> set=hm.entryset ();iterator<map.entry<string,arraylist<student>>> it= Set.iterator (); while (It.hasnext ()) {map.entry<string,arraylist<student>> me=it.next (); String Key=me.getkey (); Arraylist<student> al=me.getvalue ();iterator<student> it1=al.iterator (); while (It1.hasNext ()) {Student S=it1.next (); SOP (key+ "..." +s.getno () + "..." +s.getname ());}} public static void Sop (Object obj) {System.out.println (obj);}}
Java Map Extension Knowledge exercise