TreeSet the principle of guaranteed element uniqueness and sequencing __treeset

Source: Internet
Author: User
Tags comparable
TreeSet: Depending on the construction method, choose to sort using natural sorting or comparer.
The elements can be sorted according to the actual requirements. and guarantee the only. How to guarantee it?
Sort: The bottom structure is a two-fork tree. stored and removed according to the tree node.
Two implementations:
A: Natural Ordering (elements with comparative)
The TreeSet structure of the object requires that the class to which it belongs implements the comparable interface.
B: Comparator sorting (set with comparison)
The TREESET structure requires the construction method to receive an object that implements the comparator interface.
Unique: Depends on whether the return value is 0.
Attention:
If there are two options, who is the main one?
Sort by comparator way a pair of objects stored in the collection to implement the comparable interface, natural sorting
Package cn.liuwen.treeset; /** * Requirements: Store 3 student objects with a TreeSet collection, and then traverse the collection.
     (using natural sorting, adding sequential storage by Element) * @author Lwen * @version * @date July 7, 2015 * Requirements 2: According to the length of the object's name, from small to large sort, how to do?

* * Public class Student implements comparable<student>{private String name, private int age;
    Public Student () {} public Student (String name, int age) {this.name = name;
This.age = age;

public void SetName (String name) {this.name = name;}

Public String GetName () {return name;}

public void Setage (int age) {this.age = age;}

public int getage () {return age;} Mode one, implement the comparable interface//rewrite CompareTo () method @Override public int compareTo (Student o) {Student s = O) in the object class to which the element is to be saved;
    Object strongly converted to student type if (This.name.length ()-s.name.length () > 0) {//define comparison mode return 1; } if (This.name.length ()-s.name.length () = 0) {return this.name.compareTo (s.name);//returns the comparison result} RE
    turn-1; }///In the Main method pass in the above class object public class TreesetDemo1 {public static void main (string[] args) {treeset<student> ts = new treeset<student> ();
    Ts.add (New Student ("Liuwen", 22));
    Ts.add (New Student ("Lifuwen", 22));
    Ts.add (New Student ("Liuwen", 22));
    Ts.add (New Student ("Guixin", 12));
    Ts.add (New Student ("Guixiangd", 18));

    for (Student s:ts) {System.out.println (S.getname () + "* * *" +s.getage ());
 }
}
}
mode two treeset with parametric constructs that require the constructor to receive an object that implements the comparator interface.
public class Student2 {private String name, private int age; private int score;
    Public Student2 () {super ();
    TODO auto-generated Constructor stub} public Student2 (String name, int age, int score) {super ();
    THIS.name = name;
    This.age = age;
This.score = score;

Public String GetName () {return name;}

public void SetName (String name) {this.name = name;}

public int getage () {return age;}

public void Setage (int age) {this.age = age;}

public int Getscore () {return score;}

public void SetScore (int score) {This.score = score;} In the main method, pass in this class object public class TreeSetDemo3 {public static void main (string[] args) {//Using the anonymous inner class, override the Compare method, in this way the object
        To sort treeset<student2> ts = new treeset<student2> (new comparator<student2> () {@Override
            public int Compare (Student2 s1, Student2 s2) {int num = S2.getscore ()-s1.getscore (); Define sort Way int num2 = (num==0)?
            (S1.getage ()-s2.getage ()):(num); int num3 = (num2==0)?
            (S1.getname (). CompareTo (S2.getname ())):(num2);
        return num3;
    }
    });
    Ts.add (New Student2 ("Liuwen", 12,13));
    Ts.add (New Student2 ("Liuwen", 12,12));
    Ts.add (New Student2 ("Liuwen", 12,14));
    Ts.add (New Student2 ("Lauwen", 12,14));
    Ts.add (New Student2 ("Liuwen", 13,14));
    Ts.add (New Student2 ("Liuwen", 14,14));
    Ts.add (New Student2 ("Lauwen", 12,13));
    for (Student2 s:ts) {System.out.println (S.getname () + "* *" +s.getage () + "* *" +s.getscore ());
 }
}
}

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.