In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to compare Java Comparable and Comparator, I believe that many inexperienced people do not know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
In the actual project development process, we often need to sort an object or elements in a collection, and the two common ways are to implement an interface. The common interfaces that can implement comparison functions are Comparable interface and Comparator interface, so what's the difference between the two?
About Comparable Interfac
With regard to the Comparable interface, which is located in java.lang.Comparable, you can rewrite its compareTo method for custom sorting, which is generally used in entity classes, for example, for student objects, sorting by name, height, age, address, etc., and goods by name, inventory, price, etc. The following code mainly sorts the names, ages and addresses of students. When we rewrite its compareTo method, for a collection of student objects, we can sort them by calling Collections.sort (studentList) to achieve the desired effect.
Public class Students implements Comparable {private String name; private int age; private String address; 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;} public String getAddress () {return address;} public void setAddress (String address) {this.address = address;} @ Override public String toString () {StringBuilder sb = new StringBuilder () Sb.append ("name:") .append (this.name); sb.append ("age:") .append (this.age); sb.append ("address:") .append (this.address); return sb.toString ();} / / rewrite the sorting method, first by age, then by name, and finally by address @ Override public int compareTo (Students o) {int result= 0; result= this.age-o.getAge () If (0 = = result) {result = o.getName () .compareTo (this.getName ()); if (0 = = result) {result = this.getAddress () .compareTo (o.getAddress ());}} return result;}}
About Comparator Interfac
With regard to the Comparator interface, which is located in java.util.Comparator, you can implement this interface by overriding its compare method for custom sorting, such as list for strings, sorted according to its length decreasing; according to the collection of Integer, ascending order is implemented by default in the Collections.sort () method according to its size. In addition, for array sorting, you can also call Arrays.sort () to sort, which by default is sorted according to dictionary order.
Import java.util.*;public class CompareController1 implements Comparator {@ Override public int compare (Integer o1, Integer O2) {/ / int length2 = o1.length (); / / int length3 = o2.length (); return o2-o1; / / in descending order} public static void main (String [] args) {List list = new ArrayList (); list.add (1); list.add (23); list.add (400); list.add (222); list.add (34) Collections.sort (list,new CompareController1 ()); System.out.println (list.toString ()); Object [] objects = list.toArray (); / / for String type, the default is to sort by dictionary table / / for int type, the default is to sort Arrays.sort (objects) in ascending order; / / the array cannot be printed directly, so you can use foreach System.out.println (Arrays.toString (objects)). }}
Expansion and supplement:
About Collections.sort () and Arrays.sort ()
1) the underlying layer of the Collections.sort () method is actually Arrays.sort ()
2) the bottom layer of Arrays.sort () is divided into two types. If certain conditions are met, the sort legacyMergeSort is called, and the bottom layer is merge sorting; if it is not satisfied, it is TimSort.
3) the underlying layer of TimSort distinguishes according to the length of the array. If the length of the array is less than 32, it directly uses a simple merging algorithm, that is, binary insertion sorting (binary merge sort); if the length is greater than 32, it is a merging algorithm.
After reading the above, have you mastered the method of comparing Java Comparable and Comparator? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.