开发者

Are there plans for ImmutableEnumSet in Java 7?

I want to have all the effici开发者_JAVA技巧encies of EnumSet and pass it around without worrying that somebody would modify it.


You can get an immutable EnumSet with Google collections (Guava).


Resources :

  • Guava home page
  • Google-collections bug tracker - immutable enum set convenience constructor
  • Google documentation - Sets.immutableEnumSet()


What's wrong with Collections.unmodifiableSet() wrapping an EnumSet?

True, the original EnumSet is still mutable, but as long as you discard the original reference, it's as good as immutable inside the wrapper.

edit: OK, since EnumSet doesn't offer any instance methods over and above the Set interface, the only reason for not using this solution is that the EnumSet type is useful for documentation purposes, and you lose that when wrapping it in a Set. Other than that, EnumSet behaviour will be preserved.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