I need some data structure that I can build from standard collections or using guava. So it should be mutable Map<Enum, V>. Where V is pretty interesting structure.
V requirements:
- mutable
- sorted by comparator (with allowing elements such as compare(a, b) == 0) – these is need for iterations
- set (there is no such
aandb, thata.equals(b) == true) – optional
extra optional requirement to map
- keys should be iterated by their natural order
now it’s HashMap<SomeEnum, LinkedList<Entity>> with different stuff like collections.sort() in the code.
Thanks.
A Sample implementation
Here is a Guava Multimap implementation of the class you need:
First the drawback: it will have to reside in package
com.google.common.collect. The makers of guava have in their infinite wisdom madeAbstractSortedSetMultimappackage scoped.I will use this
enumin all my examples:Constructing the Class
There are six constructors:
Empty (Uses a
HashMapand natural ordering for values)with a
Comparator(V)(Uses aHashMap<K,SortedSet<V>>with the supplied comparator for the values)with a
Map<K,SortedSet<V>>(use this if you want to sort keys, pass in aSortedMapimplementation)with a
Map<K,SortedSet<V>>and aComparator<V>(same as above, but values are sorted using custom comparator)with a
Class<K extends Enum<K>>(uses anEnumMapinternally for higher efficiency, natural ordering for values)with a
Class<K extends Enum<K>>and aComparator<V>(same as above, but values are sorted using custom comparator)Source Code
Here’s the class:
Other ways to do it
UPDATE: I guess the proper Guava way to do it would have been something like this (it uses the
SortedArrayListclass I wrote in my other answer):