Quick revision
The short answer
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.
What to remember
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.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
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.
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().
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.
| Option | Names | Type-safe | Immutable | Cost |
|---|---|---|---|---|
record MinMax | min(), max() | yes | yes | one line of code |
Map.entry | getKey(), getValue() | yes | yes, rejects nulls | none, JDK type |
| int[] or Object[] | a[0], a[1] | int[] yes, Object[] no | no | none |
| Commons Pair, Vavr Tuple2 | getLeft(), _1 | yes | depends | extra dependency |
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.
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.
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.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
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/