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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:47:55+00:00 2026-06-13T14:47:55+00:00

I just implement a MapBuilder to build map easy, But when i try to

  • 0

I just implement a MapBuilder to build map easy,
But when i try to get an instance of HashMap.class,I suddenly found that I can’t use HashMap.class to get such an instance.

It’s illegal!

So can anybody tell me why and how to solve this problem?

The MapBuilder is follow:

import java.util.Map;

public abstract class MapBuilder {

    public static <K, V, T extends Map<K, V>> InnerMapBuilder<T, K, V> start(
            Class<T> clazz) {
        return new InnerMapBuilder<>(clazz);
    }

    public static class InnerMapBuilder<T extends Map<K, V>, K, V> {

        private T target;

        public InnerMapBuilder(Class<T> clazz) {
            try {
                target = clazz.newInstance();
            } catch (InstantiationException | IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }

        public InnerMapBuilder<T, K, V> put(K key, V val) {
            target.put(key, val);
            return this;
        }

        public T get() {
            return target;
        }
    }
}

And the test code is below:

public static void main(String[] args) {
    HashMap<String, String> v = start(HashMap<String,String>.class).put("a", "b").get();
    System.out.println(v);
}
  • 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-13T14:47:56+00:00Added an answer on June 13, 2026 at 2:47 pm

    It’s impossible to obtain a parameterized class type variable for a generic type, as Reimeus has said. So you have three choices.

    First, you can live with the unchecked cast:

    Class<? extends Map<String, Integer>> clazz = 
        (Class<? extends Map<String, Integer>>) HashMap.class;
    

    Second, you can reify the parameters for a class by extending it (in this example, using an anonymous inner class):

    Class<? extends Map<String, Integer>> clazz =
         new HashMap<String, Integer>() {}.getClass();
    

    Or third, and best, just take the Map instance instead of a class in start(). You’re not saving the user any work by taking the Class rather than an instance of Map, and the first thing you do is create an instance of it.

    By passing it in, the user can even tweak the settings of the map (e.g. for a HashMap, set the load factor, for TreeMap, specify the Comparator) so it’s a better alternative anyway. If you need to, you can assert that it’s empty when it’s passed in.

    If for some reason you really need a factory, don’t use Class: it doesn’t work well as a factory, because the only way you can customize the instance that Class creates is by subclassing the class and providing a new no-arg constructor. Just create an interface Factory<T> that has a method T create() and then accept a Factory<? extends Map<K, V>.

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

Sidebar

Related Questions

I just found a nice tutorial that will help me implement an easy user
To implement a subscriptable object is easy, just implement __getitem__ in this object's class
I'm using R5RS Scheme and I just want to implement a function that returns
I´ve just started learning GWT and I´m trying to implement http://gwt.google.com/samples/Showcase/Showcase.html#!CwFileUpload and but failing
Well I just implement jquery-ui-autocomplete with VB.net in the server side, but it was
friends, i am new to iphone development and just implement google map in iphone
I can play video files with FFmpeg libary.( just implement tutorials about ffmpeg). But
In my app I just implement a dispatch_async block that will grab images from
Just trying to implement a simple audit logging solution with Grails 2.0.2 and it
We are just about to implement full blown TFS 2008 across our development team

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.