Reputation: 135
I want to insert duplicated key, value and want to get them in inserted order what is the best solution for this in java collections?
Upvotes: 1
Views: 202
Reputation: 21
"multimap mapping from keys to multiple values"
as Keppil mentioned LinkedListMultimap is u wanted.
as i recommended u can try ImmutableListMultimap then invoke createValues to iterator the values(immutable* collection always get better peformance)
Upvotes: 0
Reputation: 6276
Just create a helper class like this:
class KeyValuePair {
public final Foo key;
public final Bar value;
public KeyValuePair(Foo key, Bar value) {
this.key = key; this.value = value;
}
}
And add this to any list you like.
Upvotes: 0
Reputation: 46219
Any List
will retain insertion order and they all allow duplicated elements.
An ordered collection (also known as a sequence). The user of this interface has precise control over where in the list each element is inserted.
There are no key-value
structures with these properties in standard Java, but Guava's LinkedListMultiMap
might have what you are looking for.
An implementation of ListMultimap that supports deterministic iteration order for both keys and values. The iteration order is preserved across non-distinct key values.
Upvotes: 6