Java data structure

I need a data structure that I can create from standard collections or using guava. Therefore, it must be modified Map<Enum, V> . Where V rather interesting structure.

V :

  • mutable
  • sorted by comparator (with resolving elements such as compare (a, b) == 0) - this is the need for iterations
  • set (there are no such a and b , a.equals(b) == true ) - optional

additional optional display requirements

  • keys must be iterated in their natural order

now it has a HashMap<SomeEnum, LinkedList<Entity>> with other stuff, for example collections.sort() in the code.

Thanks.

+6
java collections guava map
source share
4 answers

Implementation example

Here is the implementation of Guava Multimap for the class you need:

First downside: it will have to be in the package com.google.common.collect . The guava creators in their infinite wisdom made AbstractSortedSetMultimap batch coverage.

I will use this enum in all my examples:

 public enum Color{ RED, BLUE, GREEN }; 

Class building

There are six constructors:

  • Empty (uses HashMap and natural ordering for values)

     SortedSetMultimap<Color,String> simple = new EnumValueSortMultiMap<Color, String>(); 
  • with Comparator(V) (uses a HashMap<K,SortedSet<V>> with the attached comparator for values)

     SortedSetMultimap<Color,String> inreverse = new EnumValueSortMultiMap<Color, String>( Ordering.natural().reverse() ); 
  • with Map<K,SortedSet<V>> (use this if you want to sort keys, pass the implementation of SortedMap )

     SortedSetMultimap<Color,String> withSortedKeys = new EnumValueSortMultiMap<Color, String>( new TreeMap<Color, Collection<String>>() ); 
  • with Map<K,SortedSet<V>> and a Comparator<V> (same as above, but values ​​are sorted using a custom comparator)

     SortedSetMultimap<Color,String> reverseWithSortedKeys = new EnumValueSortMultiMap<Color, String>( new TreeMap<Color, Collection<String>>(), Ordering.natural().reverse() ); 
  • with Class<K extends Enum<K>> (uses EnumMap internally for greater efficiency, natural order of values)

     SortedSetMultimap<Color,String> withEnumMap = new EnumValueSortMultiMap<Color, String>( Color.class ); 
  • with Class<K extends Enum<K>> and a Comparator<V> (same as above, but values ​​are sorted using a custom comparator)

     SortedSetMultimap<Color,String> reverseWithEnumMap = new EnumValueSortMultiMap<Color, String>( Color.class, Ordering.natural().reverse() ); 

Source

Here's the class:

 package com.google.common.collect; import java.util.Collection; import java.util.Comparator; import java.util.EnumMap; import java.util.HashMap; import java.util.Map; import java.util.SortedSet; import java.util.TreeSet; public class EnumValueSortMultiMap<K extends Enum<K>, V extends Comparable<? super V>> extends AbstractSortedSetMultimap<K, V>{ private static final long serialVersionUID = 5359491222446743952L; private Comparator<? super V> comparator; private Class<K> enumType; public EnumValueSortMultiMap(){ this(new HashMap<K, Collection<V>>()); } public EnumValueSortMultiMap(final Comparator<? super V> comparator){ this(new HashMap<K, Collection<V>>(), comparator); } public EnumValueSortMultiMap(final Map<K, Collection<V>> map){ this(map, Ordering.natural()); } public EnumValueSortMultiMap(final Map<K, Collection<V>> map, final Comparator<? super V> comparator){ super(map); this.comparator = comparator; } public EnumValueSortMultiMap(final Class<K> enumClass, final Comparator<? super V> comparator){ this(new EnumMap<K, Collection<V>>(enumClass), comparator); } public EnumValueSortMultiMap(final Class<K> enumClass){ this(new EnumMap<K, Collection<V>>(enumClass)); } @Override Map<K, Collection<V>> backingMap(){ return new EnumMap<K, Collection<V>>(enumType); } @Override public Comparator<? super V> valueComparator(){ return comparator; } @Override SortedSet<V> createCollection(){ return new TreeSet<V>(comparator); } } 

Other ways to do it

UPDATE: I think the correct way for Guava to do this would be something like this (it uses the SortedArrayList class, which I wrote in my other answer ):

 public static <E extends Enum<E>, V> Multimap<E, V> getMap( final Class<E> clz){ return Multimaps.newListMultimap( Maps.<E, Collection<V>> newEnumMap(clz), new Supplier<List<V>>(){ @Override public List<V> get(){ return new SortedArrayList<V>(); } } ); } 
+12
source share

If there is too much extra class, you might want to use the factory methods of the Multimaps class.

 SortedSetMultimap<Color, Entity> set = Multimaps.newSortedSetMultimap( new HashMap<Enum, Collection<Entity>>(), new Supplier<TreeSet<Entity>>() { @Override public TreeSet<Entity> get() { return new TreeSet<Entity>(new Comparator<Entity>() { @Override public int compare(Entity o1, Entity o2) { //TODO implement } }); } }); 
+6
source share

You can use EnumMap instead of the current HashMap . EnumMap more efficient for enumeration keys. In guava check Multimap [ examples ].

+2
source share

You need an implementation of the V collection, which is used in this way: HashMap <SomeEnum, V>. The above requirements affect V (not the entity). Correctly?

I think the TreeSet <E> link should fill in your queries:

  • TreeSet implements the Set interface
  • Sorted by order or custom Comparator
  • Items can be added and removed.
0
source share

All Articles