【问题标题】:Java Pair<T,N> class implementation [closed]Java Pair<T,N> 类实现
【发布时间】:2011-11-11 02:35:20
【问题描述】:

是否有经过验证的 Java Pair 类实现?

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

【问题讨论】:

标签: java


【解决方案1】:

是的,看看 Apache Commons Pair

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

Pair 类被故意排除在标准 Java API 之外。)

【讨论】:

  • 自测试版(您链接到的)以来,他们似乎将其拆分为 MutablePair 和 ImmutablePair,但:commons.apache.org/lang/api/org/apache/commons/lang3/tuple/…
  • "leftright 并没有真正传达有关内容的任何内容" Pair 实现了 Map.Entry,因此您至少也可以将它们称为 keyvalue
  • 呵呵,说得好。但是,除非您真的将其用作地图条目(在这种情况下,我不明白您为什么不使用 Map.Entry)key/value 可能无论如何都不能很好地描述内容。
  • @AndreaBergonzo 的评论应该在有人在任何地方请求对或元组时链接。
【解决方案2】:

Map.Entry

java.util.Map.Entry 接口呢?

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

【讨论】:

    【解决方案3】:

    这是来自 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.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);
        }
    }
    

    【讨论】:

      【解决方案4】:

      当需要存储对(如大小和对象集合)时,我使用了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));
      }
      

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

      【讨论】:

        【解决方案5】:

        JavaFX 将其命名为 javafx.util.Pair

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

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

        【讨论】:

        • 如果你使用的是 openJDK,javafx 是不可以的
        【解决方案6】:

        我的解决方案是:

        public class Pair<F, S> extends java.util.AbstractMap.SimpleImmutableEntry<F, S> {
        
            public  Pair( F f, S s ) {
                super( f, s );
            }
        
            public F getFirst() {
                return getKey();
            }
        
            public S getSecond() {
                return getValue();
            }
        
            public String toString() {
                return "["+getKey()+","+getValue()+"]";
            }
        
        }
        

        非常简单,具有包装的 AbstractMap.SimpleImmutableEntry 类的所有优点。

        【讨论】:

        • 感谢这个简单的宝石!对我帮助很大。
        猜你喜欢
        • 1970-01-01
        • 2014-07-05
        • 1970-01-01
        • 2011-04-08
        • 2014-12-12
        • 2013-03-26
        • 2011-09-26
        • 2019-09-23
        • 1970-01-01
        相关资源
        最近更新 更多