Hashmap Key: String, Value: Boolean. How do I test the value within an if statement?

/ public void getFinishedBooks(HashMap<String, Boolean> library) {

if (library.size() < 1) {

  System.out.println("No books in Library");

} else {

  for (String books : library.keySet()) {

    **if (library.value == true) {**

I want the bottom IF statement to run if the Hashmap 'value' of data type Boolean is equal to try. I've tried .get() but that's not working.

Jon Skeet
people
quotationmark

Rather than getting the key set, I'd get the entry set - that means you can iterate over the key/value pairs, rather than having to look up each key again. For example, to print all the books with a true value:

for (Map.Entry<String, Boolean> entry : map.entrySet()) {
    if (entry.getValue()) {
        System.out.println(entry.getKey());
    }
}

Or to handle nullity if your values may be null:

for (Map.Entry<String, Boolean> entry : map.entrySet()) {
    if (TRUE.equals(entry.getValue()) {
        System.out.println(entry.getKey());
    }
}

people

See more on this question at Stackoverflow