Selecting an ordered pair of values
I am looking for an implementation of a data structure that will add / output an ordered pair of values. eg.
orderedPair.add(value1, text1)
orderedPair.add(value1, text2)
orderedPair.add(value2, text3, data1)
orderedPair.add(value2, data2)
orderedPair.add(value1, text5)
When I get it I want it to return iteratively like
value1, text1
value1, text2
value2, text3, data1 and so on.
LinkedHashMaps or any variants of HashMaps do not work as they only return a key based value and what I am trying to get are value, value pairs. Please note that neither value, text, nor data is unique and I cannot get it based on any keys. Also, I DO NOT want a sorted list, I only want an ORDERED list.
The question is: is there any data structure in Java that can be used for this?
I have not come across anything that serves this purpose. In this case, I am contemplating writing a custom collection that will accomplish this. Any suggestions / help is appreciated.
source to share
End the discussion in the comments in response as it seems useful to the OP:
Create class Tuple
which will be your pairs / threes.
Note that this class can be implemented with a fixed number of parameters or as a container that contains a list of objects.
Hold these objects Tuple
in List<Tuple>
and you're done.
You can also implement hashCode()
, equals()
and make it an implementation Comparable
for this class - and you can use it with other collections such as TreeSet
and HashSet
.
source to share
Just use a list map like Treemap
Map<Integer, List<Integer>> content = new Treemap<Integer, List<Integer>>();
if (not content.containsKey(value1)) {
content.put(value1, new LinkedList<Integer>());
}
content.get(value1).add(text1)
This will be the orderedPair.add feature
Then for output, move the map and for each entry, write out each element of the corresponding list
Since you want it to be ordered, pass the Comparator to the Treemap constructor.
source to share