I am new to java so please go easy on me. I have a hashmap which contains String keys and Boolean values like the following.
Map<String, Boolean> states = new HashMap<String, Boolean>();
states.put("b_StorageAvailable", true);
states.put("b_StorageWritable", true);
Which I am returning from a function. Once I get this somewhere else, I would like to be able to call an if statement on one of these to see if its true or false.
if(states.get("b_StorageAvailable")) {
//Do this
}
But java keeps showing me that I need this to be a Boolean type, and it is a Map type. How can I do this easily?
UPDATE
It should be noted that the code I am calling the function with and getting the return value looks like this,
Map states = this.getExternalStorageStatus();
Your code looks fine. Just make sure you’re using exactly the same key (
"b_StorageAvailable"). Because theBooleanin theMapis auto-boxed to a primitive boolean, if there is no entry in theMapfor the supplied key, you’ll get aNullPointerException.I would also check that your function’s return type and the local variable are defined as
HashMap<String, Boolean>as well. If it is untyped, then you won’t be able to assume it’s aBooleanin theMap.So all you need to do is change
to
And potentialy change the return type of
getExternalStorageStatus()