Java, HashTable, HashMap, TreeMap between the three types, as well as custom object is the same comparison, custom sorting, etc.

/ * 
The Map Collection: The collection is stored pairs. One to keep inside. But also to ensure uniqueness of the key. 
The Map 
	| --Hashtable: the underlying data structure is a hash table, the key can not be stored null null values. The collection is thread synchronization. low efficiency. Basic Obsolete 
	| --HashMap: underlying hash table data structure, and null null value is permitted key, the set is not synchronized. The hashtable Alternatively, high efficiency, does not guarantee the order. 
	| --TreeMap: binary tree is the underlying data structure. Threads are not synchronized. It may be used to map the set of sort keys. Guarantee the order of 
* / 

Import Classes in java.util *;. 

/ ** 
 * Student class implements the interface Comparable comparable 
 * / 
class Student the implements Comparable <Student> { 
    Private String name; 
    Private int Age; 

    Student (String name, int Age) { 
        the this name = .name; 
        this.age = Age; 
    } 

    / ** 
     * override compareTo method is the same as the interface with the composition determination target equals overwritten 
     * 
     * @param S 
     * @return
     * / 
    @Override
    @Override 
    public int the compareTo (Student S) { 
        int NUM = Integer.valueOf (this.age) .compareTo (s.age); 
        IF (NUM == 0) 
            return this.name.compareTo (s.name); 
        return NUM ; 
    } 

    / ** 
     * override hashcode method employed for the underlying hash algorithm container 
     * 
     * @return 
     * / 
    @Override 
    public int the hashCode () { 
        // a combined value as the name and age hashcode 
        return name.hashCode () + Age 34 *; 
    } 

    / ** 
     * override equals method to implement custom Comparative 
     * 
     * @param obj 
     * @return 
     * / 
    public Boolean equals (Object obj) {
        (! (the instanceof Student obj)) IF 
            the throw new new a ClassCastException ( "type mismatch"); 
        // cast class 
        Student s = (Student) obj; 
        whether // class member variables in the same comparison 
        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; 
    } 
} 

/ ** 
 * comparators 
 * /  
class StuNameComparator the implements comparator <Student> {
    public int Compare (Student S1, S2 Student) { 
        . int NUM = s1.getName () the compareTo (s2.getName ()); 
        IF (NUM == 0)
            return Integer.valueOf(s1.getAge()).compareTo(s2.getAge());
        return num;
    }
}

class Test2 {
    public static void main(String[] args) {
        HashMap<Student, String> hm = new HashMap<Student, String>();

        hm.put(new Student("lisi1", 21), "beijing");
        hm.put(new Student("lisi1", 21), "beijing"); //与上面相同不存
        hm.put(new Student("lisi1", 21), "tianjin");
        hm.put(new Student("lisi2", 22), "shanghai");
        hm.put(new Student("lisi3", 23), "nanjing");
        hm.put(new Student("lisi4", 24), "wuhan");
        hm.put(new Student("lisi4", 24), "wuhan");The same as above does not exist // 

        System.out.println ( "----------------- first remove a first embodiment keySet, iterator --------- ---------------- ");
        // first embodiment taken keySet, iterators 

        the Set <Student> hm.keySet keySet = (); 

        the Iterator <Student> IT = keySet.iterator (); 

        the while (it.hasNext ()) { 
            Student STU = it.next (); 
            String addr = hm.get (STU); 
            System.out.println (STU + "___" + addr); 
        } 

        System.out.println ( "-------------- --- The second way out entrySet, iterators ------------------------- "); 

        // The second way out entrySet, iterative is 
        the Set <of Map.Entry <Student, String >> hm.entrySet the entrySet = (); 

        the Iterator <of Map.Entry <Student, String >> entrySet.iterator ITER = (); 
 entrySet.iterator ();
        while (iter.hasNext()) {
            of Map.Entry <Student, String> = Me ITER .next ();
            Student stu = me.getKey(); 
            String addr = me.getValue (); 
            System.out.println (STU + "___" + addr); 
        } 

        System.out.println ( "- ------------- third embodiment --------------------------- removed for loop ") ; 

        // third embodiment taken for loop 
        for (of Map.Entry <Student, String> entry: hm.entrySet ()) { 
            System.out.println (entry.getKey () + "___" + entry.getValue ( )); 
        } 

        System.out.println ( "---------------- the TreeMap custom sorting, using a comparator --------------- ----------- "); 
        // the TreeMap custom sorting, using a comparator 
        the TreeMap <Student, String> (TM) = the TreeMap new new <Student, String> (new new StuNameComparator ()); 
        (TM).put(new Student("lisi1", 21), "beijing"); 
        tm.put (new new Student ( "lisi1", 21 is), "Beijing"); / / same as above does not exist
        tm.put(new Student("lisi1", 21), "tianjin");
        tm.put(new Student("lisi2", 22), "shanghai");
        tm.put(new Student("lisi3", 23), "nanjing");
        tm.put(new Student("lisi4", 24), "wuhan");
        tm.put(new Student("lisi4", 24), "wuhan"); //与上面相同不存
        for (Map.Entry<Student, String> entry : tm.entrySet()) {
            System.out.println(entry.getKey() + "___" + entry.getValue());
        }

        /**
         * -----------------第第一种取出方式 keySet, 迭代器-------------------------
         * lisi4,24___wuhan
         * lisi2,22___shanghai
         * lisi1,21___tianjin 
         * lisi3,23 ___ nanjing 
         * ----------------- second way out entrySet, iterators ------------------- ------ 
         * lisi4,24 Wuhan ___ 
         * lisi2,22 Shanghai ___ 
         * lisi1,21 Tianjin ___ 
         * ___ Nanjing lisi3,23 
         * --------------- third embodiment taken for loop ---- ----------------------- 
         * lisi4,24 ___ wuhan 
         * lisi2,22 ___ shanghai 
         * lisi1,21 ___ tianjin 
         * lisi3,23 ___ nanjing 
         * ---------- ------ TreeMap custom sort, using a comparator -------------------------- 
         * lisi1,21 Tianjin ___ 
         * lisi2,22 Shanghai ___ 
         * lisi3 , Nanjing 23 is ___ 
         * lisi4,24 Wuhan ___ 
         * / 
    } 
}

  

Guess you like

Origin www.cnblogs.com/smartsmile/p/11616167.html