Java Comparator interface code example

Source: Internet
Author: User
Tags comparison java comparator

Set: The elements are unordered (the order of storage and retrieval is not necessarily the same) and cannot be repeated.
The set function is the same as the Collection function.
------ | HashSet:
How does one ensure the uniqueness of elements?
Is done through two methods of elements, hashCode and equlas.
If the hashCode value of the element is the same, the system checks whether equlas is true.
If the hashcode value of an element is different, the equlas method is not judged.
------ | TreeSet: sorts the elements in the Set. Two elements of sorting: set and element.
TreeSet stores custom objects. In sorting, when the main conditions are the same, you must determine the secondary conditions.
The underlying data structure is a binary tree. The basis for ensuring element uniqueness: compareTo method return 0;
    
The first method of TreeSet sorting is to make the elements have a comparison. The Comparable interface must be implemented to override the compareTo method.
This square form is used to sort elements naturally, or is called the default order.

The second sorting method of TreeSet.
When the element itself does not have the comparison or the comparison, it is not necessary.
This requires the set to have a comparison. When the set is initialized, a comparison method is available.
Define a class to implement the compareable interface and overwrite the compare method.
When both methods exist, the comparator is used as the main component.

The code is as follows: Copy code

Package com. day14.wd;

Import java. util. Comparator;
Import java. util. Iterator;
Import java. util. TreeSet;

Public class TreeSetDemo {
Public static void main (String [] ag ){
TreeSet ts = new TreeSet (new MyCompare ());
Ts. add (new Student ("lisi", 123 ));
Ts. add (new Student ("wangwu", 23 ));
Ts. add (new Student ("wef", 23 ));
Iterator it = ts. iterator ();
While (it. hasNext ()){
Student ss = (Student) it. next ();
System. out. println ("name" + ss. getName () + ", age" + ss. getAge ());
       
    }
    
    }

}
Class MyCompare implements Comparator {

Public int compare (Object o1, Object o2 ){
// TODO Auto-generated method stub
Student p1 = (Student) o1;
Student p2 = (Student) o2;
// Ensure uniqueness. 0 is returned;
Int num = p1.getAge ()-p2.getAge ();
If (num = 0 ){
Return p2.getName (). compareTo (p2.getName ());
} // String implements the compareTo () method
Return num;
    }
   
}

Class Student {
Private String name;
Private int 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;
    }
    }

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.