Java Learning Note 20

Source: Internet
Author: User
Tags comparable

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: (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.age=age;    }        Protected String GetName () {        return name;    }    public void SetName (String 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 (user1, User2,new comparator<user> () {@Overridepublic int compare (user O1, user O2) {RE Turn (;}); System.out.println (Compare);}}



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 (this, str);    }

public static final comparator<string> Case_insensitive_order = new Caseinse    Nsitivecomparator ();        private static class Caseinsensitivecomparator implements Comparator<string>, { 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: Emotional Control _  

Java Learning Note 20

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.