File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes The best option for paired data, which has duplicate keys Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The best option for paired data, which has duplicate keys" Watch "The best option for paired data, which has duplicate keys" New topic

The best option for paired data, which has duplicate keys

Ben Synes
Ranch Hand

Joined: Jul 18, 2012
Posts: 48

I am interested in using the right container for a group of paired values, I am unsure what to use. I understand anything which may have a non unique key, it would be wise to stay away from HashMap etc.

The data is in a structure like this, and its way of structuring tracking tags on sites, some are just page source based, and some only appear after an event, so event based:

page based:
homepage; canonical
registration; canonical
faq; DF3768-U
homepage; GA-UFSHS1

event based:
signup; doubleclick
sentmaill; GA-UUA51

Initially I thought of having a two dimensional array, one to store the event based and one to store the page static based. But as the size cannot change, I was unsure.

I plan to iterate over an array dependent on its given event or page basis, drill down on the values that match a page type, say homepage, and scan through all the values for the homepage until it is matched.

I have been reading here, but is there any easy way to do this?

Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37884
If the Guava and Apache versions aren’t suitable for your purposes (they probably are), you can have a Map<Foo, List<Bar>>

Find the Map section in the Java Tutorials, and it has an example about counting words in. Now, instead of incrementing the count, you can insert a List and populate it with your Bar object. Later on, if you encounter the same List, you can add to it.
That is probably how the multi‑map works. When it says it is not a Map, that means it does not return one V if you use the get() method. That sort of thing is probably your best bet. There is something similar in Apache Commons, but the Guava version seems more versatile.
I agree. Here's the link:
subject: The best option for paired data, which has duplicate keys
Similar Threads
Implementing a Hash Table
need help with dropdown box
whats a hash map???
Best structure for non-duplicate pairs?
Sorting two ArrayLists, one dependent on the other