I like how Java has a Map where you can define the types of each entry in the map, for example <String, Integer>
.
What I'm looking for is a type of collection where each element in the collection is a pair of values. Each value in the pair can have its own type (like the String and Integer example above), which is defined at declaration time.
The collection will maintain its given order and will not treat one of the values as a unique key (as in a map).
Essentially I want to be able to define an ARRAY of type <String,Integer>
or any other 2 types.
I realize that I can make a class with nothing but the 2 variables in it, but that seems overly verbose.
I also realize that I could use a 2D array, but because of the different types I need to use, I'd have to make them arrays of OBJECT, and then I'd have to cast all the time.
I only need to store pairs in the collection, so I only need two values per entry. Does something like this exist without going the class route? Thanks!
I mean, even though there is no
Pair
class in Java there is something pretty simmilar:Map.Entry
Map.Entry Documentation
This is (simplifying quite a bit) what
HashMap
, or actually anyMap
stores.You can create an instance of
Map
store your values in it and get the entry set. You will end up with aSet<Map.Entry<K,V>>
which effectively is what you want.So:
Apache common lang3 has Pair class and few other libraries mentioned in this thread What is the equivalent of the C++ Pair<L,R> in Java?
Example matching the requirement from your original question:
AbstractMap.SimpleEntry
Easy you are looking for this:
How can you fill it?
This simplifies to:
And, with the help of a
createEntry
method, can further reduce the verbosity to:Since
ArrayList
isn't final, it can be subclassed to expose anof
method (and the aforementionedcreateEntry
method), resulting in the syntactically terse:Java 9+
In Java 9, you can simply write:
Map.entry(key, value)
to create an immutable pair.Note: this method does not allow keys or values to be null. If you want to allow null values, for example, you'd want to change this to:
Map.entry(key, Optional.ofNullable(value))
.Java 8+
In Java 8, you can use the more general-purpose
javafx.util.Pair
to create an immutable, serializable pair. This class does allow null keys and null values. (In Java 9, this class is included in thejavafx.base
module). EDIT: As of Java 11, JavaFX has been decoupled from the JDK, so you'd need the additional maven artifact org.openjfx:javafx-base.Java 6+
In Java 6 and up, you can use the more verbose
AbstractMap.SimpleImmutableEntry
for an immutable pair, orAbstractMap.SimpleEntry
for a pair whose value can be changed. These classes also allow null keys and null values, and are serializable.Android
If you're writing for Android, just use
Pair.create(key, value)
to create an immutable pair.Apache Commons
Apache Commons Lang
provides the helpfulPair.of(key, value)
to create an immutable, comparable, serializable pair.Eclipse Collections
If you're using pairs that contain primitives, Eclipse Collections provides some very efficient primitive pair classes that will avoid all the inefficient auto-boxing and auto-unboxing.
For instance, you could use
PrimitiveTuples.pair(int, int)
to create anIntIntPair
, orPrimitiveTuples.pair(float, long)
to create aFloatLongPair
.Project Lombok
Using Project Lombok, you can create an immutable pair class simply by writing:
Lombok will fill in the constructor, getters,
equals()
,hashCode()
, andtoString()
methods for you automatically in the generated bytecode. If you want a static factory method instead of a constructor, e.g., aPair.of(k, v)
, simply change the annotation to:@Value(staticConstructor = "of")
.Otherwise
If none of the above solutions float your boat, you can simply copy and paste the following code (which, unlike the class listed in the accepted answer, guards against NullPointerExceptions):
This is based on JavaHelp4u 's code.
Less verbose and shows how to do in one line and how to loop over things.
What about com.sun.tools.javac.util.Pair?