File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Creating a TreeMap with duplicate key values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Creating a TreeMap with duplicate key values" Watch "Creating a TreeMap with duplicate key values" New topic
Author

Creating a TreeMap with duplicate key values

David Laverdy
Greenhorn

Joined: Aug 14, 2011
Posts: 18



The output is
555
444
333
444
555

How do I go about in the right direction getting the proper output for each Key ?
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

You are more likely to get the help you need if you post code that compiles. I got 4 errors when I tried compiling this.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

David Laverdy wrote:How do I go about in the right direction getting the proper output for each Key ?


Impossible to say, since you haven't stated what you expect the output to be, or what the rules are you want to follow to produce that output, what it means to have "aaa" associated with both "111" and "555".

Note, though, that a Map by definition allows only one value per key.

The most common approach to what you seem to be asking would be to create a Map<String, List<String>>, but until you TellTheDetails(←click), that's just a guess.

David Laverdy
Greenhorn

Joined: Aug 14, 2011
Posts: 18
I apologize for being a little vague, new to posting here. The output I should expect is
aaa has the values 111 555
bbb has the values 222 444
ccc has the value 333
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Then, yes, Map<String, List<String>> is the way to go, at least for a first pass.

Is there any particular reason you're using Strings "111", "222", etc., rather than ints 111, 222? Or are those just examples to distinguish keys from values?
 
 
subject: Creating a TreeMap with duplicate key values