Saved Comparator: Comparator

Posted by loquela on Mon, 18 Oct 2021 20:29:32 +0200

/** saved comparator: comparator * the main feature of the comparable interface is that it implements the interface by default when the class is defined **
Because the initial design did not arrange the sorting of such object arrays, it was suddenly necessary to realize the sorting of object arrays*
If you can't modify book at this time, you can't modify Comparable. You can only use java. util. Comparator * *
There are two methods in Comparator * int compare(T o1,
T o2) compare the order of its two parameters. Returns a negative integer, zero, or positive integer because the first argument is less than, equal to, or greater than the second argument. In the previous description, the symbolic sgn() expression represents a mathematical symbolic function that defines - 1, 0, or 1 returned depending on whether the value of the expression is negative, zero, or positive.

The implementation program must ensure that sgn(compare(x, y)) == -sgn(compare(y, x)) all x and y.
(this means that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementer must also ensure that the relationship is transitive: ((compare (x, y) > 0) & & (compare (y, z) > 0))
It means compare (x, z) > 0.

Finally, the implementer must ensure that compare(x, y)==0 means sgn(compare(x, z))==sgn(compare(y,
z)) for all Z.

Generally, but not strictly required (compare (x, y) = = 0) = (x.equals (y)).
In general, any comparator who violates this situation should make this clear. The recommended language is "note: this comparator forces an inconsistent sort with or equal to". boolean
equals(Object obj) indicates whether some other object is equal to this comparator.
This method must comply with the general contract of Object.equals(Object). In addition, only the specified object is also a comparator, and this method can only return true
, and it is in the same order as this comparator. Therefore, comp1.equals(comp2) means sgn(comp1.compare(o1,
o2) = = SGN (Comp2. Compare (o1, o2)) references o1 and o2 for each object. Note that,
It is always safe not to override Object.equals(Object). However, in some cases, overriding this method can be done by allowing the program to determine two
Different comparators apply the same order to improve performance.

The real thing to implement is the compare method. You need to prepare a separate class to implement the Comparator interface as the sorting class

The sort() method in the Arrays class was used when using the Comparable interface, but now
After replacing an interface, you can use another sort() method public static void sort(T[] a,
Comparator <? Super T > C) * * * Please explain comparator and Comparable*
If the object array is to be sorted, the sorting rules must be set. You can use Comparator and Comparable*
java.long.Comparable implements a good interface when a class is defined. In this way, a public int is defined under the object array interface * of benlei
compareTo method, * java.util.Comparator is a comparison rule that specifically defines a specified class and belongs to a saved comparison operation*
public int comparable() public boolean equals()

package leiku;

import java.util.Arrays;
import java.util.Comparator;


```java
class Book131  ///Implementation comparison
{
	private String title;
	private double price;
	
	public Book131() {}
	public Book131(String title,double price)
	{
		this.title = title;
		this.price = price;
	}
	
	@Override
		public String toString() {
			// TODO Auto-generated method stub
			return "title"+this.title+"Price"+this.price+"\n";
		}
	
	public void setPrice(double price)
	{
		this.price = price;
	}
	
	public double getPrice() 
	{
		return price;
	}
	
	public void setTitle(String title) 
	{
		this.title = title;
	}
	
	public String getTitle()
	{
		return title;
	}
}

/*
 * Defines the tool class for sorting
 */

class BookComparator implements Comparator<Book131>
{
	@Override
	public int compare(Book131 o1, Book131 o2) 
	{
		// TODO Auto-generated method stub
		if(o1.getPrice()>o2.getPrice())
		{
			return 1;
		}
		else if(o1.getPrice()<o2.getPrice())
		{
			return -1;
		}
		else 
		{
			return 0;
		}
		
	}
}
public class bijiaoqiComparator {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Book131 books[] = new Book131[]
				{
					new	Book131("asda",123),
					new	Book131("asdsdfa",131233),
					new	Book131("assfdsfda",1),
					new	Book131("asafasdfda",131)
				};
		Arrays.sort(books,new BookComparator());
		System.out.println(Arrays.toString(books));
	}

}

Topics: Java