Beauregard Lionett
Beauregard Lionett

Reputation: 321

Not an "unsafe operation" error: Incompatible types Object cannot be converted to Entry<String, Boolean>

I have a Map in a class where I store a String key and a boolean value. Then, I return the map from the function getMap().

public class FacilityMachines {
    private static Map<String, Boolean> map = new HashMap<String, Boolean>();

    public Map getMap(){
         return map;
    }

In the class below, I'm trying to fetch that map, and then save it to an external file, I also instantiate FacilityMachines there:

public class WriteFile {
    FacilityMachines fm = new FacilityMachines();
    private Map<String, Boolean> m = new HashMap<String, Boolean>();

}

In WriteFile, I'm trying to parse the map into a new HashMap:

public void saveFacilityInfo() {
    for (Map.Entry<String, Boolean> j: fm.getMap().entrySet()){
            String s = j.getKey();
            boolean b = j.getValue();
            oStream.println(i + ": " + s + " = " + b + ". ");
        }
}

oStream is just the variable for my PrintWriter.

The above yields Object cannot be converted to Entry<String, Boolean> error.

If I change the method signature of saveFacilityInfo to saveFacilityInfo(FacilityMachines fm), and then use the fm variable to try to fetch the map at the line for (Map.Entry<String, Boolean> j: fm.getMap().entrySet()) then I get a cannot find symbol on all the functions from the Entry interface: entrySet(), getKey(), and getValue().

And before anyone asks, I've imported HashMap and Map, and also tried using only import java.util.*; to import everything just in case.

I've also tried extending FacilityMachines from WriteFile and got the same results.

Upvotes: 0

Views: 1245

Answers (1)

JavierFromMadrid
JavierFromMadrid

Reputation: 621

You need to return the map on the FacilityMachines class getMap() method with the correct type

public class FacilityMachines {
    private static Map<String, Boolean> map = new HashMap<String, Boolean>();

    public Map<String, Boolean> getMap(){
        return map;
    }
}

Upvotes: 2

Related Questions