Scrivio Interview library
CodingFoundations7 min read · updated 12 November 2026

Java has no general Pair or Tuple class in the JDK. A method must return both the minimum and maximum of a list. Show three ways to return two values and say which one you would choose in Java 25.

Java gives us several ways to return two values: a small record such as record MinMax(int min, int max), a Map.Entry built with Map.entry(min, max), or a two-element array. In Java 25 we would choose the record, because it gives the two values real names and type safety for one line of code, and for plain ints IntSummaryStatistics already does the whole job.

Why this question is asked

Returning more than one value from a method is something we do every week: a minimum and a maximum, a result and a warning, a page of rows and a total count. Languages such as Python and Kotlin have tuples or Pair built in, so people coming from them often ask why Java has nothing similar. Interviewers ask this question because the answer shows whether we know modern Java (records, local records) and whether we care about code that the next reader can understand. In this answer, we will recap why Java has no Pair, write the same min and max method three ways, compare them, and then see which one we would pick and why.

1Why Java Has No General Pair Class

A tuple is an unnamed group of values, such as (3, 9). A pair is a tuple of exactly two values. The JDK never added a general Pair class, and the reason is a design choice rather than an oversight: a Pair<Integer, Integer> tells the reader nothing about what the two numbers mean. Is the first one the minimum, the count, or an index? The type system cannot help, and every caller has to remember the order.

Java's answer has always been to write a small class with named fields. Before Java 16 that meant a constructor, two getters, equals(), hashCode() and toString(), about 30 lines for two numbers, so people reached for shortcuts. Records, final since Java 16, removed that cost. A record is a class whose whole purpose is to carry a fixed set of values, and the compiler writes the boring parts for us.

2Option 1: Return a Record

Here is the method written with a record. The record declares two components, min and max, and the compiler generates a constructor, accessor methods min() and max(), and equals(), hashCode() and toString() that use both components.

Java
record MinMax(int min, int max) {}
static MinMax minMax(List<Integer> values) {
if (values.isEmpty()) throw new IllegalArgumentException("empty list");
int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
for (int v : values) {
if (v < min) min = v;
if (v > max) max = v;
}
return new MinMax(min, max);
}
MinMax r = minMax(List.of(4, 9, 1, 7));
System.out.println(r.min()); // 1
System.out.println(r.max()); // 9
System.out.println(r); // MinMax[min=1, max=9]
System.out.println(r.equals(new MinMax(1, 9))); // true

Notice how the caller reads: r.min() and r.max() say exactly what they hold. The record is immutable, because its fields are final, so nobody can change the result after the method returns. If the type is only useful inside one method, we can even declare it as a local record, written inside the method body. A local record is implicitly static, so it cannot capture local variables, but it keeps a helper type from leaking into the rest of the class.

3Option 2: Return a Map.Entry

The JDK does contain one ready-made pair: Map.Entry, the type a map uses for one key and its value. Since Java 9 we can create one with Map.entry(key, value). The older AbstractMap.SimpleEntry also works and is mutable through setValue().

Java
static Map.Entry<Integer, Integer> minMaxEntry(List<Integer> values) {
int min = Collections.min(values);
int max = Collections.max(values);
return Map.entry(min, max);
}
var e = minMaxEntry(List.of(4, 9, 1, 7));
System.out.println(e.getKey()); // 1 (is "key" the minimum? the reader must guess)
System.out.println(e.getValue()); // 9
Map.entry(null, 9); // NullPointerException: Map.entry rejects nulls
new AbstractMap.SimpleEntry<>(null, 9); // allowed, and mutable

It works without any new type, but the names are wrong for our data. A minimum is not a key and a maximum is not a value, so every caller writes getKey() and has to remember that it means min. Map.entry also rejects null keys and values with a NullPointerException, which can surprise us if the data can contain nulls.

4Option 3: Return an Array, or a Library Pair

The cheapest option in machine terms is an array of length two.

Java
static int[] minMaxArray(List<Integer> values) {
return new int[] { Collections.min(values), Collections.max(values) };
}
int[] a = minMaxArray(List.of(4, 9, 1, 7));
System.out.println(a[0] + " " + a[1]); // 1 9
System.out.println(a.length); // 2, but nothing stops a caller from reading a[2]
Three ways to return two values
OptionNamesType-safeImmutableCost
record MinMaxmin(), max()yesyesone line of code
Map.entrygetKey(), getValue()yesyes, rejects nullsnone, JDK type
int[] or Object[]a[0], a[1]int[] yes, Object[] nononone
Commons Pair, Vavr Tuple2getLeft(), _1yesdependsextra dependency
Only the record gives the values names that explain the data.

