Java foundation - Comparable and Comparator interfaces difference

to sum up

Comparable corresponds to the "internal comparator 'Comparator and corresponds to the" external comparator. "

    
    The Comparable Example //: 
    the Collections.sort (list); 

    // Comparator Example: 
    // the "comparator (AscAgeComparator)", of the sort list, AscAgeComparator is sorted is: The "age" in ascending order of 
    the Collections.sort ( list, new new AscAgeComparator ()); 
    // the "comparator (DescAgeComparator)", of the sort list, DescAgeComparator is sorted is: sorted according to "age" in descending 
    Collections.sort (list, new DescAgeComparator () );

  

  • Comparable is the sort interface. If a class implements the Comparable interface, it means "This class supports sorting." Now that the class supports sorting Comparable interface implemented, assuming the presence of "a list of objects implemented List Comparable interface class (or array)" Now, the List list (or array) may be carried out by the Collections.sort (or Arrays.sort) Sort. In addition, "object classes implement the Comparable interface" may be used as "ordered map (e.g., the TreeMap)" key or "ordered set (TreeSet)" elements, without specifying the comparator.
  • Comparator is a comparator interface. If we need to control the order of a class, but the class itself does not support ordering (ie not implement the Comparable interface); then we can build a "class Comparator" to sort. The "comparator" need only implement Comparator interface can be. In other words, we can then sort the class by the comparator "to create a new class Comparator comparator achieve" through.

 

Comparable interface

Comparable Profile

Comparable is the sort interface.

If a class implements the Comparable interface, it means "This class supports sorting." Now that the class supports sorting Comparable interface implemented, assuming the presence of "a list of objects implemented List Comparable interface class (or array)" Now, the List list (or array) may be carried out by the Collections.sort (or Arrays.sort) Sort.

In addition, "object classes implement the Comparable interface" may be used as "ordered map (e.g., the TreeMap)" key or "ordered set (TreeSet)" elements, without specifying the comparator.

 

Comparable definitions

Comparable interface only includes only one function, which is defined as follows:

package java.lang;
import java.util.*;
public interface Comparable<T> {
  public int compareTo(T o);
}

  

Description:

Suppose we pass x.compareTo (y) to "compare the size of x and y are." If return "negative", means "x smaller than Y"; return to "zero", means "x is equal to Y '; Back" positive ", means" x is greater than Y. "

 

Comparator Interface

Comparator Introduction

Comparator is a comparator interface.

If we need to control the order of a class, but the class itself does not support ordering (ie not implement the Comparable interface); then we can build a "class Comparator" to sort. The "comparator" need only implement Comparator interface can be.

In other words, we can then sort the class by the comparator "to create a new class Comparator comparator achieve" through.

Comparator defined

Comparator interface only includes only two functions, which is defined as follows:

package java.util;
public interface Comparator<T> {
  int compare(T o1, T o2);
  boolean equals(Object obj);
}

  

Description:

(01) if a class to implement Comparator interface: it must be achieved compareTo (T o1, T o2) function, but may not be implemented equals (Object obj) function.

Why can not implement equals (Object obj) function? Because any class, the default is already achieved equals (Object obj) of. Java, all classes are derived from java.lang.Object, implements equals (Object obj) function in Object.java in; therefore, all the other classes also have achieved the equivalent function.

(02) int compare (T o1, T o2) is "to compare the size of o1 and o2." Return to "negative", which means "o1 o2 than small"; return to "zero", meaning "o1 o2 equal"; return "positive", meaning "o1 is greater than o2."

 

 

 Examples -  Comparator and Comparable compare

Comparable is the sort interfaces; if a class implements the Comparable interface, it means "This class supports sorting."

The Comparator is a comparator; if we need to control the order of a class, you can create a "class Comparator" to sort.

We can easily find: Comparable equivalent of "internal comparator", and Comparator equivalent of "external comparators."

We explained to the two interfaces through a test program. Source as follows:

Classes in java.util * Import;. 
Import java.lang.Comparable; 
/ ** 
 * @desc "Comparator" and "Comparable" in comparison programs. 
 * (01) "the Comparable" 
 * It is a sort interface contains only one function compareTo (). 
 * A class implements the Comparable interface, it means "the class itself supports sorting", which can be sorted directly through Arrays.sort () or Collections.sort (). 
 * (02) "Comparator" 
 * it is a relatively interface comprising two functions: compare () and equals (). 
 * A class implements Comparator interface, then it is a "comparator." Other classes may be sorted according to the comparator. 
 * 
 * In summary: Comparable is internal comparator, and the comparator is an external Comparator. 
 * A class itself implements the Comparable comparator, it means that it supports sorting itself; if it did not implement Comparable itself, it can also be sorted by external comparators Comparator. 
 * / 
