Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Structure (key, value) with repetition Java

Is there a java structure like HashMap but that allows to repeat the key as well as the value ?

For example:

HashMap<Integer, String> hmap = new HashMap<>();
map.put(1, "Sport");
map.put(1, "Football");
map.put(2, "Football");
map.put(1, "Handball");

And as a result:

<1,"Sport">
<1,"Football">
<2,"Football">
<3,"Handball">

Any help would be most appreciated.

Thanks, Nadhmanovic

like image 254
Nadhmanovic Avatar asked Feb 14 '26 03:02

Nadhmanovic


1 Answers

There is not a structure like this built into the language. But you can use Guava's Multimap.

like image 54
van dench Avatar answered Feb 16 '26 17:02

van dench



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!