Java曾经有过Pair类吗?

Ale*_*exx 99 java

我记得不正确,或曾经做过Java,曾经提供过Pair类作为其API的一部分?

Jos*_*Lee 84

标准框架中没有Pair,但Apache Commons Lang非常接近"标准",它有一.

  • 这个答案是不完整的(而且是错误的),但不幸的是截至今天看到的排名最高。直到 JDK 10(以及从 Javafx 2.0 开始),它都有 Pair 类 https://docs.oracle.com/javase/10/docs/api/javafx/util/Pair.html。但它在 javafx.util 包中而不是 java.util 中,这让很多人感到困惑。在 JDK 11 中,JavaFX 作为非核心模块与 JDK 分离,因此之后无法找到(是的,令人惊讶的)Pair 类。https://www.infoworld.com/article/3305073/removed-from-jdk-11-javafx-11-arrives-as-a-standalone-module.html (4认同)
  • Pair <String,String> pair = new ImmutablePair <String,String>(key,value); (3认同)
  • 如果您正在为Android开发..它也有`Pair` (3认同)
  • 看看@ gavenkoa的答案如下.`java.util.Map.Entry`可能是一个简单的解决方案. (3认同)
  • 甚至更简单:Pair <String,String> pair = Pair.of(key,value); (2认同)

gav*_*koa 60

Map.Entry

Java 1.6和upper有两个Map.Entry接口实现,将一个键与一个值配对:

从Map.Entry接口继承的SimpleEntry和SimpleImmutableEntry类的UML图

例如

Map.Entry < Month, Boolean > pair = 
    new AbstractMap.SimpleImmutableEntry <>( 
        Month.AUGUST , 
        Boolean.TRUE 
    )
;
Run Code Online (Sandbox Code Playgroud)

pair.toString():AUGUST = true

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

这件作品来自我的生产代码:

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));
}
Run Code Online (Sandbox Code Playgroud)

代码看起来很复杂,但不是Map.Entry,而是限制为对象数组(大小为2)并丢失类型检查...


Bas*_*lew 35

对类:

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

用法:

Pair<Integer, String> pair = Pair.createPair(1, "test");
pair.getElement0();
pair.getElement1();
Run Code Online (Sandbox Code Playgroud)

永恒,只有一对!

  • 如果您已经使用过Android项目(基于java),那么您可能已经使用了他们的[Pair Class](http://developer.android.com/reference/android/util/Pair.html) (2认同)

pom*_*tee 16

这里有很多实现,但总是缺少一些东西,覆盖等于和哈希方法.

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

/**
 * 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 它看起来像是来自AOSP的复制/粘贴(为什么不添加信用或参考?).为了使它成为一个完整的版本,至少用实际实现替换Objects#equal方法. (4认同)

Tho*_*mas 15

应该有所帮助.

总结一下:泛型Pair类没有任何特殊的语义,你也可以需要一个Tripplet类等.因此,Java的开发人员不包含泛型Pair但建议编写特殊的类(这并不难)喜欢Point(x,y),Range(start, end)Map.Entry(key, value).


Way*_*ett 12

不,但已被多次要求.


lam*_*que 6

没有,但 JavaFX 有。

比照。Stack Overflow:Java Pair 类实现


Dil*_*nga 5

许多第三方库都有它们的Pair版本,但Java从未有过这样的类.最接近的是内部接口java.util.Map.Entry,它公开了一个不可变的键属性和一个可能的可变值属性.