patternjavaCritical
What is the equivalent of the C++ Pair<L,R> in Java?
Viewed 0 times
pairjavatheequivalentwhat
Problem
Is there a good reason why there is no
It seems that 1.6 is providing something similar (
Pair in Java? What would be the equivalent of this C++ construct? I would rather avoid reimplementing my own.It seems that 1.6 is providing something similar (
AbstractMap.SimpleEntry), but this looks quite convoluted.Solution
In a thread on
A better practice is to write a very simple class, like the one Mike proposed, for each application you would have made of the
To sum up, in my opinion it is better to have a class
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 ?).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.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.Context
Stack Overflow Q#156275, score: 425
Revisions (0)
No revisions yet.