An int[] is fast and needs no new type, but it is unclear: index 0 and index 1 carry no names, the length is not checked by the compiler, and the array is mutable, so a caller can overwrite a[0]. An Object[] is worse still, because it also loses the types and forces casts. Library pair types, such as Pair in Apache Commons Lang or Tuple2 in Vavr, are type-safe, but they add a dependency to the project and still offer meaningless names like getLeft() and _1. They solve the typing problem without solving the readability problem.

5Which One We Choose in Java 25

In modern Java we choose the record. It costs one line, it documents the result, it is immutable, and its equals() and hashCode() are correct without any effort. If later we need a third value, such as an average, we add a component and the compiler shows every place that must change.

For this particular task there is an even shorter answer, and it is worth mentioning in an interview. The JDK already has a class that computes the minimum and maximum of ints in one pass.

Java
IntSummaryStatistics stats = List.of(4, 9, 1, 7).stream()
.mapToInt(Integer::intValue)
.summaryStatistics();
System.out.println(stats.getMin()); // 1
System.out.println(stats.getMax()); // 9
System.out.println(stats.getAverage()); // 5.25
System.out.println(new IntSummaryStatistics().getMin()); // 2147483647 for an empty input

So the full answer is: use IntSummaryStatistics when the values are numbers and a ready-made result fits, and a record whenever we design our own result type. Notice the last line, though: for an empty input getMin() returns Integer.MAX_VALUE rather than failing, so we still check getCount() before trusting the result.

6Follow-up questions

Can a record be used as a key in a HashMap?

Yes, and it is one of the best uses of records. A record's equals() and hashCode() are generated from all of its components, so two records with equal components are equal and have the same hash code, which is exactly what HashMap needs. The one condition is that the components must not change while the record is in the map. A record of ints and Strings is safe; a record holding a mutable List is not, because changing the list changes the hash code and the map can no longer find the entry.

Why not just use a Pair from a library everyone already depends on?

Because the dependency was never the main problem. A Pair<Integer, Integer> still hides the meaning of its two values, and that cost is paid by every reader for the life of the code. Since records exist, a named type costs one line, so the convenience argument for Pair is gone. Generic pairs still make sense in truly generic code, such as a utility that zips two lists.

7Conclusion

In this answer, we saw why the JDK has no Pair: a pair hides what its values mean. We wrote a min and max method three ways, with a record, with Map.entry and with an array, and compared them on names, type safety and immutability. The record wins in Java 25 because it is a one-line named, immutable type with correct equals() and hashCode(), and for plain ints IntSummaryStatistics already returns the minimum and the maximum together.

What to say in the interviewAbout 90 seconds aloud

Open with

Java has no general Pair or Tuple on purpose, because a pair says nothing about what its values mean. To return a minimum and a maximum, I see three options.

Then, if they want more

The first is a record, such as record MinMax(int min, int max). It is one line, the compiler generates the constructor, the accessors, equals, hashCode and toString, it is immutable, and the caller reads result.min() and result.max(), which is self-explanatory. If the type is only needed in one method I can declare it as a local record.

The second option is Map.entry(min, max). It needs no new type, but getKey and getValue say nothing about min and max, and Map.entry rejects nulls. AbstractMap.SimpleEntry allows nulls but is mutable. The third option is an int array of length two.

It is cheap, but index zero and index one have no names, the length is not checked, and the array can be changed by the caller. Library pairs from Commons Lang or Vavr add a dependency and still have names like left and right. In Java 25 I would choose the record every time I design my own result.

And for this specific case I would mention IntSummaryStatistics, which gives min, max, count and average of ints in one pass, with the caveat that an empty input returns Integer.MAX_VALUE as the minimum, so I check the count first.

About this answer

Last updated
12 November 2026
Versions it names
Java 16, Java 25, Java 9
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Java has no general Pair or Tuple class in the JDK. A method must return both the...": https://scrivio-interview-library.netlify.app/java/collections-overview/b34307d56d/