Objects is a new tool class in Java 7 that provides tools to manipulate objects, most of which are "null pointers" safe.
Objects implements the following methods:
How the Compare (t,t,comparator<? Super T>) method is defined in the source code:
public static <T> int compare (t A, t B, comparator<? super T> C) { return (a = = B)? 0: C.compare (A, b); }
The above uses <T> defines a generic method, and the description of the <T> and type parameter T is referred to in the follow-up to generics.
Here you pass in two objects A and B, and create an object of the comparator interface implementation class, where the comparator interface defines the following methods:
int compare (t O1, T O2); Boolean equals (Object obj);
Therefore, the Compare method first determines whether the object A and object B are equal, if equal, returns 0, unequal, through the comparator interface implementation
Compare (A, B) method.
Let's take a look at how it's defined in real life:
Class User {public String name; public int age; Public User (String Name,int age) { this.name=name; this.age=age; } Protected String GetName () { return name; } public void SetName (String name) { this.name = name; } @Override public String toString () { return "name=" +name+ "\nage=" +age; } } public class Main {public User user1,user2;public static void Main (string[] args) {new Main (). init ();} public void init () {User user1=new User ("Aill", 25); User User2=new User ("Cill"); int Compare=objects.compare (user1, User2,new comparator<user> () {@Overridepublic int compare (user O1, user O2) {RE Turn o1.name.compareToIgnoreCase (o2.name);}); System.out.println (Compare);}}
Output:
-2
In the above program, the Compare (A, B) method is implemented, so we can customize some methods to determine the size of two objects in this method to
is compared by the name of O1 and O2, the Comparetoignorecase (String str) method is the method under the Java.lang.String class, which acts by the word
The canonical order compares two strings, regardless of the case.
The String class also implements the Comparable<string> interface:
Public final class String
comparetoignorecase (String str) method in the String class:
public int comparetoignorecase (String str) { return case_insensitive_order.compare (this, str); }
public static final comparator<string> Case_insensitive_order = new Caseinse Nsitivecomparator (); private static class Caseinsensitivecomparator implements Comparator<string>, java.io.Serializable { Use Serialversionuid from JDK 1.2.2 for interoperability private static final long Serialversionuid = 8575799 808933029326L; public int Compare (string s1, string s2) {int n1 = S1.length (); int n2 = S2.length (); int min = math.min (n1, N2); for (int i = 0; i < min; i++) {Char C1 = S1.charat (i); char C2 = S2.charat (i); if (c1! = C2) {c1 = Character.touppercase (C1); C2 = character.touppercase (C2); if (c1! = C2) {c1 = Character.tolowercase (C1); C2 = character.tolowercase (C2); if (c1! = C2) { No overflow because of numeric promotion return C1-C2; }}}} return n1-n2; }/** replaces the De-serialized object. */Private Object readresolve () {return case_insensitive_order;} }
The above defines a static inner class caseinsensitivecomparator and implements thecompare in the Comparable<string> interface (String s1,
String s2) method, Math.min (N1, N2) is the one that returns a small two int value. Gets the specified charat (int index) method with a For loop
The char value at the index. Finally, the char is converted to uppercase and lowercase by character.touppercase and character.tolowercase to determine if
Equal, if not equal, returns the value after subtracting their two ASCII codes, otherwise two char values are equal and return 0.
Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/43940265 Emotional Control _
Java Learning Note 20