-5

I have been using a HashMap which handles key / value pairs. But, now i need to handle key: value , value. Is it possible to have one key with 2 values ? can you recommend a data structure/collection or strategy for me?

  • 2
    Is this Java or C++? Anyway, you need a real object. What does the `(value, value)` mean? What does it represent? When you have answered that, you already have the name of the object to create. – Tunaki May 20 '16 at 13:43
  • If you're talking about C++, then you can use a `std::unordered_map>` – sjrowlinson May 20 '16 at 13:46
  • A Map with ArrayList as the value would work. `Map> ` – Ken Wolf May 20 '16 at 13:49
  • http://stackoverflow.com/questions/4956844/hashmap-with-multiple-values-under-the-same-key ... – Tom May 20 '16 at 15:46

2 Answers2

2

Yes you can, assuming this is c++, you can have

std::unordered_map<key, std::pair<value, value>>;

You can make the std::pair whatever type you'd like them to be.

paul-g
  • 3,797
  • 2
  • 21
  • 36
Clay Brooks
  • 182
  • 1
  • 11
  • 2
    As far as I am aware `hash_map` is not part of the c++ standard, but `std::unordered_map` is. – sjrowlinson May 20 '16 at 14:01
  • It is not, but hash_map is a part of the STL. – Clay Brooks May 20 '16 at 14:02
  • http://en.cppreference.com/w/cpp/container/unordered_map it is `unordered_map` not hash_map – paul-g May 20 '16 at 14:15
  • @ClayBrooks STL is not relevant for standard C++. It had some influence, but that's it. – deviantfan May 20 '16 at 14:16
  • You guys are correct. I only used hasmap because the poster used HashMap in his question. It was incorrect of me to do std:: before the hashmap. Its unfortunate visual studio requires `std::` to implement `hash_map`. [Is hash_map part of the STL?](http://stackoverflow.com/questions/5908581/is-hash-map-part-of-the-stl) – Clay Brooks May 20 '16 at 14:19
1

What'd make more sense to do is to make an object to hold your two values, like HashMap<KeyType, ContainerObject> map.

In container object, you can use something like a list, or your own defined object that's made to just hold whatever two values you need. This way, you can use the HashMap and just access whatever values you need through the object that holds them.

Jeremy Kato
  • 467
  • 5
  • 12