3

I've created an "Attribut" class which is just a wrapper for a key/value single item. I know that Maps and HashMaps are designed for lists of this kind of items so I feel like i reinvented the wheel... Is there some Class which fulfill this purpose ?

Regards

( My code to be clear about what i'm looking for )

public class Attribut {
    private int id;
    private String value;
    @Override
    public String toString() {
        return value;
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getValue() {
        return value;
    }
    public void setValue(String value) {
        this.value = value;
    }
}
7

5 Answers 5

2

You can reuse Map.Entry<K, V>:

http://docs.oracle.com/javase/6/docs/api/java/util/Map.Entry.html

In your case it'd be Map.Entry<Integer, String>.

3
  • Unfortunately, that's just an interface.
    – Thilo
    Dec 9, 2011 at 10:27
  • 2
    But there are two implementations SimpleEntry and SimpleImmutableEntry since JDK6.
    – Thilo
    Dec 9, 2011 at 10:28
  • @Thilo: That's cool! I hadn't realized that those had been made public. Dec 9, 2011 at 10:36
2

HashMap !

example :

    Map<Integer,String> attribut = new HashMap<Integer, String>();
    attribut.put(1, "hi");
    String value = attribut.get(1);

you can iterate :

    for (Integer key : attribut.keySet()) {
        value = attribut.get(key);
    }

EDIT :

OK, just for a Pair !

public class Pair<K, V> {

    private final K element0;
    private final V element1;

    public static <K, V> Pair<K, V> createPair(K key, V value) {
        return new Pair<K, V>(key, value);
    }

    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 !

2
  • 1
    Why create an entire Map just to hold a pair of values?
    – duffymo
    Dec 9, 2011 at 10:36
  • That seems ok but as I just needed an int / string pair, I'll stay with my custom class. Good generic code though :)
    – chamel
    Dec 9, 2011 at 11:29
1

You can use AbstractMap.SimpleEntry. There is also a SimpleImmutableEntry.

However, I believe that it is not wrong designing your own type. There is a plethora of examples in the JDK itself where something like this (tuple) has been done:

I believe that it's a good thing, since you're code is more easily readable and you gain additional type safety.

1

You're not "reinventing the wheel", you just specifying your requirements. You want a class that constitutes a mutable int/String pair, and so your code is OK.

Your problem is that Java syntax is overly verbose. It would be nice to simply define it as something like

class IdValuePair(id: int, value: String)

but that's something for other languages.

0

You could use [Collections.singletonMap()](http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#singletonMap(K, V)).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.