2

I have a situation where I need to have a key-value pair data structure. But it should allow multiple keys and values corresponding to that.

It would be like :

a - 1

b - 2

a - 3

So, when retrieving, I can have getFirstValueOfKey(key) and get 1...something like that.

Is there something existing or I need to implement this?

If I need to implement this, I am thinking to proceed as :

Create a class, which a=can hold K-V pairs, and add them to a list. And write corresponding API's required. Is this right approach? Shall I continue like this?

EDIT : I actually want multiple entries of keys in the data structure.

EDIT : The thing is, I want to maintain order in which new entries(keys) were made, i.e. I want to have sequence of keys how they are put, (a and then b and then a). If array of values is used, this sequence is lost.

Example :

a -> 1 (Time 0) b -> 2 (Time 1) a -> 3 (Time 2)

These time stamps are also required.

3
  • commons.apache.org/proper/commons-collections/javadocs/… should do the trick Commented Apr 3, 2014 at 15:37
  • Could you also explain why do you need multiple keys? Do you want to rely on order of keys? Commented Apr 3, 2014 at 15:47
  • @ProblemFactory - yes, I want to rely on order of keys. :) Commented Apr 8, 2014 at 3:49

2 Answers 2

6

You can have a look at MultiMap by Guava library from Google

There are two ways to think of a Multimap conceptually: as a collection of mappings from single keys to single values:

a -> 1 a -> 2 a -> 4 b -> 3 c -> 5 or as a mapping from unique keys to collections of values:

a -> [1, 2, 4] b -> 3 c -> 5

Though you have a jar dependency, Guava collections are normally more concise and efficient

Sign up to request clarification or add additional context in comments.

4 Comments

I want to rely on order of keys. Edited question.
then probably you need to use LinkedListMultiMap. Also see this discussion. Though I haven't tested these myself.
This is what was required. :)
glad that it helped :-) would you then consider choosing it as the correct answer? cheers!
2

It looks like you are looking for Map<Character, List<Integer>>.

Since you edited question: to rely both on keys order and values per each key,

  • you should use list of Map.Entry implementation, (for example Pair from Apache Commons), where: List<Map.Entry<Character, List>> struct = new ArrayList<Pair<Character, ArrayList>>();

  • or if you don't want to add additional libraries, instead of Pair you could use just Map (witch will always contains 1 key - your letter): List<Map<Character, List>>.

4 Comments

It's like, 1 key having multiple values, this is not required. I actually want multiple entries of keys.
@Batty I don't see the difference. It all depends on how you access it. If you want a getFirstValueOfKey operation, use the map get to get the list and then get the first element of the list. It is not hard to get the program to see (key, [val1, val1, val3...]) as (key, val1), (key, val2), (key, val3) ...
@Batty, I don't understand your problem, you want have multiple keys and also keep order of values, my solution solve that problem. You can't rely on keys order in Map.
I want to rely on order of keys. Edited question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.