-2

Hello everyone I am a beginner with Java.

I have created a multimap with below data and I want to sort the multimap in a specific way.

Key                                    Values 

Basket1                               apple_green
                                      mango_red
Basket2                               orange_orange
                                      mango_green
basket100                             apple_red
                                      orange_green

I want to sort the above multimap in depending on the similarities in the values. The string in values can be any thing, so the code must be able to find the similarity and group them togather. something like this.

basket1                             apple_green
basket100                           apple_red
basket1                             mango_red
basket2                             mango_green
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Jayesh
  • 11
  • 1

3 Answers3

2

Your exact sorting requirements are unclear, but it is apparent that you want to sort primarily by the value. As others have noted, this is not what a Map (or Multimap) is designed for, since they are key-oriented data structures.

However, you can get access to the key-value pairs using Multimap.entries():

Collection<Map.Entry<KeyType, ValueType>> entries = multimap.entries();

(KeyType and ValueType are just type variables, because you've not specified what they are in your question - maybe they are simply String)

You can then sort these using a Comparator<Map.Entry<KeyType, ValueType>>, or, since you are using Guava, an Ordering<Map.Entry<KeyType, ValueType>>:

List<Map.Entry<KeyType, ValueType>> sortedEntries =
    new Ordering<Map.Entry<KeyType, ValueType>>() {
      @Override public int compare(
          Map.Entry<KeyType, ValueType> left,
          Map.Entry<KeyType, ValueType> right) {
        // You might want to tweak the logic here to make the
        // comparison more stable in the face of equal values.
        return left.getValue().compareTo(right.getValue());
      }
    }.sortedCopy(entries);
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

Map is a key-based storage. Values do not and must not influence the way search in the map works. If you just need to ensure iteration order then use a vector of key->value pairs and sort them as you want. There is no other way to implement what you want efficiently.

dzidzitop
  • 385
  • 2
  • 11
0

As mentioned a Map can not be sorted in alphabetical order. Even if you put the entries into it in the ordering you want to have there is no guarantee that it will be stored in the map in this ordering.

Anyway, one way to sort the entries and iterate over them an the way you want is the following :

HashMap<String, List<String>> map = new HashMap<String, List<String>>();

   for(List<String> list : map.values()) {
       Collections.sort(list);
   }

   SortedSet<String> keys = new TreeSet<String>(map.keySet());
   for (String key : keys) { 
      System.out.println(key + " -> " + map.get(key));
   }

The first 3 lines sort the content of the buckets, the following lines sort the buckets themself.

krestano
  • 136
  • 3