什么是C ++ Pair< L,R>的等价物。在Java? [英] What is the equivalent of the C++ Pair<L,R> in Java?
问题描述
Java中有没有 Pair< L,R>
这是否有充分的理由?这个C ++构造的等价物是什么?我宁愿避免重新实现自己。
Is there a good reason why there is no Pair<L,R>
in Java? What would be the equivalent of this C++ construct? I would rather avoid reimplementing my own.
似乎 1.6 提供类似的东西( AbstractMap.SimpleEntry< K ,V>
),但这看起来很复杂。
It seems that 1.6 is providing something similar (AbstractMap.SimpleEntry<K,V>
), but this looks quite convoluted.
推荐答案
在 上的线程comp.lang.java.help
,Hunter Gratzner对Java中存在 Pair
结构提出了一些论点。主要的论点是类 Pair
没有传达关于两个值之间关系的任何语义(你怎么知道first和second是什么意思?) 。
In a thread on comp.lang.java.help
, Hunter Gratzner gives some arguments against the presence of a Pair
construct in Java. The main argument is that a class Pair
doesn't convey any semantics about the relationship between the two values (how do you know what "first" and "second" mean ?).
更好的做法是为每个应用程序编写一个非常简单的类,如迈克建议的类, Pair
class。 Map.Entry
是一个在其名称中带有其含义的货币对的示例。
A better practice is to write a very simple class, like the one Mike proposed, for each application you would have made of the Pair
class. Map.Entry
is an example of a pair that carry its meaning in its name.
总结一下,我认为最好有一个位置(x,y)
,一个类范围(开始,结束)
和一个类条目(键,值)
而不是一般的对(第一,第二)
关于它应该做什么的任何事情。
To sum up, in my opinion it is better to have a class Position(x,y)
, a class Range(begin,end)
and a class Entry(key,value)
rather than a generic Pair(first,second)
that doesn't tell me anything about what it's supposed to do.
这篇关于什么是C ++ Pair< L,R>的等价物。在Java?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!