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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:51:44+00:00 2026-05-19T00:51:44+00:00

public class AutoKeyMap<K,V> { public interface KeyGenerator<K> { public K generate(); } private KeyGenerator<K>

  • 0
public class AutoKeyMap<K,V> {

    public interface KeyGenerator<K> {
        public K generate();
    }    
    private KeyGenerator<K> generator;

    public AutoKeyMap(Class<K> keyType) {
        // WARNING: Unchecked cast from AutoKeyMap.IntKeyGen to AutoKeyMap.KeyGenerator<K>
        if (keyType == Integer.class) generator = (KeyGenerator<K>) new IntKeyGen();
        else throw new RuntimeException("Cannot generate keys for " + keyType);
    }

    public void put(V value) {
        K key = generator.generate();
        ...
    }


    private static class IntKeyGen implements KeyGenerator<Integer> {

        private final AtomicInteger ai = new AtomicInteger(1);

        @Override public Integer generate() {
            return ai.getAndIncrement();
        }

    }


}

In the code sample above, what is the correct way to prevent the given warning, without adding a @SuppressWarnings, if any?

  • 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-19T00:51:45+00:00Added an answer on May 19, 2026 at 12:51 am

    You can fix your code temporarily by doing this:

    private KeyGenerator<?> generator;
    
    public AutoKeyMap(Class<?> keyType) {
        // WARNING: Unchecked cast from AutoKeyMap.IntKeyGen to AutoKeyMap.KeyGenerator<K>
        if (keyType == Integer.class) generator = new IntKeyGen();
        else throw new RuntimeException("Cannot generate keys for " + keyType);
    }
    

    But if you need to implement a method in AutoKeyMap like

    K generate(){
      return generator.generate();
    }
    

    then it will break again. The problem is as soon as you start doing runtime checking of class types (like you do in if(keyType == Integer.class)) then Java compiler has no way of statically ensuring that type will be correct. Because you can instantiate new AutoKeyMap<Boolean>(Class<Boolean> keyType) and then write

    if(keyType == Boolean.class){
      generator = new IntKeyGen();
    }
    

    which will obviously break, and therefore the whole point of statically checked generic types will be lost.

    Usually all problems like yours that involve dynamic type casting are beyond generics, so you have to live with unchecked casts, and just make sure you write enough unit tests and make sure your code works and does not throw ClassCastException in all scenarios.

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

Sidebar

Related Questions

public class WrapperTest { public static void main(String[] args) { Integer i = 100;
I have class A: public class ClassA<T> Class B derives from A: public class
public class MyClass { public int Age; public int ID; } public void MyMethod()
public class Test { public static void main(String[] args) { } } class Outer
public class Item { ... } public class Order { public List<Item> Items ...
public class Address { public string ZipCode {get; set;} } public class Customer {
public class doublePrecision { public static void main(String[] args) { double total = 0;
public class CovariantTest { public A getObj() { return new A(); } public static
public class WrapperTest { static { print(10); } static void print(int x) { System.out.println(x);
In the following snippet: public class a { public void otherMethod(){} public void doStuff(String

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.