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 8837911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:51:03+00:00 2026-06-14T09:51:03+00:00

Is there an existing open source Map implementation for java, which would be a

  • 0

Is there an existing open source Map implementation for java, which would be a normal key-value map, but would also support multiple values per key? The multimap implementations I’ve found seem to associate key with collection, which doesn’t quite cut it, as I need a drop-in replacement for existing code.

I sense some people saying “you can’t do that”, so here’s an example of one way how it can behave, in a widely used framework, Qt. Here’s an excerpt form the docs for QMap class:

If the map contains no item with key key, the function returns a
default-constructed value. If there are multiple items for key in the
map, the value of the most recently inserted one is returned.

My need is quite limited, so at the moment I’m using the hack below, which is adequate, since there are no removals and many values per key are exception, and the duplicate keys getting a bit mangled is not a problem:

public static <V, V2 extends V> String mapMultiPut(
        Map<String, V> map, 
        String key, 
        V2 value) {
    int count = 0;
    String tmpKey = key;
    while (map.containsKey(tmpKey)) {
        ++count;
        tmpKey = key + '_' + count;
    }
    map.put(tmpKey, value);
    return tmpKey;
}

But I’d like a nicer solution, if one exists…

  • 1 1 Answer
  • 0 Views
  • 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-06-14T09:51:04+00:00Added an answer on June 14, 2026 at 9:51 am

    You could use a ListMultimap along with

    Iterables.getLast(listMultiMap.get(key), defaultValue(key))
    

    where you define your own defaultValue method.

    This assumes you don’t actually need the Map interface in your class.

    If you really want a Map you could try this

    public abstract class QtMap<K, V> extends ForwardingMap<K, V>
    {
        private final ListMultimap<K, V> listMultimap = ArrayListMultimap.create();
    
        final Map<K, V> delegate = Maps.<K, Collection<V>, V> transformEntries(listMultimap.asMap(), new EntryTransformer<K, Collection<V>, V>()
        {
    
            @Override
            public V transformEntry(K key, Collection<V> value)
            {
                return Iterables.getLast(value, defaultValue(key));
            }
    
        });
    
        @Override
        protected Map<K, V> delegate()
        {
            return delegate;
        }
    
        @Override
        public V put(K key, V value)
        {
            listMultimap.put(key, value);
            return null;
        }
    
        @Override
        public void putAll(Map<? extends K, ? extends V> map)
        {
            for (Map.Entry<? extends K, ? extends V> entry : map.entrySet())
            {
                put(entry.getKey(), entry.getValue());
            }
        }
    
        @Override
        public V get(Object key)
        {
            return listMultimap.containsKey(key) ? delegate.get(key) : defaultValue(key);
        }
    
        protected abstract V defaultValue(Object key);
    
    }
    

    although it’s only sketchily tested

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone point me at a simple, open-source Map/Reduce framework/API for Java? There doesn't
Is there an existing open-source Java or JSTL tag library that will render the
Before we implement our own, is there an existing Open Source piece of Java
Are there any existing add-ons which would provide the functionality Upload image from local
In short: Is there an existing open-source Python interface for the Apache scoreboard IPC
Are there any open source libraries which offer implementations of some or all of
Are there any existing open source UIWebView control (with back/forward buttons) that is suitable
Are there any existing open source or commercial solutions for this?
Is there a single open source library which contains API calls for each brokers
Is there an open source Java library/algorithm for finding if a particular piece of

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.