Today to learn a small point of knowledge, it is pair, first let us look at its short source:
public class Pair<f, s> {public final F first; Public final S second; /** * Constructor for a Pair. * @param first object in the pair * @param second the second object in the pair */public Pair ( F First, S second) {this.first = first; This.second = second; }/** * Checks the objects for equality by delegating to their respective * {@link object#equals (Object)} m Ethods. * * @param o the {@link Pair} to which this one are to being checked for equality * @return true if the underlying obj ECTs of the Pair is both considered * equal
* Note here: If the parameter passed here is not a pair type, it will return false directly.
* If the parameter is pair type, compare in near one step. The following will be explained in one step. */@Override public boolean equals (Object o) {if (! ( o instanceof Pair) {return false; } pair<?,? > P = (pair<?,? >) o; Return Objects.equal (P.first, first) && objects.equal (P.second, second); }/** * Compute a hash code using the hash codes of the underlying objects * * @return a hashcode of the Pa ir
* ^ Bitwise XOR or if the two bits values for the operation are the same, then 0, different otherwise 1 */@Override public int hashcode () {return (first = = null? 0:firs T.hashcode ()) ^ (second = = null? 0:second.hashcode ()); }/** * Convenience method for creating an appropriately typed pair. * @param a the first object in the pair * @param B, the second object in the pair * @return a pair it is Templati Zed with the types of a and b */public static <a, b> Pair <a, b> Create (a A, b b) {return new Pair<a, B> (A, B); }}
The best way to learn a knowledge point is practice, let's see how the pair is used!!! Take a look at the following code:
public class Pairactivity extends Activity {private final String TAG = "Pairactivity.class"; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Integer a = 3; Integer B = 3; LOG.E ("TAG", "--a.equals (b)----" +a.equals (b));/True---the Equals method of the integer type compares the value pair pair = new pair (1, 2);//First Creation mode LOG.E ("tag", pair.first.toString ());//1 LOG.E ("tag", pair.second.toString ());//2 LOG.E ("tag", Pair.equals ("1") + "");//False LOG.E ("tag", pair.equals (1) + "");//False LOG.E ("tag", "------------------- -----"); Pair Pair2 = pair.create ("1", 2);//The second creation mode LOG.E ("TAG", Pair2.first.equals ("1") + "");/True---The Equals method of string type The comparison is the content log.e ("tag", pair2.first.equals (1) + "");//False LOG.E ("tag", Pair2.second.equals (2) + "");//true- --the Equals method of the integer type compares the numeric LOG.E ("TAG", pair.equals (1) +"");//False LOG.E ("tag", Pair.equals ("1") + "");//False LOG.E ("tag", "------------------------"); LOG.E ("tag", Pair.equals (PAIR2) + "");//False LOG.E ("tag", pair.equals (pair) + "");//True}}
First look at the first way to create, we can get to the creation of the pair object to pass in the parameter value. The parameter of the Equals method of the pair object must be the pair type to be compared in the next step, otherwise it returns false directly. We can see in the source of the Equals method of the pair that the final call is the Equals method of the objects class, and look at the source code:
/** * Null-safe equivalent of {@code a.equals (b)}. * /public static Boolean equals (object A, object B) { return (a = = null)? (b = = null): A.equals (b); }
We see that the final call is the Equals method of the parameter type you passed in.
Based on the above analysis, let's look at the second way of creating. First let's look at the source code for the Equals method of the String class and the integer class:
Stirng class: Comparing the contents of a string
@Override public boolean equals (Object) {if (other = = this) {return true; } if (other instanceof string) {String s = (string) other; int count = This.count; if (s.count! = count) {return false; }//Todo:we want to avoid many boundchecks in the loop below//for long Strings until we have arr ay equality intrinsic. Bad benchmarks just push. Equals without first getting A//Hashcode hits (unlike real world use in a Hashtab Le). Filter//out these long strings here. When we get the array equality//intrinsic then remove this with the hashcode. if (hashcode () = S.hashcode ()) {return false; } for (int i = 0; i < count; ++i) {if (CharAt (i)! = S.charat (i)) {return False }} return true; } else { return false; } }
Integer class: The comparison is numeric
@Override public boolean equals (Object o) { return (o instanceof integer) && (((Integer) o). Value = = value);}
So we're looking back at Pair.equals (PAIR2), the two parameter types in the pair are all integer types, the first one in Pair1 is the string type, the second is the integer type, according to the findings of our analysis above, The comparison of the first parameter equals returns FALSE, and the second parameter, the integer, returns True, but false && true eventually returns false.
Finally, look at the generics used when the pair was created, it is not possible to use eight basic data types, but to use their wrapper types. Of course you can use reference types, a simple class that is interesting.
Use of Pair