Piotr0123456
Piotr0123456

Reputation: 648

Multimap with duplicated keys ordered by order with which they appear in the given multimap

I want to create multimap with keys as a Strings and values as a Sets

Multimap<String, Set<String>> = ???

when I put keys, and values in this way:

multimap.put("e", set1);
multimap.put("x", set2);
multimap.put("a", set3);
multimap.put("m", set4);
multimap.put("p", set5);
multimap.put("l", set6);
multimap.put("e", set7);

I want to receive exactly this same order with this same sets, so it means:

"e" -> set1
"x" -> set2
"a" -> set3
"m" -> set4
"p" -> set5
"l" -> set6
"e" -> set7

I'm new inn guava, so could anybody write how to implement this Multimap to set duplicated keys in it and receive values in this order?

Upvotes: 1

Views: 1432

Answers (2)

SORTED MULTIMAP WITH PUBLIC NON-PARAMETRIC MAPPINGS The multimap abstract data type, a container of key-value associations where

dublicate keys are allowed, is defined with the following interface

public interface SortedMultimapek extends Comparable<K,5 [ /**Returns a value to which the specifted key is napped.

@param key the key whose associated value is to be returned.

@return a value to which the specifled key is mapped, or null if this sorted multimap contains no mapping for the key.

*V find(K key):

/**Returns true if this sorted multimap contains no key-value mappings.

*/ boolean LsEmpty();

@return true if this sorted multimap contains no key-value mappings.

/**Associates the specified value with the specified key in this sorted multimap. @param key the key with which the specifled value is to be associated.

@param value the value to be associated with the specified key. @throw java.lang. IllegalArgumentException if the specified key or value is null.

*/ void insert(K key, V value);

/**Removes a mapping for a key from this sorted multimap if it is present. @param key the key whose mapping is to be removed from this sorted multimap. @return the previous value associated with key, or null if there was no mapping for key.

*/V remove(K key);

/**Returns the number of key-value mappings in this sorted multimap.

*/ int size();

@return the number of key-value mappings in this sorted multimap.

/**Returns a sorted array view of the keys contained in this sorted multimap. @return an array view of the sorted keys contained in this sorted multimap where keys are sorted in ascending order according to their natural order.

*/ Object[] sortedKeys();

where K and V are the parametric data types for the key and value. The class to be used for the mappings is the following public class

public class Entry {

// instance variables

/**Constructs a new mapping with the specified key and value.

private Object key, value;

@param k the specified key of this mapping.

@param v the specified value of this mapping.

*/ public Entry(Object k, Object v) { setKey(k); setValue(v); }

/**Returns the key of this mapping.

@return the key of this mapping.

*/ public Object getKey() { return key; }

/**Returns the value of this mapping.

@return the value of this mapping.

*/ public Object getValue() { return value; }

Sets the key of this mapping with the specified key. /

@param k the specified key.

*/ public void setKey(Object k) (key = k; }

Upvotes: 0

Louis Wasserman
Louis Wasserman

Reputation: 198163

LinkedHashMultimap.entries() preserves the exact order the entries were added to the multimap.

Upvotes: 4

Related Questions