Is Java 8 missing an OptionalBoolean?

前端 未结 4 1650
醉话见心
醉话见心 2020-12-29 20:23

As a primitive version of Optional*, Java 1.8 provides OptionalInt, OptionalLong and OptionalDouble.

But I cannot find the equivalent OptionalBoolean cl

4条回答
  •  無奈伤痛
    2020-12-29 21:04

    boolean values are often abused as parameters. Effective Java 2nd edition warns against abuse of booleans. They often lead to badly readable code if they are not for actual boolean true/false arguments. Instead, Joshua Bloch - the writer - tries to convince people to use double valued enum's:

    Prefer two-element enum types to boolean parameters. It makes your code easier to read and to write, especially if you're using an IDE that supports autocompletion. Also it makes it easy to add more options later.

    Most OptionalBoolean instances would probably be used incorrectly. This is a good reason for not including one. But I cannot say - only Oracle can - if this is the reason why it is not in there.

提交回复
热议问题