Introduction:
The comparator Comparable is the core of this section.
The main content
- Revisiting the Arrays class
- The use of two comparators
The specific content
The Arrays class
The java.util.arrays.sort () class is a utility class provided in the java.utill package that performs all the functions related to Arrays.
Public static int binarySearch(datatype [] a, datatype key). But if you’re going to do binary lookup, one of the things you need to do is have an array that’s sorted.
Example: Implement binary search
public class TestDemo { public static void main(String args[]) { int data [] = new int [] {1, 5, 6, 2, 3, 4, 9, 8, 7, 10}; java.util.Arrays.sort(data); System.out.println(Arrays. BinarySearch (data, 9)); }}Copy the code
The output
8
Copy the code
The Arrays class provides array comparisons: public static Boolean equals(data type [] A, data type [] A2) using the equals() method name.
Example: Implement binary search
public class TestDemo { public static void main(String args[]) { int dataA [] = new int [] {1, 2, 3}; int dataB [] = new int [] {2, 1, 3}; int dataC [] = new int [] {1, 2, 3}; System.out.println(Arrays.equals(dataA, dataB)); System.out.println(Arrays.equals(dataA, dataC)); }}Copy the code
The output
false
ture
Copy the code
To determine whether the arrays are the same, you need to have the exact same order.
- Public static void fill(data type [] a, data type val).
- Public static String toString(datatype [] a).
Example: Use the method above
public class TestDemo { public static void main(String args[]) { int data[] = new int[10]; Arrays.fill(data, 3); System.out.println(Arrays.toString(data)); }}Copy the code
The output
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
Copy the code
The above is actually the basic operation of arrays, but such operations are rarely seen in real development.
Comparator: Comparable (Core)
Public static void sort(Object[] a) public static void sort(Object[] a) The Arrays class lets you sort objects directly using sort().
Example: Write test code
public class Book { private String title; private double price; public Book(String title, double price) { this.title = title; this.price = price; } public String to String() {return "this.title +" price: "+ this.price; }} public class TestDemo {public static void main(String args[]) {Book Book [] = new Book [] {new Book("Java development ", 79.8), New Book("JSP Development ", 69.8), New Book("Oracle Development ", 99.8), New Book("Android Development ", 89.8),}; Arrays.sort(books); System.out.println(arrays.tostring (books)); }}Copy the code
The output
The Exception in the thread "main" Java. Lang. ClassCastException: Book always be cast parable to java.lang.Com...Copy the code
There is only one reason for such an exception: a mandatory conversion of c unrelated class objects.
Every object in fact only have address information, address there is content, so if it is the ordinary type int array to compare, as long as the judge size is enough, but if it is an array of objects, if only with coding is no sense (address), take the code above, should be sorted by price is meaningful, The comparison rules are defined by the Comparable interface, which is defined as follows:
public interface Comparable<T> {
public int compareTo(T o);
}
Copy the code
The String class is essentially a subclass of the Comparable interface. The compareTo() method was used to compare objects. If you are comparing objects now, it is recommended that compareTo() return three types of data: 1 (greater than), 0 (equal to), and -1 (less than).
Example: Use a comparator
/** * Java learning exchange QQ group: 589809992 We learn Java together! */ public class Book implements Comparable<Book> {private String implements Comparable; private double price; public Book(String title, double price) { this.title = title; this.price = price; } @override public String to String() {return "this.title +" this.price; } @override public int compareTo(Book o) {// arrays.sort () } else if(this.price < o.price) { return -1; } else { return 0; Public class TestDemo {public static void main(String args[]) {Book Book [] = new Book [] {new Book("Java development ", 79.8), New Book("JSP Development ", 69.8), New Book("Oracle Development ", 99.8), New Book("Android Development ", 89.8),}; Arrays.sort(books); System.out.println(arrays.tostring (books)); }}Copy the code
The output
[Title: JSP development price: 69.8, title: Java development price: 79.8, title: Android development price: 89.8, title: Oracle development price: 99.8]Copy the code
The compareTo() method is automatically called by arrays.sort ().
In any future situation where a group of objects is to be sorted, the class in which the objects belong must implement the Comparable interface.
Saved Comparator: Comparator
The main feature of the Comparable interface is that it is implemented by default when the class is defined, so if there is a class that has been developed, then the Comparable interface is implemented by default when the class is defined. But due to the initial design did not arrange such an array of objects of sorting, then suddenly needs to have the sort of an array of objects, so this time, in the case of can’t modify the Book class definition is cannot use the Comparable interface, therefore, in order to solve this problem in Java, in Java again another comparator: Parator at java.util.Com.
There were originally two methods on the Comparator interface:
@FunctionalInterface
public interface Comparator<T> {
public int compare(T o1, T o2);
public boolean equals(Object obj);
}
Copy the code
All you really need to implement is compare(). You need to prepare a separate class to implement the Comparator interface. This class will act as the sorting class for the specified class.
Example: Define the sorting utility class
public class BookComparator implements Comparator<Book> { @Override public int compare(Book o1, Book o2) { if(o1.getPrice() > o2.getPrice()) { return 1; } else if(o1.getPrice() < o2.getPrice()) { return -1; } else { return 0; }}}Copy the code
The Comparable interface used the sort() method from the Arrays class, but now you can use another overloaded sort() method with a different interface: public static <T> void sort(T[] a, Comparator<? Super T > c).
Example: Implement sorting
Public class TestDemo {public static void main(String args[]) {Book Book [] = new Book [] {new Book("Java development ", 79.8), New Book("JSP Development ", 69.8), New Book("Oracle Development ", 99.8), New Book("Android Development ", 89.8),}; Arrays.sort(books, new BookComparator()); System.out.println(arrays.tostring (books)); }}Copy the code
Output result:
[Title: JSP development price: 69.8, title: Java development price: 79.8, title: Android development price: 89.8, title: Oracle development price: 99.8]Copy the code
Using the Comparator is cumbersome because you define a special sort class and specify a collation class when calling a sort.
Comparable: Please explain the difference between Comparable and Comparator.
- If an array of objects is to be sorted then a collation rule must be set. This can be done using the Comparable or Comparator interface.
- Java.lang.Com parable is an interface that needs to be implemented when a class is defined so that its array of objects can be sorted. A public int compareTo() method is defined under the Comparable interface.
- Java.util.Com parator is a salvage comparison operation that defines a class comparison rule. It has two methods: compare() and equals().
conclusion
In any case, the Comparable interface is mostly used when sorting arrays of objects.