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

  • Home
  • SEARCH
  • 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 3970250
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:03:50+00:00 2026-05-20T04:03:50+00:00

I’m trying to develop a generic function to filter maps. The code I have

  • 0

I’m trying to develop a generic function to filter maps.

The code I have so far is:

public static Map<?, ?> filterAttrs(Map<?, ?> args, String... unless) {

    Map<?, ?> filteredAttrs = Map.class.newInstance();

    Arrays.sort(unless);
    for (Object o : args.keySet()) {
        if (Arrays.binarySearch(unless, o.toString()) < 0 ) {
            filteredAttrs.put(o, args.get(o));
        }
    }
    return filteredAttrs;
}

I get the following errors in filteredAttrs.put

The method put(capture#5-of ?, capture#6-of ?) in the type Map is not applicable for the arguments (Object, capture#8-of ?)

I don’t know how to instantiate a generic Map (I tried with 1Map.class.newInstance()`).

Any ideas?

Edit: after reading many answers, the problem seems to be how to make filteredAttrs be an instance of the same type as args. (Map) args.getClass().newInstance() seems to do the trick.

  • 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-20T04:03:50+00:00Added an answer on May 20, 2026 at 4:03 am

    The problem with this code is that the type system prevents you from putting an object into a Map whose key type is ?. This is because if the key type is ?, the compiler has no idea what’s actually being stored in the map – it could be Object, or Integer, or List<Object> – and so it can’t confirm that what you’re trying to add to the map is actually of the correct type and won’t be out of place in the Map. As an example, if you have this method:

    public static void breakMyMap(Map<?, ?> m) {
        m.put(new Object(), new Object()); // Won't compile
    }
    

    and then write code like this:

    Map<String, String> myMap = new HashMap<String, String>();
    breakMyMap(myMap);
    

    Then if the code in breakMyMap were to compile, it would put a pair of Objects as keys and values into a Map<String, String>, breaking the invariant that all the elements are indeed Strings.

    To fix this, instead of making this function work on Map<?, ?>, change the function so that you have more type information about what the keys and values are. For example, you could try this:

    public static <K, V> Map<K, V> filterAttrs(Map<K, V> args, String... unless) {
    
        Map<K, V> filteredAttrs = new HashMap<K, V>();
    
        Arrays.sort(unless);
        for (K o : args.keySet()) {
            String attr = o.toString();
            if (Arrays.binarySearch(unless, o.toString()) < 0 ) {
                filteredAttrs.put(o, args.get(o));
            }
        }
        return filteredAttrs;
    }
    

    Now that the compiler knows that the key type is K, it can verify that the put will not mix up the types of the keys in the map.

    Another thing I should point out is that the code you had would never have worked even if this did compile. The reason is that the line

    Map<?, ?> filteredAttrs = Map.class.newInstance();
    

    Will cause an exception at runtime because Map is an interface, not a class, and so trying to use newInstance to create an instance of it will fail to work correctly. To fix this, you can either specify the type of the map explicitly (as I’ve done in the above code), or get the class of the argument:

    Map<K, V> filteredAttrs = args.getClass().newInstance();
    

    Of course, this isn’t guaranteed to work either, though the general contract for collections is that all collections should have a no-arg constructor.

    Hope this helps!

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

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
public static bool CheckLogin(string Username, string Password, bool AutoLogin) { bool LoginSuccessful; // Trim
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.