Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you test for a boolean value inside a Map?

Tags:

java

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();
like image 714
Metropolis Avatar asked Dec 12 '11 04:12

Metropolis


People also ask

How do you test a boolean?

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.

How do you check if a map has a value?

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.

Does MAP return boolean?

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.


1 Answers

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
}
like image 91
ziesemer Avatar answered Oct 24 '22 05:10

ziesemer