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();
To check if a value is of boolean type, check if the value is equal to false or equal to true , e.g. if (variable === true || variable === false) . Boolean values can only be true and false , so if either condition is met, the value has a type of boolean.
To check if a value exists in a map:Convert the iterator to an array and call the includes() method on the array, passing it the specific value as a parameter. The includes method returns true if the value is contained in the array and false otherwise.
Return Value: The Map.has() method returns a boolean value. It returns true if the element exists in the map else it returns false if the element doesn't exist. Examples of the above function are provided below.
Assuming you're on Java 5 or newer (which you must be, given the demonstrated use of Generics):
if(Boolean.TRUE.equals(states.get("b_StorageAvailable"))){
//Do this
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With