Java Map-hashmap, TreeMap exercises

Source: Internet
Author: User
Tags comparable

/* Review: As long as the underlying data structure is a collection of hash tables, overwrite the two methods to guarantee the uniqueness of the elements in the hash table. public int Hashcode () public boolean equals (Object obj) (1) HashMap guarantees the uniqueness of the element, does not repeat, (principle: Overrides the Hashcode () and Equals () methods in the object class), The object class can implement the comparable interface and implement the CompareTo () method so that the object is fairly comparable and has a sequence in the HashMap collection. (2) TreeMap enables elements to be stored in a certain order, which can be defined by default (natural order) and can be customized by the comparator. TreeMap the two methods guarantee the order of storage when storing custom objects. 1, the object itself is comparative, that is, the stored object class implements the comparable interface, and implements CompareTo () method 2, the custom comparer, which is the class Mycompare implements comparator{//implementation compare ( Object Obj1,object Obj2) method} and passes the comparer object as a parameter to the TreeMap constructor method. Practice: Each student has a corresponding place of attribution. Student student, address string student properties: Name, age note: Students with the same name and age are considered to be the same student. Ensure the uniqueness of students. 1, describe the Student 2, define the map container, the student as the key, address as the value. Deposit. 3, gets the value of the map container. */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 String GetName () {return name;} public int getage () {return age;} /* If the comparable generic is not specified as student, then the parameter needs to be an object Objpublic int compareTo (object obj)//sorted by Student Age {if (! ( obj instanceof Student)) throw new RuntimeException ("not student"); Student s= (Student)Obj;int num=new Integer (this.age). CompareTo (New Integer (S.age)), if (num==0) return This.name.compareTo (S.name); return num;} */public int CompareTo (Student s)//Sort by student age {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 RuntimeException ("not student"); Student s= (Student) obj;if (this.name.equals (s.name) && this.age==s.age) return True;return false;}} Class Maptest {public static void main (string[] args) {hashmap<student,string> map=new hashmap<student,string& gt; (); Map.put (New Student ("zhangsan002"), "place001"), Map.put (New Student ("zhangsan003", "place003"); Map.put (New Student ("zhangsan005", "Max"), "place005"), Map.put (New Student ("zhangsan004", "place004"); Map.put (new Student ("zhangsan002", "place009"), Map.put (New Student ("zhangsan001"), "place001");//Mode one set<student> Set=map.KeySet ();iterator<student> it=set.iterator (); while (It.hasnext ()) {Student s=it.next (); String Place=map.get (s); SOP ("Name:" +s.getname () + "... Age: "+s.getage () +" ... " +place);} Way two set<map.entry<student,string>> entryset=map.entryset (); iterator<map.entry<student,string >> It1=entryset.iterator (); while (It1.hasnext ()) {map.entry<student,string> me=it1.next (); Student s1= Me.getkey (); String place1=me.getvalue (); SOP ("Name:" +s1.getname () + "...". Age: "+s1.getage () +" ... place: "+place1);}}. public static void Sop (Object obj) {System.out.println (obj);}}

The above uses the comparable interface makes the student object have the comparison (according to the student age to order);

Using another comparison method, the custom comparer implements the comparator interface for sorting by student name.

/* Store student objects in the order of student names. At this point, you define the comparer to implement a custom sorting method. */import java.util.*;class MapTest2 {public static void main (string[] args) {treemap<student,string> tm=new Treemap<student,string> (New MyComp ()), Tm.put (New Student ("zhangsan002"), "place001"), Tm.put (New Student (" Zhangsan003 ",", "place003"), Tm.put (New Student ("zhangsan005", "Max"), "place005"), Tm.put (New Student ("zhangsan004", "place004"), Tm.put (New Student ("zhangsan002", "place009"), Tm.put (New Student ("zhangsan001"), "place001" ); Tm.put (New Student ("zhangsan001"), "place001"); Set<student> set=tm.keyset ();iterator<student> it=set.iterator (); while (It.hasnext ()) {Student s= It.next (); String Place=tm.get (s); Maptest.sop ("Name:" +s.getname () + "... Age: "+s.getage () +" ... " +place);}}} Class MyComp implements comparator<student>//Sort by name. {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;}}




Java Map-hashmap, TreeMap exercises

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.