Java Pair<T,N> class implementation [closed]
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 7 years ago.
Improve this questionIs there ava开发者_运维知识库ilable a proven Java Pair class implementation?
I mean readily available, widely accepted and tested, maybe part of a more extensive library such as Apache Commons or Guava.
Yes, have a look at Apache Commons Pair
.
Use sparingly, if at all; left
and right
doesn't really convey anything about the content or relation between the elements.
(The Pair
class was deliberately left out of the standard Java API.)
Map.Entry
What about java.util.Map.Entry
interface?
Two concrete implementation bundled with Java 6 and later:
java.util.AbstractMap.SimpleEntry
java.util.AbstractMap.SimpleImmutableEntry
Here is an implementation from the Android SDK:
/**
* 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);
}
}
I used AbstractMap.SimpleEntry and AbstractMap.SimpleImmutableEntry 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...
JavaFX has it as javafx.util.Pair
.
http://docs.oracle.com/javafx/2/api/javafx/util/Pair.html
If you include jfxrt.jar in Java SDK you can use it.
My solution was:
public class Pair<F, S> extends java.util.AbstractMap.SimpleImmutableEntry<F, S> {
public Pair( F f, S s ) {
super( f, s );
}
public F getFirst() {
return getKey();
}
public S getSecond() {
return getValue();
}
public String toString() {
return "["+getKey()+","+getValue()+"]";
}
}
Very simple, with all the benefits of the wrapped AbstractMap.SimpleImmutableEntry class.
精彩评论