1

Here is a example:

import java.util.HashMap;

class Main {
    public static void main(String[] args) {
        HashMap<Integer, Integer> a = new HashMap<Integer, Integer>();
        a.put(1,2);
        a.put(2,5);
        Object b = a;
        // Do something here to make the variable "b" become a HashMap
    }
}

I tried this

HashMap<Integer, Integer> c = (HashMap<Integer, Integer>) b;

I get this warning:

Unchecked cast: 'java.lang.Object' to 'java.util.HashMap<java.lang.Integer,java.lang.Integer>'

4
  • When posting here, always include the full text of your warning or error. Just saying "gave me a warning" is not helpful. Also sometimes helpful to mention the version of Java. Commented Nov 4, 2023 at 5:28
  • Since Java is a strongly typed language, it attempts to check for possible incompatible casts/assignments upfront during compilation time. This is what is usually desirable too, so that you don't get the issue in production deployment. However, for genuine use cases (like frameworks), there are other provisions, like generics. If you can provide the use case, it will help. Commented Nov 4, 2023 at 5:28
  • Please use proper English, punctuation, case, etc. as best you can when posting here. This site is meant to be more like Wikipedia, less like a casual chat room. I cleaned up some of your prose for you this time. Commented Nov 4, 2023 at 5:30
  • By the way, the Java compiler can infer the parameterized types on right side of the = assignment. So: = new HashMap <>();. Commented Nov 4, 2023 at 5:32

2 Answers 2

2

Don’t use @SuppressWarnings. That warning exists for good reason: all sorts of strange things can go wrong, if you try to force the compiler to make an unsafe assumption about a generically typed object. For example:

Map<Integer, Integer> a = Map.of(1, 2, 3, 4);

Object obj = a;

// Compiler warning:  unchecked cast
Map<String, String> b = (Map<String, String>) obj;

// Causes ClassCastException, even though the code has no obvious cast!
String key = b.get(1);

The safe way to do this is to immediately verify that the object is a Map that contains Integer keys and values:

Map<Integer, Integer> a = Map.of(1, 2, 3, 4);

Object obj = a;

// This is safe, because it doesn't make any assumptions about generic typing.
Map<?, ?> b = (Map<?, ?>) obj;

Map<Integer, Integer> c = new HashMap<>();
for (Map.Entry<?, ?> entry : b.entrySet()) {
    // Throws a ClassCastException right away, instead of later
    // in an unexpected and strange place.
    Integer key = (Integer) entry.getKey();
    Integer value = (Integer) entry.getValue();

    c.put(key, value);
}
Sign up to request clarification or add additional context in comments.

Comments

1

The warning is expectable in this situation, because such a casting considered as an unsafe operation. From your example it is hard to say why do you need such a cast, maybe you should re-think your code to avoid it. But if there is no other option, you may leave it, because in runtime it will work. You may also consider to suppress the warning for the method or class:

@SuppressWarnings({"unchecked"})
public static void main(String[] args) {
   // your code there
}

4 Comments

I would add the word "potentially* unsafe.
i got a another warning: HashMap is a raw type. References to generic type HashMap<K,V> should be parameterized
@HuyWallz update your question with the code example, or create a different question as it's not really related to the "Convert "Object" to "HashMap" in Java" topic.
I'd put the @SuppressWarnings on only variable c, not the entire method. That keeps the scope of the suppression as small as possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.