Pair Class in Java Since Java 8, Java provides a Pair class to store the values in key pair combinations. To store the value in pair combination, we need to use the parameterized constructor provided by the javafx. util. Pair class.
To sum it up: a generic Pair class doesn't have any special semantics and you could as well need a Tripplet class etc. The developers of Java thus didn't include a generic Pair but suggest to write special classes (which isn't that hard) like Point(x,y) , Range(start, end) or Map.
A mutable pair consists of two object elements. MutablePair is defined in the Apache Commons Lang. The package has to be added to the classpath before MutablePair is used. If Maven is used, add the following in the dependencies section of pom.
String toString() : This method will return the String representation of the Pair. K getKey() : It returns key for the pair. V getValue() : It returns value for the pair.
There is no Pair in the standard framework, but the Apache Commons Lang, which comes quite close to “standard”, has a Pair.
Map.Entry
Java 1.6 and upper have two implementation of Map.Entry
interface pairing a key with a value:
AbstractMap.SimpleEntry
AbstractMap.SimpleImmutableEntry
For example
Map.Entry < Month, Boolean > pair =
new AbstractMap.SimpleImmutableEntry <>(
Month.AUGUST ,
Boolean.TRUE
)
;
pair.toString(): AUGUST=true
I use it when need to store pairs (like size and object collection).
This piece from my production code:
public Map<L1Risk, Map.Entry<int[], Map<L2Risk, Map.Entry<int[], Map<L3Risk, List<Event>>>>>>
getEventTable(RiskClassifier classifier) {
Map<L1Risk, Map.Entry<int[], Map<L2Risk, Map.Entry<int[], Map<L3Risk, List<Event>>>>>> l1s = new HashMap<>();
Map<L2Risk, Map.Entry<int[], Map<L3Risk, List<Event>>>> l2s = new HashMap<>();
Map<L3Risk, List<Event>> l3s = new HashMap<>();
List<Event> events = new ArrayList<>();
...
map.put(l3s, events);
map.put(l2s, new AbstractMap.SimpleImmutableEntry<>(l3Size, l3s));
map.put(l1s, new AbstractMap.SimpleImmutableEntry<>(l2Size, l2s));
}
Code looks complicated but instead of Map.Entry you limited to array of object (with size 2) and lose type checks...
A Pair class :
public class Pair<K, V> {
private final K element0;
private final V element1;
public static <K, V> Pair<K, V> createPair(K element0, V element1) {
return new Pair<K, V>(element0, element1);
}
public Pair(K element0, V element1) {
this.element0 = element0;
this.element1 = element1;
}
public K getElement0() {
return element0;
}
public V getElement1() {
return element1;
}
}
usage :
Pair<Integer, String> pair = Pair.createPair(1, "test");
pair.getElement0();
pair.getElement1();
Immutable, only a pair !
There are lots of implementation around here, but all the time something is missing , the Override of equal and hash method.
here is a more complete version of this class:
/**
* Container to ease passing around a tuple of two objects. This object provides a sensible
* implementation of equals(), returning true if equals() is true on each of the contained
* objects.
*/
public class Pair<F, S> {
public final F first;
public final S second;
/**
* Constructor for a Pair.
*
* @param first the first object in the Pair
* @param second the second object in the pair
*/
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
/**
* Checks the two objects for equality by delegating to their respective
* {@link Object#equals(Object)} methods.
*
* @param o the {@link Pair} to which this one is to be checked for equality
* @return true if the underlying objects of the Pair are both considered
* equal
*/
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) {
return false;
}
Pair<?, ?> p = (Pair<?, ?>) o;
return Objects.equals(p.first, first) && Objects.equals(p.second, second);
}
/**
* Compute a hash code using the hash codes of the underlying objects
*
* @return a hashcode of the Pair
*/
@Override
public int hashCode() {
return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
}
/**
* Convenience method for creating an appropriately typed pair.
* @param a the first object in the Pair
* @param b the second object in the pair
* @return a Pair that is templatized with the types of a and b
*/
public static <A, B> Pair <A, B> create(A a, B b) {
return new Pair<A, B>(a, b);
}
}
This should help.
To sum it up: a generic Pair
class doesn't have any special semantics and you could as well need a Tripplet
class etc. The developers of Java thus didn't include a generic Pair
but suggest to write special classes (which isn't that hard) like Point(x,y)
, Range(start, end)
or Map.Entry(key, value)
.
No, but it's been requested many times.
Many 3rd party libraries have their versions of Pair, but Java has never had such a class. The closest is the inner interface java.util.Map.Entry, which exposes an immutable key property and a possibly mutable value property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With