Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Unchecked cast from Object to Map.Entry<String,String>

I have an "unchecked cast from Object to Map.Entry" on "Map.Entry entry = (Entry) o;"

I think everything is OK and I can put an add suppress warning safely but I'm not sure. Here's my code.

public abstract class WebPath {
    protected Hashtable<String, String> wp;

    public String get(String whatDoYouWant) {
        for (Object o : wp.entrySet()) {

            Map.Entry<String, String> entry = (Entry<String, String>) o;
            if (whatDoYouWant.equals(entry.getKey())) {
                return (String) entry.getValue();
            }
        }

        return null;
    }
}

Thank you everybody, have a good day !

like image 336
Benjamin Bettan Avatar asked Jul 29 '26 01:07

Benjamin Bettan


1 Answers

Don't suppress the warning, it's giving you good advice! You are choosing to assign each entry to the uninformative base class Object, instead of the more precise type, Map.Entry<String, String>. If you fix your for-loop you will eliminate the warning:

for(Map.Entry<String, String> o : wp.entrySet()) {
like image 102
Mark Peters Avatar answered Jul 30 '26 13:07

Mark Peters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!