1

Lets say I have a hash map like this:

private Map<someObject, Boolean> hashMap = new HashMap<>();

And I have two methods like this:

public void methodOne(key) {
    if (hashMap.get(key) == null || !hashMap.get(key)) {
       //do something
       hashMap.put(key, true);
    }
}

and

public void methodtwo(key) {
    if (hashMap.get(key) != null && hashMap.get(key)) {
       //do something else
       hashMap.put(key, false);
    }
}

My code seems to be working, but I am wondering the logical operation that I am doing in the if statements are valid and wether there is a better way to do it?

The other thing I am wondering is, what would be the result for the following logical operations:

  • null || true
  • null || false
  • null && true
  • null && false
5
  • 2
    The usual incorporation of null into boolean logic has null || true == true, null || false == null, null && true == null, null && false == false Commented Apr 16, 2020 at 23:36
  • 1
    Nothing wrong with your code, "better way" in this case is a matter of opinion. The result of the logical operations at the end of your question is a compilation error. Commented Apr 16, 2020 at 23:50
  • @khelwood How so? Is SQL more "usual" than C? Commented Apr 16, 2020 at 23:54
  • 1
    You cannot combine null with true or false. null can have many meanings, and such operations should not be performed. Java explicitly removed such operations from C/C++. Commented Apr 16, 2020 at 23:55
  • @Oleg I was describing the rules I know according to three-valued logic. If you know a better set of defined rules, go ahead. Commented Apr 17, 2020 at 7:20

4 Answers 4

5

You could use Map::getOrDefault. See below:

public void methodOne(key) {
    if (!hashMap.getOrDefault(key, false)) {
       //do something
       hashMap.put(key, true);
    }
}

and

public void methodtwo(key) {
    if (hashMap.getOrDefault(key, false)) {
       //do something else
       hashMap.put(key, false);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great answer because it doesn't assume any property of null, it simply replaces the missing value with a default one, in this case false.
0

How about this:

public void methodOne(key) {
  if (!Boolean.TRUE.equals(hashMap.get(key))) {
     //do something
     hashMap.put(key, true);
  }
}

public void methodtwo(key) {
  if (Boolean.TRUE.equals(hashMap.get(key))) {
     //do something
     hashMap.put(key, false);
  }
}

1 Comment

That hides the fact that null can be returned and completely goes against the principle of least surprise. It's a ugly hack and the next programmer may try and replace it with Boolean.FALSE.equals(...) and see the code fail horribly, usually in the field after testing.
0

Another option is to use optional

public void methodOne(key) {
    if (!Optional.ofNullable(hashMap.get(key)).orElse(false)) {
       //do something
       hashMap.put(key, true);
    }
}

public void methodtwo(key) {
    if (Optional.ofNullable(hashMap.get(key)).orElse(false)) {
       //do something else
       hashMap.put(key, false);
    }
}

Comments

-1

Are the if statements valid and is there a better way to do it?

Yes and yes. See answer by Stepan Kolesnik.

What would be the result for the following logical operations:

  • null || true
  • null || false
  • null && true
  • null && false

Truth table

                  null    false    true
null || true      yes     no       yes
null || false     yes     yes      no       methodOne: set true
null && true      ERROR   no       no
null && false     ERROR   no       no
!null && true     no      no       yes      methodtwo: set false
!null && false    no      yes      no

As you can see, in essence, methodOne is an unconditional key = true, while methodtwo leaves null unchanged, otherwise sets key = false.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.