在Java中使用Pairs或2元组 [英] Using Pairs or 2-tuples in Java
本文介绍了在Java中使用Pairs或2元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Java中的Hashtable将受益于具有元组结构的值。我可以在Java中使用哪种数据结构?
My Hashtable in Java would benefit from a value having a tuple structure. What data structure can I use in Java to do that?
Hashtable<Long, Tuple<Set<Long>,Set<Long>>> table = ...
推荐答案
我不认为那里是Java中的通用元组类,但自定义元组可能如下所示:
I don't think there is a general purpose tuple class in Java but a custom one might be as easy as the following:
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;
}
}
当然,有一些重要的含义如何进一步设计这个类关于相等性,不变性等,特别是如果你打算使用实例作为散列的关键。
Of course, there are some important implications of how to design this class further regarding equality, immutability, etc., especially if you plan to use instances as keys for hashing.
这篇关于在Java中使用Pairs或2元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文