Java 中的元组(不可修改的异构元素有序列表)支持
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13977236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tuple (unmodifiable ordered list of heterogeneous elements) support in Java
提问by Oleksandr Karaberov
I wonder why Java doesn't have a tuple data structure implementation in its standard library. For instance C++ has a very good implementationof this fixed-size collection of heterogeneous values. The same in Haskell. In Java I only know javatuplesand some support in Functional Javalibrary via Product
(P1 - P8
) types. I wonder why tuple
or at least pair
not in standard library at all? Even Android SDK developers added their own implementationof 2-tuple (pair).
我想知道为什么 Java 在其标准库中没有元组数据结构实现。例如,C++很好地实现了这种固定大小的异构值集合。在Haskell 中也是如此。在 Java 中,我只知道javatuples和通过( ) 类型对Functional Java库的一些支持。我想知道为什么或至少根本不在标准库中?甚至 Android SDK 开发人员也添加了他们自己的 2 元组(对)实现。Product
P1 - P8
tuple
pair
采纳答案by yshavit
The "Java way" is to define use-specific classes rather than these sorts of lightweight semi-class types. If you think about it, a tuple is really just a simplified struct; the Java folks would prefer you to just go ahead and create the struct.
“Java 方式”是定义特定于用途的类,而不是这些轻量级的半类类型。仔细想想,元组实际上只是一个简化的结构;Java 人员更希望您继续创建结构体。
This perspective is changing a bit, especially in Java 8 with its lambdas (which put pressure on the JDK to provide generic Function
-type interfaces rather than use-case-specific interfaces like FooCallback
). But it's still a fairly strong mindset for a lot of Java developers, and there's some sense to it. Java is a very statically typed language; a tuple is somewhere between static typing and dynamic typing, in that there's nothing in the type system to prevent you from thinking this (int, String)
that represents a customer ID and name is actually an (int, String)
representing an order ID and its description.
这种观点正在发生一些变化,尤其是在 Java 8 及其 lambda 表达式中(这对 JDK 施加了压力,需要提供泛型Function
类型的接口,而不是像 那样的用例特定的接口FooCallback
)。但是对于很多 Java 开发人员来说,它仍然是一种相当强烈的心态,并且有一定的意义。Java 是一种非常静态类型的语言;元组介于静态类型和动态类型之间,因为类型系统中没有任何内容可以阻止您认为这(int, String)
表示客户 ID 和名称实际上是(int, String)
表示订单 ID 及其描述。
See, for instance, this discussion("Tuples for n >= 2") on the issue within the Guava project. Granted, that's not official; but it's a good representation of the Java mindset.
例如,请参阅有关Guava 项目中该问题的讨论(“n >= 2 的元组”)。当然,这不是官方的。但它很好地体现了 Java 的心态。
回答by Display Name
There is a nice library called javatuples
. It defines generic tuple types for arities from 1(Unit
) to 10(Decade
) and all essential methods like equals
, hashCode
, toString
and even compareTo
.
有一个不错的库,名为javatuples
. 它为从 1( Unit
) 到 10( Decade
) 的元组以及所有基本方法(如equals
、hashCode
、toString
甚至)定义了通用元组类型compareTo
。
Official site: http://www.javatuples.org/
Maven dependency:
官方网站:http: //www.javatuples.org/
Maven 依赖:
<dependency>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
<version>[version]</version>
<scope>compile</scope>
</dependency>
(at the moment latest version is 1.2
)
(目前最新版本是1.2
)
回答by Adam Arold
It has:
它有:
Collections.unmodifiableList(yourList)
will do the trick.
会做的伎俩。
I think that the versatility of the JCF makes the existence of explicit tuple datastructures unnecessary.
我认为 JCF 的多功能性使得显式元组数据结构的存在变得不必要。
I assume that by tupleyou mean an ordered immutable list of elements (according to the wikipedia definition).
我假设元组是指有序的不可变元素列表(根据维基百科定义)。
As for the 3rd party library my power search on google yielded this, thisand of course this.
回答by Peter Verhas
There is a type in the Java runtime and it is called Entry. This is an interface inside Map and there are simple implementation of it inside AbstractMap.
Java 运行时中有一种类型,称为 Entry。这是 Map 内部的一个接口,并且在 AbstractMap 内部有它的简单实现。
http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html
http://docs.oracle.com/javase/7/docs/api/java/util/AbstractMap.SimpleEntry.html
http://docs.oracle.com/javase/7/docs/api/java/util/AbstractMap.SimpleEntry.html
You can use it as a tuple for two members, you can even apply generics. I would think hard what I did wrong if ever I needed more elements than two. Probably worth it own class then.
您可以将其用作两个成员的元组,甚至可以应用泛型。如果我需要两个以上的元素,我会认真思考我做错了什么。那么可能值得拥有自己的课程。
回答by Tom G
A colleague and I were discussing this a few weeks ago. The only solution I could come up with was to internally store the values in a Map<Class, Object>
and have some accessor method getValue(Class)
. Otherwise, how do you access the values in your tuple? You can't have a generic Tuple
class with methods for each member, e.g. getInteger
, getString
, etc because those methods would not be known until runtime, when you create the tuple. This also means you could never have two members with the same type -- how would you be able to write such a class so that at runtime, it knew which object to retrieve?
几周前,我和一位同事正在讨论这个问题。我能想出的唯一解决方案是将值内部存储在 a 中Map<Class, Object>
并有一些访问器方法getValue(Class)
。否则,您如何访问元组中的值?你不能有一个通用的Tuple
为每个成员方法,如类getInteger
,getString
等等,因为这些方法不会直到运行时知道,当您创建的元组。这也意味着你永远不可能有两个相同类型的成员——你如何能够编写这样一个类,以便在运行时知道要检索哪个对象?
回答by John B
FunctionalJava has provided a set of P
(Product) classes that I believe meet this idea. It provides P1
- P8
to allow for up to 8 element Tuples. I agree with Guava's explantation of why they are discouraged but there you go.
FunctionalJava 提供了一组P
(产品)类,我相信它们符合这个想法。它提供P1
-P8
允许最多 8 个元素元组。我同意 Guava 关于为什么他们不鼓励的解释,但你去了。