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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:31:18+00:00 2026-05-31T18:31:18+00:00

In the application I’m developing, I make use of several maps associating strings to

  • 0

In the application I’m developing, I make use of several maps associating strings to collections of elements, e.g. Map<String, List<String>>, Map<String, SortedSet<Object>>. In many cases I want simple functions to add/remove elements in the collection given by a certain key, possibly removing or creating new entries in the map.

I implemented some generic methods for the effect, but the putIntoCollection() method is giving me some problems. My implementation, which does not rise any warning, is the following:

public static <K, V, C extends Collection<V>> void putIntoCollection(
        Map<K, C> map, K key, V value, Class<? extends C> collectionClass)
        throws InstantiationException, IllegalAccessException {
    C collection = map.get(key);
    if (collection == null) {
        collection = collectionClass.newInstance();
        map.put(key, collection);
    }
    collection.add(value);
}

C represents the type of the collection, which can be any type of Collection, and the Class<? extends C> parameter allows me to pass a concrete class token to instantiate new C‘s (e.g. pass a token of ArrayList for a Map of Lists).

However, if I try to use it like this:

Map<String, Set<String>> tags;
String key, value;
MapUtilities.putIntoCollection(map, key, value, HashSet.class);

I get the compile error:

The parameterized method <K, V, Set<V>>putIntoCollection(Map<K,Set<V>>, K, V, Class<? extends Set<V>>) of type MapUtilities is not applicable for the arguments (Map<K,Set<V>>, K, V, Class<HashSet>) 

I understand that happens because I’m passing a Class<HashSet> argument while it expects a parametrized Set class. However, I don’t know how (or if) can I obtain such instances of Class. Is there a better way to do a generic method such as this?

  • 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-05-31T18:31:19+00:00Added an answer on May 31, 2026 at 6:31 pm

    Are you in a position to use third-party libraries? You’re basically reinventing Guava’s Multimap — ListMultimap<String, String> and SortedSetMultimap<String, Object> are your two examples. A bunch of implementations are provided — most notably, for your case, ArrayListMultimap and TreeMultimap.

    That said, it’s generally easier to just pass an explicit factory object:

    interface Supplier<T> { 
      T get();
    }
    
    void putIntoCollection(Map<K, Set<V>>, K, V, Supplier<Set<V>> emptySetSupplier);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Application I work on generates several hundreds of files (csv) in a 15 minutes
Application I am developing does some kind of server-side authorization. Communication is done via
my application use 10 threads that to read a lot of html file.similar the
Application shows information about planets, their moons, and etc. It shows a list of
application for project .....csproj is not installed. Make sure the application for the project
Application use NHibernate. I Have object A that contains set of objects B. I
application.rb config.time_zone = 'Athens' I want to list entries which are published between any
My application has a complex schema for the domain entity. It is required use
Application I develop requires several data sources (2 RDBMS and one file storage) to
Application able to record error in OnError, but we are not able to do

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.