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.

Community
  • 1
  • 1
codingenious
  • 8,385
  • 12
  • 60
  • 90

2 Answers2

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

senseiwu
  • 5,001
  • 5
  • 26
  • 47
  • I want to rely on order of keys. Edited question. – codingenious Apr 08 '14 at 03:50
  • 1
    then probably you need to use [LinkedListMultiMap](http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/collect/LinkedListMultimap.html). Also see [this discussion](http://stackoverflow.com/questions/14975681/how-is-arraylistmultimap-different-from-linkedlistmultimap). Though I haven't tested these myself. – senseiwu Apr 08 '14 at 05:31
  • This is what was required. :) – codingenious Apr 08 '14 at 11:04
  • glad that it helped :-) would you then consider choosing it as the correct answer? cheers! – senseiwu Apr 08 '14 at 18:05
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>>.

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66
  • It's like, 1 key having multiple values, this is not required. I actually want multiple entries of keys. – codingenious Apr 03 '14 at 15:38
  • 1
    @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) ... – ajb Apr 03 '14 at 15:42
  • @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`. – IProblemFactory Apr 03 '14 at 15:45
  • I want to rely on order of keys. Edited question. – codingenious Apr 08 '14 at 03:51