我的Java哈希表将受益于具有元组结构的值。我可以在Java中使用什么数据结构来做到这一点?

Hashtable<Long, Tuple<Set<Long>,Set<Long>>> table = ...

当前回答

为了补充@maerics的答案,下面是Comparable元组:

import java.util.*;

/**
 * A tuple of two classes that implement Comparable
 */
public class ComparableTuple<X extends Comparable<? super X>, Y extends Comparable<? super Y>>
       extends Tuple<X, Y>
       implements Comparable<ComparableTuple<X, Y>>
{
  public ComparableTuple(X x, Y y) {
    super(x, y);
  }

  /**
   * Implements lexicographic order
   */
  public int compareTo(ComparableTuple<X, Y> other) {
    int d = this.x.compareTo(other.x);
    if (d == 0)
      return this.y.compareTo(other.y);
    return d;
  }
}

其他回答

如果您正在寻找一个内置的Java双元素元组,请尝试AbstractMap.SimpleEntry。

Apache Commons提供了一些常见的java实用程序,包括Pair。它实现了Map。条目,可比较和序列化。

另外2美分:从Java 7开始,现在在标准Lib中有一个类:javafx.util.Pair。

是的,它是标准Java,现在JavaFx包含在JDK中:)

你可以使用谷歌番石榴表

我认为Java中没有通用的元组类,但自定义的元组类可能像下面这样简单:

public class Tuple<X, Y> { 
  public final X x; 
  public final Y y; 
  public Tuple(X x, Y y) { 
    this.x = x; 
    this.y = y; 
  } 
} 

当然,关于如何进一步设计这个类的相等性、不可变性等,有一些重要的含义,特别是如果您计划使用实例作为哈希的键。