Tell the container where you can put such values:

  • key1 value1
  • key1 value2
  • key1 value3
  • key2 value4
  • key2 value5
  • key3 value6

That is, so that one key can store many elements in it.

  • one
    And what map did not fit: Map<KeyType, List<Value>> ? - jmu

3 answers 3

There is such a thing in Google Guava, MultiMap

    There is no multimap in the standard Java library (which is in STL C ++). The article describes how you can do this yourself.

      Well, you can, for example, do this - implement a class with the key and value fields. Further, just in any necessary (or convenient) container you push objects of this class.