Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3460114
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:11:26+00:00 2026-05-18T10:11:26+00:00

I need some data structure that I can build from standard collections or using

  • 0

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 a and b, that a.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.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T10:11:26+00:00Added an answer on May 18, 2026 at 10:11 am

    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 made AbstractSortedSetMultimap package scoped.

    I will use this enum in all my examples:

    public enum Color{
        RED, BLUE, GREEN
    };
    

    Constructing the Class

    There are six constructors:

    • Empty (Uses a HashMap and natural ordering for values)

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

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

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

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

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

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

    Source Code

    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 guess the proper Guava way to do it would have been something like this (it uses the SortedArrayList class 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>();
                }
            }
        );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to export some data from SAS to CSV, so that I can
I have some data and need to create a json file with this structure
I am writing some and I need to pass a complicated data structure to
I need to read some data from an input file and plot a graph
I need to write some data to SQL Server database from Linux in C++.
I need a data structure of some sort to do the following: One set
I've got some delimited data that I need to get into a Flex tree
We are using an extractor application that will export data from the database to
I am trying to build a data table structure that will best support the
I need to build a java based JSON data API that will be accessed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.