Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicates in LinkedHashMap

In my code I am using a set of interleaved LinkedHashMaps inside each other as below. The code is fine and gives me the result I want except it automatically removes the duplicates. I couldnt find out how I can use TreeMap or Set in order to keep the duplicates.

LinkedHashMap<String, LinkedHashMap<Integer, LinkedHashMap<String, Vector<String>>>> 
dataAll =new LinkedHashMap<String, LinkedHashMap<Integer, LinkedHashMap<String, 
Vector<String>>>>();
like image 899
Ramin Avatar asked Nov 08 '13 16:11

Ramin


Video Answer


1 Answers

LinkedHashMap is still a Map data structure. It maps a unique key to a value. If you assign two different values to a key the second value will simply replace the first value assigned to that key.

Also imagine why do you need a Map of duplicated key? The sole purpose of Map is to provide a one to one relationship between key/value pair. It does not handle one to many relationship.

If you have to map a key with a list of values, use something like:

LinkedHashMap<String, List<..>>

This allows you to have one key maps to a list of values.

like image 118
KKKCoder Avatar answered Sep 19 '22 16:09

KKKCoder