Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between Comparable and Comparator interfaces in Java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report