今天学习一个细小的知识点,它就是Pair,首先让我们看一下它简短的源码:
public class Pair<F, S> {
public final F first;
public final S second;
/**
* Constructor for a Pair.
*
* @param first the 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 two objects for equality by delegating to their respective
* {@link Object#equals(Object)} methods.
*
* @param o the {@link Pair} to which this one is to be checked for equality
* @return true if the underlying objects of the Pair are both considered
* equal
* 这里需要注意一下:如果此处传进来的参数不是Pair类型,就直接返回false。
* 如果参数是Pair类型,在近一步比较。下面会近一步讲解。
*/
@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 Pair
* ^ 按位异或 若参加运算的两个二进制位值相同,则为0,不同否则为1
*/
@Override
public int hashCode() {
return (first == null ? 0 : first.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 that is templatized 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);
}
}
学习一个知识点最好的方法就是实践,下面让我们看看Pair是怎么使用的吧!!!请看下面的代码:
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 --- Integer类型的equals方法比较的是数值
Pair pair = new Pair(1, 2);//第一种创建方式
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);//第二种创建方式
Log.e("TAG", pair2.first.equals("1") + "");// true --- String类型的equals方法比较的是内容
Log.e("TAG", pair2.first.equals(1) + "");// false
Log.e("TAG", pair2.second.equals(2) + "");// true --- Integer类型的equals方法比较的是数值
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
}
}
首先看一下第一种创建方式,我们可以通过创建的Pair对象获取到创建的时候传进去的参数值。同时Pair对象的equals方法的参数必须是Pair类型才能近一步比较,否则直接返回false。我们在Pair的equals方法的源码中可以看到最终调用的是Objects类的equals方法,看一下源码:
/**
* Null-safe equivalent of {@code a.equals(b)}.
*/
public static boolean equals(Object a, Object b) {
return (a == null) ? (b == null) : a.equals(b);
}
我们看到最终调用的是你传递进来的参数类型的equals方法。
根据上面的分析我们看一下第二种创建方式。首先先让我们看一下String类和Integer类的equals方法的源码:
Stirng类:比较的是字符串的内容
@Override
public boolean equals(Object other) {
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 array equality intrinsic.
// Bad benchmarks just push .equals without first getting a
// hashCode hit (unlike real world use in a Hashtable). Filter
// out these long strings here. When we get the array equality
// intrinsic then remove this use of 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类:比较的是数值
@Override
public boolean equals(Object o) {
return (o instanceof Integer) && (((Integer) o).value == value);
}
所以我们在回头看看pair.equals(pair2),pair中的两个参数类型都是Integer类型, pair1中第一个是String类型,第二个是Integer类型,根据我们上面分析的发现,第一个参数equals的比较返回false,第二个参数Integer的比较返回true,但是 false && true 最终返回的是false。
最后看看Pair创建时候使用的泛型,这里不能使用八大基本数据类型,而应该使用它们的包装类型。当然可以使用引用类型,很有意思的一个简单类。