7

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>>>>();
Ramin
  • 891
  • 2
  • 10
  • 16
  • 2
    Duplicate key, value or combination? – Lukas Eichler Nov 08 '13 at 16:39
  • 4
    `Set` doesn't allow duplicate elements, and `Map` replaces values for duplicate keys. That how they are, so why not try using an `ArrayList` instead? – Rahul Nov 08 '13 at 16:39
  • 22
    Oh my God, what a maintenance nightmare you're creating there. Use custom classes instead of maps of maps of lists of maps... – JB Nizet Nov 08 '13 at 16:41
  • possible duplicate of [Map implementation with duplicate keys](http://stackoverflow.com/questions/1062960/map-implementation-with-duplicate-keys) – Mena Nov 08 '13 at 16:41
  • Templar and RJ got it right. In maps you cannot have duplicate keys, in sets you cannot have duplicates. – Pete B. Nov 08 '13 at 16:43
  • @JBNizet if you are going to make comments like that, you will have to do so on about 95% of the coding problems on here. – Pete B. Nov 08 '13 at 16:44
  • Why do you want to do this? – Markus Koivisto Nov 08 '13 at 16:45
  • I am reading from a large SPSS file and trying to merge certain rows; so I need to basically have two LinkedHashMap inside each other. I do realize maybe defining classes and objects would be better but not that fast and easy writing it for processing 3 million rows in a spss file – Ramin Nov 08 '13 at 16:48

1 Answers1

9

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.

KKKCoder
  • 903
  • 9
  • 18
  • my problem is not with the keys and key duplicates (as I dont even want them). when storing values to internal linkedhashmap I dont want the duplicates to be removed – Ramin Nov 08 '13 at 16:50
  • 1
    Which one, you got 2 internal maps...? – Jonathan Drapeau Nov 08 '13 at 16:52
  • Sorry, when you say "you don't want duplicates to be removed", do you mean "you don't want duplicated keys to be removed"? Or you mean "you don't want duplicated values to be removed". – KKKCoder Nov 08 '13 at 16:55
  • I dont want duplicate values to be removed – Ramin Nov 08 '13 at 18:20
  • There should not be a restriction for duplicate values binded to different keys. Maybe something else is wrong? Can you provide code for the duplicated values? – Tigerware Jun 03 '19 at 02:01