3. Pair对于返回值是很有效的,但是如果要作为映射中的关键要素就有些勉为其难。 copyright dedecms
4. 拥有用于调试或其他toString()用途的Pair字符串代表形式是非常好的。
5. 最后,允许Pair和包含的对象可被序列化,其内容也被假定为可以序列化了。
copyright dedecms
因此,让我们看看它是如何改变Pair执行的:
织梦内容管理系统
| public final class Pair< A,B> implements Serializable {
private static final long serialVersionUID = 1L; // shouldn't 内容来自dedecms // need to change 内容来自dedecms
public final A first; copyright dedecms public final B second; 本文来自织梦
private Pair (A first, B second) { 织梦好,好织梦 this.first = first; 本文来自织梦 this.second = second; dedecms.com } 本文来自织梦
public static < A,B> Pair< A,B> of (A first, B second) { copyright dedecms return new Pair< A,B>(first,second); 内容来自dedecms
} 织梦好,好织梦
@Override 本文来自织梦 public boolean equals(object obj) { dedecms.com if (obj == null) { copyright dedecms return false; 本文来自织梦
} 织梦好,好织梦 if (getClass() != obj.getClass()) { 内容来自dedecms return false; 本文来自织梦 } 本文来自织梦
final Pair other = (Pair) obj; 内容来自dedecms if (this.first != other.first && dedecms.com (this.first == null || !this.first.equals(other.first))) { dedecms.com return false; 本文来自织梦 } 织梦内容管理系统 if (this.second != other.second && 本文来自织梦 (this.second == null || !this.second.equals(other.second))) { 本文来自织梦 return false; 织梦好,好织梦 } copyright dedecms return true; copyright dedecms
} copyright dedecms @Override 织梦好,好织梦 public int hashCode() { 织梦内容管理系统 int hash = 7; dedecms.com hash = 37 * hash + (this.first != null ? 织梦好,好织梦 this.first.hashCode() : 0); 织梦好,好织梦 hash = 37 * hash + (this.second != null ? this.second.hashCode() : 0); dedecms.com return hash; copyright dedecms } 内容来自dedecms @Override 内容来自dedecms
public String toString () { 织梦内容管理系统 return String.format("Pair[%s,%s]", first,second); 织梦好,好织梦 } 本文来自织梦 } 织梦好,好织梦 |








