Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the difference between Comparable and Comparator interfaces in Java

Shulou Source: shulou.com Published: 2022-06-02 08:31:17 09月28日 Update

This article introduces what is the difference between Comparable and Comparator interface in Java. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

Introduction to Comparable Comparable is a sorting interface

If a class implements the Comparable interface, it means that "this class supports sorting". In addition, objects of classes that implement the Comparable interface can be used as keys in ordered maps (such as TreeMap) or elements in ordered sets (TreeSet) without specifying a comparator.

The sizes of x and y are compared by x.compareTo (y) in the interface. If you return a negative number, it means that x is smaller than y; if you return zero, it means x is equal to y; if you return a positive number, it means that x is greater than y.

Comparator is the comparator interface

If we need to control the order of a class and the class itself does not support sorting (that is, it does not implement the Comparable interface), we can set up a "comparator of this class" for sorting. This "comparator" only needs to implement the Comparator interface. That is, we can create a new comparator by implementing the Comparator class, and then sort the classes through the comparator.

Int compare (T _ 1, T _ 2) is similar to x.compareTo (y) above, where positive numbers are returned when the collation is defined, and zero and negative numbers represent greater, equal and less than, respectively.

The connection between the two

Comparable is equivalent to an "internal comparator", while Comparator is equivalent to an "external comparator".

Code implementation

Package mytest;import java.util.*;/** * @ _ ooOoo_ * o8888888o * 88 "> public class LearnCompare {public static void main (String [] args) {List list = new ArrayList (); list.add (new Node (" yguo ", 25)) List.add (new Node ("msdfj", 22)); list.add (new Node ("skf", 20)); list.add (new Node ("sfe", 23)); System.out.println ("= = Age sort external comparator = =") Collections.sort (list, new Comparator () {@ Overridepublic int compare (Node o1, Node O2) {return o1.getAge ()-o2.getAge ();}}); for (Iterator it = list.iterator (); it.hasNext ();) {System.out.println (it.next ()) } System.out.println ("= Name sort external comparator ="); Collections.sort (list, new Comparator () {@ Overridepublic int compare (Node o1, Node O2) {return o1.getName (). CompareTo (o2.getName ());}}); for (Iterator it = list.iterator (); it.hasNext () ) {System.out.println (it.next ());} System.out.println ("= Age sort internal comparator ="); Collections.sort (list); for (Iterator it = list.iterator (); it.hasNext ();) {System.out.println (it.next ()) }} / / class Node implements Comparable {private String name;private int age;public Node (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 int compareTo (Node other) {if (age > other.getAge ()) return-1 political if (age < other.getAge ()) return 1 political return 0;} @ Overridepublic String toString () {return "Name" + name + "age" + age;}} about the difference between Comparable and Comparator interfaces in Java, I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Comparator interface sort means order content more positive negative help support yes that is code representation element interest can be passed size Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Tech Info Shulou Information Apple MySQL