Java中的Hashtable将受益于具有元组结构的值.我可以在Java中使用哪种数据结构来做到这一点?
Hashtable<Long, Tuple<Set<Long>,Set<Long>>> table = ...
Run Code Online (Sandbox Code Playgroud)
mae*_*ics 307
我不认为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;
}
}
Run Code Online (Sandbox Code Playgroud)
当然,如何在平等性,不变性等方面进一步设计这个类有一些重要的含义,特别是如果你打算使用实例作为散列的键.
小智 162
javatuples是Java 中元组的专用项目.
Unit<A> (1 element)
Pair<A,B> (2 elements)
Triplet<A,B,C> (3 elements)
Run Code Online (Sandbox Code Playgroud)
rhg*_*hgb 93
Apache Commons提供了一些常见的Java实用程序,包括一对.它实现Map.Entry
,Comparable
和Serializable
.
小智 56
如果您正在寻找内置的Java双元素元组,请尝试AbstractMap.SimpleEntry
.
Ara*_*yan 38
作为@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;
}
}
Run Code Online (Sandbox Code Playgroud)
Teo*_*ali 29
另外2美分:从Java 7开始,现在标准Lib中有一个类:javafx.util.Pair.
是的,它是标准的Java,现在JavaFx包含在JDK中:)
not*_*eti 17
以下是其他地方完全相同的问题,其中包括更强大的内容equals
,hash
这些内容暗示:
那个讨论继续反映了maerics和ColinD的方法"我应该重新使用一个具有非特定名称的类元组,或者在每次遇到这种情况时创建一个具有特定名称的新类".多年前我在后一个营地; 我已经发展成为支持前者.
小智 9
使用lombok可以很容易地声明一个Pair
类:
@Data(staticConstructor = "of")
public class Pair<A, B> {
private final A left;
private final B right;
}
Run Code Online (Sandbox Code Playgroud)
这将生成getter,名为"of"的静态构造函数equals()
,hashcode()
和toString()
.
@Data
有关更多信息,请参阅文档
虽然这篇文章现在已经很老了,而且我知道我并不是真的很有帮助,但我认为《向 Java 添加元组:轻量级数据结构的研究》中描述的建议在主流 Java 中会很好。
您可以执行以下操作:
int a;
char b;
float c;
[a,b,c] = [3,'a',2.33];
Run Code Online (Sandbox Code Playgroud)
或者
[int,int,char] x = [1,2,'a'];
Run Code Online (Sandbox Code Playgroud)
或者
public [int,boolean] Find(int i)
{
int idx = FindInArray(A,i);
return [idx,idx>=0];
}
[idx, found] = Find(7);
Run Code Online (Sandbox Code Playgroud)
这里的元组是:
这种方法增加了
为了补充@ 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;
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
534956 次 |
最近记录: |