How can you check the boolean value inside the map?

I am new to java, so please come to me. I have a hashmap that contains String and Boolean keys, such as 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 the function. As soon as I get it somewhere else, I would like to be able to call the if statement on one of them to see if it is true or false.

 if(states.get("b_StorageAvailable")) { //Do this } 

But java continues to show me that I need it to be a Boolean type, and this is a map type. How can i make it easy?

UPDATE

It should be noted that the code that I call with the function and get the return value looks like this:

 Map states = this.getExternalStorageStatus(); 
+6
source share
4 answers

Your code looks great. Just make sure you use the exact same key ( "b_StorageAvailable" ). Since a Boolean in Map automatically placed in a field with a primitive boolean value, if there is no entry in the Map for the entered key, you will get a NullPointerException .

I would also like to check that the type of the returned function and the local variable are also defined as HashMap<String, Boolean> . If it is untyped, then you cannot consider it a Boolean in Map .

 public static void main(String[] args) { Map<String, Boolean> states = new HashMap<String, Boolean>(); states.put("b_StorageAvailable", true); states.put("b_StorageWritable", true); if(states.get("b_StorageAvailable")){ // works fine! System.out.println("storage is available!"); } Map states2 = new HashMap<String, Boolean>(); // untyped! states.put("b_StorageAvailable", true); states.put("b_StorageWritable", true); if(states2.get("b_StorageAvailable")){ // Type mismatch: cannot convert from Object to boolean System.out.println("storage is available!"); } } 

So all you have to do is change

 Map states = this.getExternalStorageStatus(); 

to

 Map<String, Boolean> states = this.getExternalStorageStatus(); 

And potentially change the return type getExternalStorageStatus()

 private Map<String,Boolean> getExternalStorageStatus(){ ... } 
+5
source

Assuming you are in Java 5 or later (which you need, given the demonstrated use of Generics):

 if(Boolean.TRUE.equals(states.get("b_StorageAvailable"))){ //Do this } 
+5
source

When you do it

 if(states.get("b_StorageAvailable")) 

You enter a value stored with the key b_StorageAvailable

change it as follows:

 if(states.get("b_StorageAvailable")) != null { //Do your task check if the boolean is false or not } 

will bring you to you or the simplest

 if(states.get("b_StorageAvailable") != null && states.get("b_StorageAvailable").booleanValue()) { //Do your task } 

if you want to check if your map contains a specific key, you can check that if map.containsKey API

+1
source

Your syntax is fine, I wrote the test code below, and it passed. You can also make sure that you spelled the key values ​​correctly and check to make sure the key is valid using containsKey. I wrote the following test code and passed it.

(As an additional suggestion, you may also want to reorganize the map of your states into the appropriate class, as I did below. I avoid, whenever possible, moving around the Maps).

 import static org.junit.Assert.assertTrue; import java.util.HashMap; import java.util.Map; import org.junit.Test; public class StatesTest { @Test public void testStates() { Map<String, Boolean> statesMap = new HashMap<String, Boolean>(); statesMap.put("b_StorageAvailable", true); statesMap.put("b_StorageWriteable", false); States states = new States(statesMap); assertTrue(states.isStorageAvailable()); assertTrue(states.isStorageWriteable() == false); } } class States { private boolean storageAvailable; private boolean storageWriteable; // etc public States(Map<String, Boolean> states) { if (states.containsKey("b_StorageAvailable")) { storageAvailable = states.get("b_StorageAvailable"); } if (states.containsKey("b_StorageWriteable")) { storageWriteable = states.get("b_StorageWriteable"); } } public boolean isStorageAvailable() { return storageAvailable; } public boolean isStorageWriteable() { return storageWriteable; } } 
0
source

Source: https://habr.com/ru/post/903568/


All Articles