-2

As we all know map doesn't allow duplicate values,but is there a way to add duplicate values to a map.I need to add duplicate Key and Value to a map such as

1    A
1    C
1    D
2    D
2    G.

When its put to a map it will return only 1 D and 2 G.I have tried google Multimap but having problem with iteration .Is there any possibility to solve this or what approach can be taken to allow duplicate value to a map.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255

3 Answers3

0

you can use the map but define it to hold a list of values for every key...

Map<String, List<Points>> myMap = ....

now you need to carefully append those values to every key present so you should just do myMap.put(1, A) but instead get the value by 1 and if present call value.add(A)

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
0

You do not add duplicate key values to a Map. But you can create a Map <Key, Collection <Value>> and add the values to the collection behind key.

pirho
  • 11,565
  • 12
  • 43
  • 70
-1

if you just need the Multimap for one special case you can simply create a data object like

class Multivalue

String (or Object, whatever) valueOne String valueTwo

HashMap Integer, Multivalue map = new HashMap Integer,Multivalue ()

baumfreund
  • 71
  • 5