/* Each student has a corresponding place of attribution. Student student, address string. Student attributes: Name, age. Note: The same name and age are regarded as the same student. Ensure the uniqueness of students. 1, describe the student. 2, define the map container. Use the student as the key, the address as the value. Deposit. 3, gets the elements in the map collection. */import Java.util.*;class Student implements comparable<student>{private String name;private int age; Student (String Name,int age) {this.name = Name;this.age = age;} public int CompareTo (Student s) {int num = new Integer (this.age). CompareTo (New Integer (s.age)); if (num==0) return This.name.compareTo (s.name); return num;} public int hashcode () {return name.hashcode () +age*34;} public boolean equals (Object obj) {if (!) ( obj instanceof Student)) throw new ClassCastException ("Type Mismatch"); Student s = (Student) Obj;return this.name.equals (s.name) && this.age==s.age;} Public String GetName () {return name;} public int getage () {return age;} Public String toString () {return name+ ":" +age;}} Class Maptest{public static void Main (string[] args) {hashmap<student,string> HM = new HASHMAP<STUDENT,STRING&G t; (); Hm.put (New Student ("Lisi1"), "Beijing"); Hm.put (New Student ("Lisi1", +), "TiaNjin "), Hm.put (New Student (" Lisi2 "," Shanghai "), Hm.put (New Student (" Lisi3 "," Nanjing "); Hm.put (New Student Lisi4 "," Wuhan ");//First removal method keysetset<student> KeySet = Hm.keyset ();iterator<student> it = Keyset.iterator (); while (It.hasnext ()) {Student Stu = It.next (); String addr = Hm.get (stu); System.out.println (stu+ "..." +ADDR);} The second method of removal entrysetset<map.entry<student,string>> EntrySet = Hm.entryset ();iterator<map.entry< Student,string>> iter = Entryset.iterator (); while (Iter.hasnext ()) {map.entry<student,string> me = Iter.next (); Student stu = Me.getkey (); String addr = Me.getvalue (); System.out.println (stu+ "...") (+ADDR);}}
/* Requirements: Sort the age of the student objects in ascending order. Because the data exists in the form of a key-value pair. So you want to use a map collection that can be sorted. TreeMap. */import Java.util.*;class Stunamecomparator implements Comparator<student>{public int compare (Student s1, Student s2) {int num = S1.getname (). CompareTo (S2.getname ()); if (num==0) return new Integer (S1.getage ()). CompareTo (New Integer (S2.getage ())); return num;}} Class Maptest2{public static void Main (string[] args) {treemap<student,string> TM = new treemap<student,string& gt; (new Stunamecomparator ()); Tm.put (New Student ("Blisi3", "Nanjing"), Tm.put (New Student ("Lisi1"), "Beijing") Tm.put (New Student ("Alisi4", "Wuhan"), Tm.put (New Student ("Lisi1", "Tianjin") Tm.put (New Student ("Lisi2", 22 ), "Shanghai"); set<map.entry<student,string>> EntrySet = Tm.entryset ();iterator<map.entry<student,string> > it = Entryset.iterator (); while (It.hasnext ()) {map.entry<student,string> me = It.next (); Student stu = Me.getkey (); String addr = Me.getvalue (); System.out.println (stu+ ":::" +addr);}}}
/* Exercise: "SDFGZXCVASDFXCVDF" gets the number of occurrences of the letter in the string. Want to print results: A (1) C (2) ..... The results show that each letter has a corresponding number of times. Indicates that there are mappings between letters and times. Note that when a mapping relationship is found, you can select the Map collection. Because the map collection is stored as a mapping relationship. What about using the map collection? When there is a mapping between the data, you need to think of the map collection first. Idea: 1, converts a string into a character array. Because you want to operate on every single letter. 2, define a map collection, because the letters of the printed results are in order, so use the TreeMap collection. 3. Iterate through the character array. Use each letter as a key to check the map collection. If NULL is returned, the letter and 1 are stored in the map collection. If the return is not NULL, it indicates that the letter already exists in the map collection and has a corresponding number of times. Then get the number of times and self-increment. , and then deposit the letter and the number of times since the increment into the map collection. Overrides the value that corresponds to the calling principle key. 4, returns the data in the map collection into the specified string form. */import java.util.*;class maptest3{public static void Main (string[] args) {String s= charcount ("Ak+abaf1c,dckaabc-defa "); System.out.println (s);} public static string CharCount (String str) {char[] chs = Str.tochararray (); treemap<character,integer> TM = new treemap<character,integer> (); int count = 0;for (int x=0; x<chs.length ; X + +) {if (!) ( Chs[x]>= ' A ' && chs[x]<= ' z ' | | chs[x]>= ' A ' && chs[x]<= ' Z ')) Continue;integer value = Tm.get (Chs[x]); if (value!=null) Count = value;count+ +;tm.put (Chs[x],count);//store characters and numbers directly into the collection, why, because they are automatically boxed. Count = 0;/*if (value==null) {Tm.put(chs[x],1);} Else{value = value + 1;tm.put (chs[x],value);} */}//SYSTEM.OUT.PRINTLN (tm); StringBuilder sb = new StringBuilder (); set<map.entry<character,integer>> EntrySet = Tm.entryset (); Iterator<map.entry<character,integer >> it = Entryset.iterator (); while (It.hasnext ()) {map.entry<character,integer> me = It.next (); Character ch = me.getkey (); Integer value = Me.getvalue (); Sb.append (ch+ "(" +value+ ")");} return sb.tostring ();}}
Java-map Practice