Comparator is an interface, which can override the two methods of compare() and equals() for price comparison function; if it is null, it uses the default order of elements, such as a, b, c, d, e, f, g, that is, a, b, c, d, e, f, g, of course, the same is true for numbers.
compare(a,b) method: Return negative integer, zero or positive integer according to the first parameter, which is less than, equal to or greater than the second parameter.
equals(obj) method: Return true only if the specified object is also a Comparator and the same sort as this Comparator is forced to be implemented.
The second parameter of Collections.sort(list, new PriceComparator()); returns an int-type value, which is equivalent to a flag, telling the sort method in what order to sort the list.
The specific implementation code method is as follows:
Book Entity Class:
package com.tjcyjd.comparator; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.GregorianCalendar; import java.util.Iterator; import java.util.TreeMap; /** * Book entity class* * @author yjd * */ public class Book implements Comparable { // Define a class named Book, which is inherited by default from the Object class public int id;// Number public String name;// Name public double price; // Price private String author;// Author public GregorianCalendar calendar;// Publication date public Book() { this(0, "X", 0.0, new GregorianCalendar(), ""); } public Book(int id, String name, double price, GregorianCalendar calendar, String author) { this.id = id; this.name = name; this.price = price; this.calendar = calendar; this.author = author; } // Rewrite the method inherited from the parent class Object to meet the requirements of Book class information description public String toString() { String showStr = id + "/t" + name; // Define the string that displays the class information DecimalFormat formatPrice = new DecimalFormat("0.00");// Format price to two decimal places showStr += "/t" + formatPrice.format(price);// Format price showStr += "/t" + author; SimpleDateFormat formatDate = new SimpleDateFormat("yyyy year MM month dd day"); showStr += "/t" + formatDate.format(calendar.getTime()); // Format time return showStr; // Return class information string} public int compareTo(Object obj) {// Method in Comparable interface Book b = (Book) obj; return this.id - b.id; // Compare the size according to the id of the book, used for default sorting} public static void main(String[] args) { Book b1 = new Book(10000, "Dream of the Red Chamber", 150.86, new GregorianCalendar(2009, 01, 25), "Cao Xueqin, Gao E"); Book b2 = new Book(10001, "Romance of the Three Kingdoms", 99.68, new GregorianCalendar(2008, 7, 8), "Luo Guanzhong"); Book b3 = new Book(10002, "Water Margin", 100.8, new GregorianCalendar(2009, 6, 28), "Shi Nai'an"); Book b4 = new Book(10003, "Journey to the West", 120.8, new GregorianCalendar(2011, 6, 8), "Wu Cheng'en"); Book b5 = new Book(10004, "The Legend of the Condor Heroes", 10.4, new GregorianCalendar(2011, 9, 23), "Sohu"); TreeMap tm = new TreeMap(); tm.put(b1, new Integer(255)); tm.put(b2, new Integer(122)); tm.put(b3, new Integer(688)); tm.put(b4, new Integer(453)); tm.put(b5, new Integer(40)); Iterator it = tm.keySet().iterator(); Object key = null, value = null; Book bb = null; while (it.hasNext()) { key = it.next(); bb = (Book) key; value = tm.get(key); System.out.println(bb.toString() + "/t inventory:" + tm.get(key)); } } } Custom comparator and test classes:
package com.tjcyjd.comparator; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.GregorianCalendar; import java.util.Iterator; import java.util.List; public class UseComparator { public static void main(String args[]) { List<Book> list = new ArrayList<Book>(); // Array sequence Book b1 = new Book(10000, "Dream of Red Mansions", 150.86, new GregorianCalendar(2009, 01, 25), "Cao Xueqin, Gao E"); Book b2 = new Book(10001, "Romance of the Three Kingdoms", 99.68, new GregorianCalendar(2008, 7, 8), "Luo Guanzhong"); Book b3 = new Book(10002, "Water Margin", 100.8, new GregorianCalendar(2009, 6, 28), "Shi Nai'an"); Book b4 = new Book(10003, "Journey to the West", 120.8, new GregorianCalendar(2011, 6, 8), "Wu Chengen"); Book b5 = new Book(10004, "Tianlong Baba", 10.4, new GregorianCalendar(2011, 9, 23), "Sohu"); list.add(b1); list.add(b2); list.add(b3); list.add(b4); list.add(b5); // Collections.sort(list); // There is no default comparator, System.out.println("Elements in an array sequence:"); myprint(list); Collections.sort(list, new PriceComparator()); // Sort by price System.out.println("Sorted by book price:"); myprint(list); Collections.sort(list, new CalendarComparator()); // Sort by time System.out.println("Sorted by book public static void myprint(List<Book> list) { Iterator it = list.iterator(); // Get an iterator for traversing all elements in the list while (it.hasNext()) {// If there is an element in the iterator, it returns true System.out.println("/t" + it.next());// Show this element} } // Custom comparator: Sort by the price of the book static class PriceComparator implements Comparator { public int compare(Object object1, Object object2) {// Implement the method in the interface Book p1 = (Book) object1; // Casting Book p2 = (Book) object2; return new Double(p1.price).compareTo(new Double(p2.price)); } } // Implement the method in the interface Book p1 = (Book) object1; // Casting Book p2 = (Book) object2; return new Double(p1.price).compareTo(new Double(p2.price)); } } // Custom comparator: sort static class CalendarComparator implements Comparator { public int compare(Object object1, Object object2) {// Implement the method in the interface Book p1 = (Book) object1; // Casting Book p2 = (Book) object2; return p2.calendar.compareTo(p1.calendar); } } }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.