在java中将两个long编码为另一个原语
我有一个 Tuple
对象,它包含 3 个基元:Tuple(double, long, long)
。为了避免创建大量的元组,我正在考虑使用 Trove 库的原始 MAP,它将两个原始作为键和值。就我而言,它将是 Map
。
我的问题:是否可以有效地将两个 long
编码为一个可以存储在地图中的原语,然后对它们进行解码?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
否,仅仅因为 long 是 64 位,并且没有 Java 原语比这更长。您需要一个 128 位原语来将两个长整型编码到其中。
No, simply because longs are 64-bit, and no Java primitive is longer than that. You would need a 128-bit primitive to encode two longs into it.
没错,您不能将两个 64 位原语打包到另一个原语中,而另一个原语的大小最多为 64 位。按照标准,
double
和long
均由 64 个二进制数字映射。问题是,您是否可以对您正在处理的号码施加一些限制。如果您知道,您将始终拥有偶数数字或非偶数数字,或者第一个组件将具有整数范围,或者您正在处理 1000 的众多,您可以在这里赢得一些比特。
实际上,您永远不会利用所有
长值对的
。另一方面,处理值对上的映射也没什么大不了的。这就是让 Java 这样的面向对象语言不仅能够处理 C 中的 struct 等数据类型,而且能够将方法绑定到数据上的全部努力。
您可以在网络中找到 Pair 类的良好实现,例如 angelikalanger.com。或者您可以轻松地自己编写一个实现,特别是因为您只需要一对
Long
值。还可以考虑使用
Pair>
或立即实现一个Tuple
类而不是 Map,即键值组合,遵循以下轮廓配对
实现。最后,您甚至可以使用 内存数据库,例如 H2 来保存您的
元组(double, long, long)
条目。将其作为 Java 库包含在您的项目中并正确配置它就足够了。顺便说一句,3元组称为三元组。因此,您可以正确地调用您的
类 Triple(double, long, long)
或更好的Triple(Double, Long, Long)
。It's right, you cannot pack two 64-bit primitives into another primitive, which is at most 64 bits of size. Both,
double
andlong
by standard are mapped by 64 binary digits.The question is, whether you can impose some restrictions on the numbers you are dealing with. If you know, you will always have even numbers or uneven numbers or the first component will have integer range or you are dealing with multitudes of 1000, you can win some bits here.
Practically speaking, you will never make use of all
of pairs of long values.
On the other hand, it's no big deal to handle maps on pairs of values. That was the whole effort to make object-oriented languages like Java to not only deal with data types like
struct
in C, but also to bind methods to the data.You can find good implementations of a Pair class in the web, e.g. angelikalanger.com. Or you can easily code an implementation yourself, especially, since you only need a pair of
Long
values.Also consider to use
Pair<Double, Pair<Long, Long>>
or implement aTuple<M,N,T>
class right away instead of a Map, i.e. key-value combination, following the outline of thePair<M,N>
implementation.Finally, you could even employ an in-memory database like H2 to hold your
Tuple(double, long, long)
entries. It is enough to enclose it in your project as a Java library and configure it properly.By the way, a 3-tuple is called a triple. Therefore, you could correctly call your
class Triple(double, long, long)
or betterTriple(Double, Long, Long)
.您可以使用 Trove 的双对象映射并将两个 long 编码为 BigInteger,但如果您的目标是严格遵守原始类型,那么这显然没有任何帮助。
You could use Trove's double-Object map and encode the two longs into a BigInteger, but if your objective is to stay strictly with primitive types, that obviously isn't any help.
正如 Joonas 所说,没有任何一个原语可以容纳 128 位。可能满足您需求的是使用数组来保存两个长整型:
Map
。虽然Double
和long[]
并不是严格意义上可能适合的原语。请记住,您不能将 double (small-d) 放入 Map 中,因为 Map 只能包含引用类型,而不能包含基元。或者,
Map(Double, Pair)
怎么样,其中Pair
是一个保存两个 long 的小类?大多数图书馆都有类似的东西。As Joonas says, there is no single primitive that will hold 128 bits. What might meet your need is to use an array to hold the two longs:
Map<Double, long[]>
. WhileDouble
andlong[]
are not strictly primitives that might suit. Remember that you cannot putdouble
(small-d) into a Map as Maps can only contain reference types, not primitives.Alternatively, how about
Map(Double, Pair)
, wherePair
is a small class to hold two longs? Most libraries have something like that lying around somewhere.