Public class CompareComparatorAndComparableTest { 
  public static void main (String [] args) { 
    // new ArrayList (dynamic array)
    ArrayList <the Person> List = new new ArrayList <the Person> (); 
    // add the object to the ArrayList 
    List.add (new new the Person ( "CCC", 20 is)); 
    List.add (new new the Person ( "the AAA", 30)) ; 
    List.add (new new the Person ( "BBB", 10)); 
    List.add (new new the Person ( "ddd", 40)); 
    // list printing original sequence 
    System.out.printf ( "original sort, list: S% \ n-', list); 
    // sort the list of 
    // based here, "" sort, i.e. based on "Person implemented Comparable <String> Interface sort name" 
    the Collections.sort (list); 
    the System. form out.printf ( "Sort the Name, list:% S \ n-', list); 
    // the" comparator (AscAgeComparator) ", of the sort list 
    // AscAgeComparator is sorted is: the" age "ascending order 
    Collections .sort (list, new AscAgeComparator () );
    System.out.printf ( "the Asc (Age) Sort, list:% S \ n-', list); 
    // the" comparator (DescAgeComparator) ", of the sort list 
    // DescAgeComparator is sorted is: The" age "descending order of 
    the Collections.sort (List, new new DescAgeComparator ()); 
    System.out.printf (" Asc (Age) Sort, List:% S \ n-', List); 
    // determines whether the same person two 
    testEquals ( ); 
  } 
  / ** 
   * @desc two Person Comparative test for equality. 
   * Due to Person implements equals () function: if two person's age, name are equal, are considered equal two person. 
   * So, here's equal p1 and p2. 
   * 
   * The TODO: if in Person equals remove () function, p1 is not equal to P2 
   * / 
  Private static void testEquals () { 
    Person p1 = new new Person ( "Eee", 100); 
    Person P2 = new new Person ( "Eee" , 100); 
    IF (p1.equals (P2)) { 
      System.out.printf ( "EQUAL% S% S \ n-", P1, P2); 
    } the else { 
      System.out.printf ( "the NOT EQUAL% S% S \ n-", P1, P2); 
    } 
  } 
  / ** 
   * @desc the Person class.
   * Person implements Comparable interface, which means that Person itself supports sorting 
   * / 
  Private static class Person the implements Comparable <Person> { 
    int Age; 
    String name; 
    public Person (String name, int Age) { 
      this.name = name; 
      the this. age = age; 
    } 
    public String getName () { 
      return name; 
    } 
    public int getAge () { 
      return age; 
    } 
    public String toString () { 
      return name + " - " +age;
    } 
    / ** 
     * compare two Person equality: if their name and age are equal, that they are equal 
     * / 
    Boolean the equals (the Person Person) { 
      IF (this.age this.name == == person.age && PERSON.NAME) 
        return to true;
      to false return; 
    } 
    / ** 
     * @desc achieve "Comparable <String>" interface, i.e., rewriting compareTo <T t> function. 
     * This is compared by the "person name" 
     * / 
    @Override 
    public int the compareTo (the Person Person) { 
      return name.compareTo (PERSON.NAME); 
      // return this.name - PERSON.NAME; 
    } 
  } 
  / ** 
   * @desc AscAgeComparator comparator 
   * it "age of ascending comparator Person" 
   * / 
  Private static class AscAgeComparator the implements comparator <Person> {  
    @Override
    public int Compare (P1 Person, Person P2) { 
      return p1.getAge () - P2. getAge (); 
    } 
  } 
  / ** 
   * @desc comparator DescAgeComparator
   * It is the "age of the comparator descending Person" 
   * / 
  Private static class DescAgeComparator the implements Comparator <Person> { 
    @Override 
    public int Compare (P1 Person, Person P2) { 
      return p2.getAge () - p1.getAge (); 
    } 
  } 
}

  

Running the program, the output is as follows:

Original sort, list:    [ccc - 20, AAA - 30, bbb - 10, ddd - 40]
Name   sort, list:     [AAA - 30, bbb - 10, ccc - 20, ddd - 40]
Asc(age) sort, list:   [bbb - 10, ccc - 20, AAA - 30, ddd - 40]
Desc(age) sort, list: [ddd - 40, AAA - 30, ccc - 20, bbb - 10]
eee - 100 EQUAL eee - 100

  

 

reference

Detailed difference in Java Comparable and Comparator interfaces  https://www.jb51.net/article/123097.htm

Guess you like

Origin www.cnblogs.com/frankcui/p/12118174.html