没有一次Java有一个Pair类?

我是否曾经错误地记住过,或曾经做过Java,提供一个Pair类作为其API的一部分?

在标准框架中没有Pair,但是非常接近“标准”的Apache Commons Lang有一个Pair 。

Java 1.6和Upper有两个Pair( Map.Entry接口)的实现: AbstractMap.SimpleEntry和AbstractMap.SimpleImmutableEntry

我需要存储对(如大小和对象集合)时使用它。

这个从我的生产代码:

 public Map<L1Risk, Map.Entry<int[], Map<L2Risk, Map.Entry<int[], Map<L3Risk, List<Event>>>>>> getEventTable(RiskClassifier classifier) { Map<L1Risk, Map.Entry<int[], Map<L2Risk, Map.Entry<int[], Map<L3Risk, List<Event>>>>>> l1s = new HashMap<>(); Map<L2Risk, Map.Entry<int[], Map<L3Risk, List<Event>>>> l2s = new HashMap<>(); Map<L3Risk, List<Event>> l3s = new HashMap<>(); List<Event> events = new ArrayList<>(); ... map.put(l3s, events); map.put(l2s, new AbstractMap.SimpleImmutableEntry<>(l3Size, l3s)); map.put(l1s, new AbstractMap.SimpleImmutableEntry<>(l2Size, l2s)); } 

代码看起来很复杂,但不是Map.Entry ,而只限于对象数组(大小为2),并且会丢失types检查…

Pair类:

 public class Pair<K, V> { private final K element0; private final V element1; public static <K, V> Pair<K, V> createPair(K element0, V element1) { return new Pair<K, V>(element0, element1); } public Pair(K element0, V element1) { this.element0 = element0; this.element1 = element1; } public K getElement0() { return element0; } public V getElement1() { return element1; } } 

用法:

 Pair<Integer, String> pair = Pair.createPair(1, "test"); pair.getElement0(); pair.getElement1(); 

不可变的,只有一双!

这应该有所帮助。

总结一下:一个普通的Pair类没有任何特殊的语义,你也可以需要一个Tripplet类等等。因此,Java的开发者并没有包含一个通用的Pair而是build议编写特殊的类(这不是这很难)像Point(x,y)Range(start, end)Map.Entry(key, value)

在这里有很多的实现,但总是缺less一些东西,比如equals和hash方法的Override。

这里是这个类的更完整的版本:

 /** * Container to ease passing around a tuple of two objects. This object provides a sensible * implementation of equals(), returning true if equals() is true on each of the contained * objects. */ 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 */ @Override public boolean equals(Object o) { if (!(o instanceof Pair)) { return false; } Pair<?, ?> p = (Pair<?, ?>) o; return Objects.equals(p.first, first) && Objects.equals(p.second, second); } /** * Compute a hash code using the hash codes of the underlying objects * * @return a hashcode of the Pair */ @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版本,但Java从来没有这样的类。 最接近的是内部接口java.util.Map.Entry,它公开一个不可变的键属性和一个可能的可变值属性。

如果你想要一对(不是所谓的键 – 值对)只是把两个通用数据放在一起,上面的解决scheme(或所谓的Key)不能改变(既不是在Apache Commons的Lang对中也不是在AbstractMap.SimpleEntry )。 他们有自己的理由,但仍然需要能够改变这两个组件。 这里是可以设置两个元素的Pair类

 public class Pair<First, Second> { private First first; private Second second; public Pair(First first, Second second) { this.first = first; this.second = second; } public void setFirst(First first) { this.first = first; } public void setSecond(Second second) { this.second = second; } public First getFirst() { return first; } public Second getSecond() { return second; } public void set(First first, Second second) { setFirst(first); setSecond(second); } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Pair pair = (Pair) o; if (first != null ? !first.equals(pair.first) : pair.first != null) return false; if (second != null ? !second.equals(pair.second) : pair.second != null) return false; return true; } @Override public int hashCode() { int result = first != null ? first.hashCode() : 0; result = 31 * result + (second != null ? second.hashCode() : 0); return result; } } 

这看起来很奇怪。 我发现这个线程,也认为我曾经看过一个,但在Javadoc找不到它。

我可以看到Java开发人员使用专用类的观点,并且存在一个通用的Pair类可能会导致开发人员变得懒惰(消亡!

然而,以我的经验来看,无疑有时候你build模的东西实际上只是一对东西,并且为这两者之间的关系提出了一个有意义的名字,实际上比刚刚开始的时候更痛苦用它。 相反,我们只需要创build一个“定制”类的实际锅炉代码 – 可能被称为“对”。

这可能是一个滑坡,但是一个Pair和一个Triplet类将覆盖很大比例的用例。

不,但是JavaFX有它。

参看 堆栈溢出:Java Pair类的实现