I'd like to create new item that similarly to Util.Map.Entry
i.e. that will already contain the structure key,value
The problem is that I can't use Map.Entry itself because apperently it's a read-only object that I can't instanciate new instance of it.
Does anyone know how to create new generic key/value object?
views:
138answers:
4There's public static class AbstractMap.SimpleEntry<K,V>
. Don't let the Abstract
part of the name mislead you: it is in fact NOT an abstract
class (but its top-level AbstractMap
is).
The fact that it's a static
nested class means that you DON'T need an enclosing AbstractMap
instance to instantiate it, so something like this compiles fine:
Map.Entry<String,Integer> entry =
new AbstractMap.SimpleEntry<String, Integer>("antichrist", 666);
As noted in another answer, Guava also has a convenient static
factory method Maps.immutableEntry
that you can use.
You said:
I can't use
Map.Entry
itself because apparently it's a read-only object that I can't instantiate newinstanceof
That's not entirely accurate. The reason why you can't instantiate it directly (i.e. with new
) is because it's an interface Map.Entry
.
Caveat and tip
As noted in the documentation, AbstractMap.SimpleEntry
is @since 1.6
, so if you're stuck to 5.0, then it's not available to you.
To look for another known class that implements Map.Entry
, you can in fact go directly to the javadoc. From the Java 6 version
Interface Map.Entry
All Known Implementing Classes:
Unfortunately the 1.5 version does not list any known implementing class that you can use, so you may have be stuck with implementing your own.
Try Maps.immutableEntry from Guava
This has the advantage of being compatible with Java 5 (unlike AbstractMap.SimpleEntry
which requires Java 6.)
You can just implement the Map.Entry<K, V>
interface yourself:
import java.util.Map;
final class MyEntry<K, V> implements Map.Entry<K, V> {
private final K key;
private V value;
public MyEntry(K key, V value) {
this.key = key;
this.value = value;
}
@Override
public K getKey() {
return key;
}
@Override
public V getValue() {
return value;
}
@Override
public V setValue(V value) {
V old = this.value;
this.value = value;
return old;
}
}
And then use it:
Map.Entry<String, Object> entry = new MyEntry<String, Object>("Hello", 123);
I defined a generic Pair class that I use all the time. It's great. As a bonus, by defining a static factory method (Pair.create) I only have to write the type arguments half as often.
public class Pair<A, B> {
private A component1;
private B component2;
public Pair() {
super();
}
public Pair(A component1, B component2) {
this.component1 = component1;
this.component2 = component2;
}
public A fst() {
return component1;
}
public void setComponent1(A component1) {
this.component1 = component1;
}
public B snd() {
return component2;
}
public void setComponent2(B component2) {
this.component2 = component2;
}
@Override
public String toString() {
return "<" + component1 + "," + component2 + ">";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((component1 == null) ? 0 : component1.hashCode());
result = prime * result
+ ((component2 == null) ? 0 : component2.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Pair<?, ?> other = (Pair<?, ?>) obj;
if (component1 == null) {
if (other.component1 != null)
return false;
} else if (!component1.equals(other.component1))
return false;
if (component2 == null) {
if (other.component2 != null)
return false;
} else if (!component2.equals(other.component2))
return false;
return true;
}
public static <A, B> Pair<A, B> create(A component1, B component2) {
return new Pair<A, B>(component1, component2);
}
}