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;
    }
}
chamel
  • 367
  • 4
  • 16
  • your code doesn't clarify anything. if what you want is to get `value` by id, then you're correct, `Map` would be the right way to go – jFrenetic Dec 09 '11 at 10:21
  • Why you don't want to use Map? – narek.gevorgyan Dec 09 '11 at 10:22
  • you are looking for a class which fulfills this purpose, And HashMap is a Class !! so whats really the question? – Zohaib Dec 09 '11 at 10:22
  • 2
    possible duplicate of [What is the equivalent of the C++ Pair in Java?](http://stackoverflow.com/questions/156275/what-is-the-equivalent-of-the-c-pairl-r-in-java) – Thilo Dec 09 '11 at 10:24
  • also: http://stackoverflow.com/questions/5303539/didnt-java-once-have-a-pair-class – Thilo Dec 09 '11 at 10:25
  • @Thilo : Yes, this is a duplicate but I didn't find those in my search, thank you for pointing these out. – chamel Dec 09 '11 at 10:52
  • @narek.gevorgyan, I don't want to use Map because I want a single pair, not a list of pair. – chamel Dec 09 '11 at 10:53

5 Answers5

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>.

duffymo
  • 305,152
  • 44
  • 369
  • 561
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 !

Bastiflew
  • 1,136
  • 3
  • 18
  • 31
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.

Ingo Kegel
  • 46,523
  • 10
  • 71
  • 102
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.

scravy
  • 11,904
  • 14
  • 72
  • 127
0

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

Crozin
  • 43,890
  • 13
  • 88
  • 135