Ed B
Ed B

Reputation: 23

Java Multimap of naturally ordered keys but collections sorted in the order the elements were added

Is there an easy way to implement a Multimap that is like a TreeMultimap in the sense that the keys take a natural ordering, but also like an ArrayListMultimap in the sense that the collections are sorted in the order the values were added?

Upvotes: 2

Views: 177

Answers (1)

Grzegorz Rożniecki
Grzegorz Rożniecki

Reputation: 28005

You can use MultimapBuilder to create combinations of features you need. From javadoc:

A builder for a multimap implementation that allows customization of the backing map and value collection implementations used in a particular multimap.

This can be used to easily configure multimap data structure implementations not provided explicitly in com.google.common.collect, for example:

ListMultimap<String, Integer> treeListMultimap =
      MultimapBuilder.treeKeys().arrayListValues().build();
SetMultimap<Integer, MyEnum> hashEnumMultimap =
     MultimapBuilder.hashKeys().enumSetValues(MyEnum.class).build();

Upvotes: 4

Related Questions