I have a collection of objects, and for each of them I need to know if a specific condition has been met. That is a ternary condition, it could be not yet tested, tested and failed, or tested and passed. Currently I have a map where the objects are keys, and the values are Booleans. The value is null until tested, and true or false thereafter. However, while the objects should never change during this process, they are not immutable and could theoretically change. I feel like there must be a better way to implement this, but I don't know what it is. Any ideas?


2 Answers 2


You have several options:

  1. Rather than using Boolean in your current code, create an enum with values UNTESTED, PASSED, and FAILED. That is more descriptive than a ternary Boolean value. Enumerations have advantages beyond readability, such as being able to add methods to do stuff.

  2. If the test is computationally inexpensive, simply add a method on the object being tested to return the test result. If the test is computationally expensive and the method to do the test may be called a lot this may do more harm than good.

  3. Store the test result on the object itself. If any of the test inputs change, the object can blank out the test result, effectively invalidating the test result.

In all cases I would still apply the change in option 1. You really should be using a descriptive enumeration rather than a Boolean.

See Also

  • I like the enum idea. I'll end up using that, I'm sure. How the computation is done is left up to subclasses of my primary object, but may be done asynchronously to allow for things like network access, so option 2 does not work for me. Option 3 might be useful though. But you don't see any problems with using the object as the key?
    – TBridges42
    Commented Oct 27, 2015 at 22:20
  • 1
    @TBridges42 using the object as a key is fine even if the object changes, as long as no state used in equals() or hashCode() changes. In other words, as long as its identity remains the same. If this is not true, then I would need additional details about your design to determine how best to proceed.
    – user22815
    Commented Oct 27, 2015 at 22:37

You can use Collections.unmodifiableMap(myMap); to return an immutable view of your map, but I'm not sure how this would fit into your current code - you can't add new objects to an unmodifiable map.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.