In the actual project development process, we often need to be sorted on an object or a collection of elements commonly used in two ways to achieve a certain interface. Common functions can be implemented relatively interfaces Comparable interface and Comparator interfaces, these two, what difference does it make?
About the Comparable interface
About the Comparable interface, which is located in java.lang.Comparable, implement this interface can be customized to sort by overriding its compareTo method, generally used for the entity classes, such as an object for students, according to their name, height, age, address and other sorts of goods based on the name, inventory, price ranking. The main section of the code below is the name of the student, age, address sort, when we rewrite its compareTo method for the collection of a student objects, we can order by calling Collections.sort (studentList) it can achieve the desired effect.
1 public class Students implements Comparable<Students> { 2 3 private String name; 4 private int age; 5 private String address; 6 7 public String getName() { 8 return name; 9 } 10 11 public void setName(String name) { 12 this.name = name; 13 } 14 15 public int getAge() { 16 return age; 17 } 18 19 public void setAge(int age) { 20 this.age = age; 21 } 22 23 public String getAddress() { 24 return address; 25 } 26 27 public void setAddress(String address) { 28 this.address = address; 29 } 30 31 @Override 32 public String toString() { 33 SB = the StringBuilder new new the StringBuilder (); 34 is sb.append ( "Name:") the append (. The this .name); 35 sb.append ( "Age:"). The append ( the this .age); 36 sb.append ( " address:. ") the append ( the this .Address); 37 [ return sb.toString (); 38 is 39 } 40 41 is // override sorting method according to ascending order of age, and then in descending order according to the name, the address ascending order and finally 42 is @Override 43 is public int the compareTo (O Students.) { 44 is int Result = 0 ; 45 Result = the this.age - o.getAge(); 46 if (0 == result){ 47 result = o.getName().compareTo(this.getName()); 48 if (0 == result){ 49 result = this.getAddress().compareTo(o.getAddress()); 50 } 51 } 52 return result; 53 } 54 55 }
About Comparator interface
Comparator interface on which is located the java.util.Comparator, implement this interface can be sorted by overwriting customize their methods compare, for example, the string list, in descending order according to their length; Integer set according to according to its ascending size (the Collections.sort () method is the default implementation ascending). In addition, for the array is sorted, you can also call Arrays.sort () to sort, the default is to sort according to dictionary order.
import java.util.*; public class CompareController1 implements Comparator<Integer> { @Override public int compare(Integer o1, Integer o2) { // int length1 = o1.length(); // int length2 = o2.length(); return o2 - o1; //按照降序排列 } public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(23); List.add ( 400 ); List.add ( 222 ); List.add ( 34 is ); the Collections.sort (List, new new CompareController1 ()); System.out.println (list.toString ()); Object [] Objects = List.toArray (); // for type String default dictionary table is sorted according to @ for int type, default is sorted in ascending Arrays.sort (Objects); // can not be directly printed arrays, such as printed address, may be used the foreach System.out.println (of Arrays.toString (Objects)); } }
Extended added:
About Collections.sort () and Arrays.sort ()
1) Collections.sort () method is in fact the underlying Arrays.sort (),
2) the underlying Arrays.sort () is divided into two, to meet certain conditions to call this sort legacyMergeSort, underlying that merge sort; if not, is TimSort
3) the underlying TimSort the length of the array according to distinguish, if the length of the array is less than 32, using a simple merge algorithm directly, i.e., binary insertion sort (binary merge sort); if the length is greater than 32, or merged algorithm.