Lasith Malinga
Lasith Malinga

Reputation: 135

What is the best approach for insert duplicated key,value pairs and get them inserted order in java

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

Answers (4)

realvalkyrie
realvalkyrie

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

Dakkaron
Dakkaron

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

Keppil
Keppil

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

Eugene
Eugene

Reputation: 120858

Based on comment I think that this is what u are looking for:

https://guava-libraries.googlecode.com/svn-history/r13/trunk/javadoc/com/google/common/collect/ArrayListMultimap.html

Upvotes: 0

Related Questions