JavaSE-JDK conventional comparator based -Java

In Java scheduling problems often involve an array of objects, then it comes to the comparison issues between objects.
Java object that implements the sort of way, there are two:
the natural ordering: java.lang.Comparable
custom sorting: java.util.Comparator

1. Natural Sort: java.lang.Comparable

  • Comparable interface imposes its object to realize a total ordering of each class. This ordering is called natural ordering class.
  • Implement Comparable class must implement compareTo (Object obj) method, i.e., two objects
    return value by comparing the size of compareTo (Object obj) method. If the current object is greater than this parameter the object obj, returns a positive integer, if the current object is less than this parameter the object obj, a negative integer is returned, if the current object is equal to this parameter the object obj, a zero is returned.
  • Comparable interface implement a list of objects (and arrays) may be performed automatically or sorted by Collections.sort Arrays.sort. Object implementing this interface can be used as elements of an ordered set of ordered key or the map without specifying the comparator.
  • Each of e1 and e2 for, if and only if e1.compareTo (e2) == 0 having the same boolean value that e1.equals (e2), natural ordering of class C to class C it is called uniform and equals. It recommended (though not required) to make the best natural ordering is consistent with equals.

1.1 Comparable typical implementation

The default is ascending order of

  • String: compare according to Unicode values ​​of characters in a string
  • Character: according to Unicode values ​​of characters to compare
  • Values ​​corresponding to the type of packaging and BigInteger, BigDecimal: according to numerical values ​​compared to their corresponding
  • Boolean: true wrapper class instance is greater than the corresponding wrapper class instance corresponding false
  • Date, Time, etc.: date and time later than the date and time of the foregoing large

2. Customize Sort: java.util.Comparator

  • When the type of the interface element is not yet achieved java.lang.Comparable convenient to modify the code, or to achieve the collation is not suitable interface java.lang.Comparable current operation, consider Comparator used to sort the object, the multi-force objects to compare overall ranking.
  • Rewriting compare (Object o1, Object o2) method, and size comparison o1 o2: if the method returns a positive integer, greater than said o1 o2; if return 0, indicating equal; return-negative integer less than o1 o2.  Comparator can be passed to a sort method (e.g. Collections.sort or Arrays.sort), thereby allowing precise control over the sort order.
  • Comparator may also be used to control certain data structures (e.g., ordered or sorted map set) in this order, or to provide for the collection of objects is not ordered natural order.
Goods[] all = new Goods[4];
all[0] = new Goods("book", 100);
all[1] = new Goods("food", 80);
all[2] = new Goods("phone", 140);
all[3] = new Goods("computer", 120);
Arrays.sort(all, new Comparator() {
	@Override
	public int compare(Object o1, Object o2) {
		Goods g1 = (Goods) o1;
		Goods g2 = (Goods) o2;
		return g1.getName().compareTo(g2.getName());
	}
});
System.out.println(Arrays.toString(all));
Published 337 original articles · won praise 77 · views 570 000 +

Guess you like

Origin blog.csdn.net/feicongcong/article/details/104901231