UPDATE: This answer is in response to the original question, Does Java SE 8 have pairs or tuples? (And implicitly, if not, why not?) The OP has updated the question with a more complete example, but it looks like it can be solved without using any kind of Pair structure. [Nota da OP: qui l’altra risposta corretta .]
The short answer is no. You need to roll over or insert one of the many libraries that implement it.
Have a class
Pair in Java SE it has been proposed and rejected at least once. You see this discussion thread on one of the OpenJDK mailing lists. The trade-offs aren’t obvious. On the one hand, there are many implementations of Pair in other libraries and in application code. This demonstrates a need, and adding a Java SE-like class will increase reuse and sharing. On the other hand, having a Pair class adds to the temptation to create complicated data structures from pairs and collections without creating the necessary types and abstractions. (This is a paraphrase of message from Kevin Bourillion from that thread.)
I recommend everyone to read the whole discussion via email. It is extraordinarily intuitive and has no influence. It is quite convincing. When it started I thought, “Yes, there should be a Pair class in Java SE” but when the thread reached the end I changed my mind.
However, note that JavaFX has the class javafx.util.Pair . The JavaFX APIs have evolved separately from the Java SE APIs.
As can be seen from the linked question What is the equivalent of the C ++ pair in Java? there is enough design space surrounding what is apparently such a simple API. Should objects be immutable? Should they be serializable? Should they be comparable? Should the class be final or not? Should the two elements be ordered? Should it be an interface or a class? Why stop at couples? Why not triple, quadriceps or N-tuples?
And of course there is the inevitable mention of the number of bikes for the elements:
- (a, b)
- (first second)
- (left right)
- (car, cdr)
- (foo, bar)
A big problem that has just been mentioned is the relationship between couples and primitives. If you have a data
(int x, int y) representing a point in 2D space, representing this as
Pair consumes three objects instead of two 32-bit words. Additionally, these objects must reside on the heap and incur GC overhead.
It would seem clear that, like the Streams, it would be essential that primitive specializations exist for couples. We want to see:
Pair ObjIntPair ObjLongPair ObjDoublePair IntObjPair IntIntPair IntLongPair IntDoublePair LongObjPair LongIntPair LongLongPair LongDoublePair DoubleObjPair DoubleIntPair DoubleLongPair DoubleDoublePair
IntIntPair would still require an object in the heap.
These are, of course, reminiscent of the proliferation of functional interfaces in the package
Java.util.function in Java SE 8. If you don’t want a bloated API, which ones could you leave out? It could also be argued that this is not enough and that specializations for, for example,
My feeling is that if Java had added a Pair class a long time ago, it would have been straightforward, or even simplistic, and would not have satisfied many of the use cases we are envisioning now. Consider that if the pair had been added in the time frame of JDK 1.0, it probably would have been mutable! (Look at Java.util.Date.) Would people have been happy with that? My guess is that if there was a Pair class in Java, it would be kinda-sort-not-really-useful and each would keep running their own to suit their needs, there would be various Pair and Tuple implementations in external libraries, and the people would keep arguing / discussing how to fix Java’s Pair class. In other words, like in the same place we are in today.
In the meantime, work is being done to solve the fundamental problem, which is better support in the JVM (and ultimately in the Java language) for types of value . See this status of values document. This is preliminary, speculative work, and it only covers issues from the JVM’s point of view, but it already has a fair amount of thinking behind it. Obviously there are no guarantees that this will happen on Java 9, or that it can get anywhere, but it does show the current direction of thinking on this topic.