这个问题在这里已有答案:
Java中的Hashtable将受益于具有元组结构的值。我可以在Java中使用哪种数据结构来做到这一点?
Hashtable<Long, Tuple<Set<Long>,Set<Long>>> table = ...
我不认为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;
}
}
当然,如何在平等性,不变性等方面进一步设计这个类有一些重要的含义,特别是如果你打算使用实例作为散列的键。
创建一个类,描述您实际建模和使用它的概念。它可以存储两个Set<Long>
并为它们提供访问器,但它应该被命名以指示这些集合中的每个集合是什么以及为什么它们被组合在一起。
为了补充@ 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;
}
}
虽然这篇文章已经很老了,虽然我明白我并不是很有帮助,但我认为在这里完成的工作:http://www.pds.ewi.tudelft.nl/pubs/papers/cpe2005.pdf,在主流Java中会很好。
你可以这样做:
int a;
char b;
float c;
[a,b,c] = [3,'a',2.33];
要么
[int,int,char] x = [1,2,'a'];
要么
public [int,boolean] Find(int i)
{
int idx = FindInArray(A,i);
return [idx,idx>=0];
}
[idx, found] = Find(7);
这里的元组是:
这种方法增加了
你可以使用Google Guava Table
我将从关于Java中元组的一般观点开始,并以对您具体问题的含义完成。
1)在Java中避免使用非通用语言中的元组的方式,因为它们不是类型安全的(例如在Python中:tuple = (4, 7.9, 'python')
)。如果你仍然想使用像通用元组(不推荐)这样的东西,你应该使用Object[]
或List<Object>
并在用instanceof
检查后投射元素以确保类型安全。
通常,某个设置中的元组总是以相同的方式使用,包含相同的结构。在Java中,您必须在class
中明确定义此结构,以提供定义良好的类型安全值和方法。这看起来很烦人且不必要,但在编译时已经防止了错误。
2)如果你需要一个包含相同(超级)类Foo
的元组,请使用Foo[]
,List<Foo>
或List<? extends Foo>
(或列表的不可变对应物)。由于元组不是定义的长度,因此该解决方案是等效的。
3)在你的情况下,你似乎需要一个Pair
(即一个明确定义的长度为2的元组)。这使得maerics的答案或其中一个补充答案最有效,因为您可以在将来重用代码。
javatuples是Java中元组的专用项目。
Unit<A> (1 element)
Pair<A,B> (2 elements)
Triplet<A,B,C> (3 elements)
Apache Commons提供了一些常见的java实用程序,包括Pair。它实现了Map.Entry
,Comparable
和Serializable
。
如果您正在寻找内置的Java双元素元组,请尝试使用AbstractMap.SimpleEntry
。
作为@maerics很好的答案的扩展,我添加了一些有用的方法:
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;
}
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if (!(other instanceof Tuple)){
return false;
}
Tuple<X,Y> other_ = (Tuple<X,Y>) other;
// this may cause NPE if nulls are valid values for x or y. The logic may be improved to handle nulls properly, if needed.
return other_.x.equals(this.x) && other_.y.equals(this.y);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((x == null) ? 0 : x.hashCode());
result = prime * result + ((y == null) ? 0 : y.hashCode());
return result;
}
}
另外2美分:从Java 7开始,现在标准Lib中有一个类:javafx.util.Pair。
是的,它是标准的Java,现在JavaFx包含在JDK中:)
这是其他地方完全相同的问题,包括更强大的equals
,hash
,maerics暗示:
那个讨论继续反映了maerics和ColinD的方法“我应该重新使用一个具有非特定名称的类元组,或者在每次遇到这种情况时创建一个具有特定名称的新类”。多年前我在后一个营地;我已经发展成为支持前者。
此对象提供equals()的合理实现,如果equals()在每个包含的对象上为true,则返回true。