Java 16 includes Record Classes:
public record Pair<K, V>(K key, V value)
{
// intentionally empty
}
Creates a class with final fields, constructor, methods to retrieve key & value, and overrides of equals, hashCode and toString.
Usage example:
var one = new Pair<Integer, String>(1, "one");
var two = new Pair(2, "two");
System.out.println(one); // Pair[key=1, value=one]
if (!one.equals(two))
System.out.printf("not same as Pair for %d%n", two.key());
not sure what is best, if available (java 16) I would use that
JLS 8.10
JEP 395
tuples - Best way to implement a Pair class in Java - Stack Overflow
Is there a pair class in Java?
pairs in java
Why doesn't Java have tuples?
I personally prefer to read code where the class (and what it holds) has been written clearly and there is no ambiguation.
Return a user: Tuple<String,String,String,String> user = new Tuple<>(name, phone, address, nickname);
This is extremely, extremely ugly. A User class is much more appropriate and conveys the meaning of each param (what does getFirst mean with the tuple? What if i make the tuple new Tuple<>(phone, name....)? What does get(0) would mean also?).
C++11 has std::tuple now with variadic templates, and it proved useful every now and then, but one has to be very careful with it (well ... one has to be careful with c++ in general).
Given the maturity of the average Java programmer that i meet (IRL or interwebs), I would strongly oppose adding such a feature. It can be written (trivial), but it should not be provided out of the box (imho).
More on reddit.com