开发者

Are there plans for ImmutableEnumSet in Java 7?

开发者 https://www.devze.com 2023-01-14 09:53 出处:网络
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).

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

精彩评论

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