标签:
Immutable objects have many advantages, including:
public static final ImmutableSet<String> COLOR_NAMES = ImmutableSet.of( "red", "orange", "yellow", "green", "blue", "purple"); class Foo { final ImmutableSet<Bar> bars; Foo(Set<Bar> bars) { this.bars = ImmutableSet.copyOf(bars); // defensive copy! } }
不可变的集合,其实是保证了最重要的安全性,还有性能上的提升。
也可以对这种集合做排序,比如调用ImmutableSortedSet 这个排序是在构造函数阶段排序的。
Google Guava 学习记录《4》Immutable Set
标签:
原文地址:http://www.cnblogs.com/-Doraemon/p/4806701.html