Treeset sorting, storing custom objects, and user-defined comparator example

Source: Internet
Author: User
Tags comparable set set custom name
Set: unordered. elements cannot be repeated.
| -- Hashset: The data structure is a hash table. The thread is not synchronous.
Principle of ensuring element uniqueness: Determine whether the hashcode value of an element is the same.
If they are the same, the equals method of the element is determined to be true.

| -- Treeset: sorts the elements in the Set set.
The underlying data structure is a binary tree.
The basis for ensuring element uniqueness: compareto method return 0.

The first method of treeset sorting: Make the elements have a comparison.
The comparable interface must be implemented to override the compareto method.
It also becomes the natural sequence of elements, or the default sequence.

The second sorting method of treeset.
When the element itself does not have a comparison, or the comparison is not necessary.
In this case, the set itself needs to be compared.

When the set is initialized, a comparison method is available.


Example: requirement:
Store custom object students in the treeset set, and sort by student age.

Package Tan; import Java. util. iterator; import Java. util. treeset; public class treesetdemo {public static void main (string [] ARGs) {treeset Ts = new treeset (); Ts. add (new student ("tan1", 21); Ts. add (new student ("tan3", 20); Ts. add (new student ("tan2", 23); Ts. add (new student ("tan5", 21); iterator it = ts. iterator (); While (it. hasnext () {system. out. println (it. next () ;}} class student implements comparable {priv Ate string name; private integer age; Public student (string name, int age) {This. name = Name; this. age = age;} 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 ;}@ overridepublic string tostring () {return "name:" + this. name + "" + "Age:" + this. age ;}@ overridepublic int compareto (Object OBJ) {I F (! (OBJ instanceof student) throw new runtimeexception ("non-student object"); student s = (student) OBJ; If (this. age> S. age) return 1; // when the main conditions are the same, you must determine the secondary conditions. If (this. Age = S. Age) {return this. Name. compareto (S. Name);} return-1 ;}}

Custom Comparator

Package Tan; import Java. util. comparator; import Java. util. iterator; import Java. util. treeset; public class treesetdemo {public static void main (string [] ARGs) {// You can input the custom comparator treeset Ts = new treeset (New studentagecomparator () here ()); ts. add (new student ("tan01", 21); Ts. add (new student ("tan03", 20); Ts. add (new student ("tan03", 22); Ts. add (new student ("tan0012", 23); Ts. add (new student ("tan007", 21); iterator It = ts. iterator (); While (it. hasnext () {system. out. println (it. next () ;}} class student implements comparable {private string name; private integer age; Public student (string name, int age) {This. name = Name; this. age = age;} 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 ;}@ overridepublic String tostring () {return "name:" + this. Name + "" + "Age:" + this. Age ;}@ overridepublic int compareto (Object OBJ) {If (! (OBJ instanceof student) throw new runtimeexception ("non-student object"); student s = (student) OBJ; If (this. age> S. age) return 1; // when the main conditions are the same, you must determine the secondary conditions. If (this. age = S. age) {return this. name. compareto (S. name);} return-1 ;}// custom name comparator class studentnamecomparator implements comparator {@ overridepublic int compare (Object O1, object O2) {student S1 = (student) o1; Student S2 = (student) O2; int num = s1.getname (). compareto (s2.getname (); If (num = 0) {// because ingteger has implemented the comparable interface return New INTEGER (s1.getage ()). compareto (New INTEGER (s2.getage (); // You can also write/* If (s1.getage ()> s2.getage () return 1; if (s1.getage () = s2.getage () return 0; Return-1; */} return num ;}} // custom age comparator class studentagecomparator implements comparator <student >{@ overridepublic int compare (student O1, student O2) {int I = o1.getage ()-o2.getage (); return I ;}}

Exercise: sort by string length.


The string itself has a comparison, but its comparison method is not required. In this case, only the comparator can be used.

Package Tan; import Java. util. *; public class treesettest {public static void main (string [] ARGs) {treeset Ts = new treeset (New strlengthcomparator (); Ts. add ("ABCD"); Ts. add ("cc"); Ts. add ("CBA"); Ts. add ("AAA"); Ts. add ("Z"); Ts. add ("HAHAHA"); iterator it = ts. iterator (); While (it. hasnext () {system. out. println (it. next () ;}} class strlengthcomparator implements comparator {@ overridepublic int compare (Object O1, object O2) {string S1 = (string) O1; string S2 = (string) o2; int num = new INTEGER (s1.length ()). compareto (New INTEGER (s2.length (); // when the first condition is met, the second condition is sorted in the natural order if (num = 0) {return s1.compareto (S2);} return num ;}}


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.