Java Pair <T,N>类实现

yan*_*isf 43 java

是否有可靠的Java Pair类实现?

我的意思是随时可用,广泛接受和测试,可能是更广泛的库的一部分,如Apache Commons或Guava.

aio*_*obe 34

是的,看看Apache Commons Pair.

如果有的话,谨慎使用; leftright没有真正传达关于元素之间的内容或关系的任何信息.

(Pair该类被故意排除在标准Java API之外.)

  • "`left`和`right`并没有真正传达任何关于内容的内容"Pair实现Map.Entry,所以你至少也可以称它们为'key`和`value`. (2认同)
  • 呵呵,好点.但是,除非你真的将它用作地图条目(在这种情况下我不明白为什么你不使用Map.Entry)`key` /`value`可能不会很好地描述内容. (2认同)
  • 为什么它被故意遗漏:http://mail.openjdk.java.net/pipermail/core-libs-dev/2010-March/003995.html (2认同)

Sim*_* G. 12

Map.Entry

java.util.Map.Entry界面怎么样?

Java 6及更高版本捆绑的两个具体实现:


pom*_*tee 8

以下是Android SDK的实现:

/**
 * 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.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
     */
    @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)


gav*_*koa 7

当需要存储对(如大小和对象集合)时,我使用了AbstractMap.SimpleEntryAbstractMap.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));
}
Run Code Online (Sandbox Code Playgroud)

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


lam*_*que 5

JavaFX将其作为javafx.util.Pair

http://docs.oracle.com/javafx/2/api/javafx/util/Pair.html

如果在Java SDK中包含jfxrt.jar,则可以使用它。