不可變類就是創(chuàng)造以后就不能有任何改變的類,比如String
既然是不可變的,那么天然是線程安全的,而且可以緩存
1.class必須被聲明為final
禁止繼承,如果有子類,子類就可以覆蓋掉field,重寫method,變成一個(gè)隨便怎么樣的類,然后還屬于父類這個(gè)類型,那么父類就可以不是不可變類了
2.field都是private final,不能提供它們的set方法
3.構(gòu)造器初始化初始化field的時(shí),深拷貝
不能初始化的環(huán)境中還有這個(gè)對(duì)象的引用,不然就可以通過這個(gè)引用在外面改變這個(gè)對(duì)象的值
4.get方法中,不給field的引用,只深拷貝一份返回
原因和3一樣

@Slf4j
@ThreadSafe
public class ImmutableExample2 {
private static Map<Integer, Integer> map;
static {
Map<Integer, Integer> m = Maps.newHashMap();
m.put(1, 2);
m.put(3, 4);
m.put(5, 6);
map = Collections.unmodifiableMap( m);
}
public static void main(String[] args) {
map.put(1, 3); //這行報(bào)錯(cuò)
log.info("{}", map.get(1));
}
}
內(nèi)部實(shí)現(xiàn)是 重寫了put等修改的方法
public V put(K key, V value) {
throw new UnsupportedOperationException();
}
public V remove(Object key) {
throw new UnsupportedOperationException();
}
public void putAll(Map<? extends K, ? extends V> m) {
throw new UnsupportedOperationException();
}
public void clear() {
throw new UnsupportedOperationException();
}

@ThreadSafe
public class ImmutableExample3 {
private final static ImmutableList<Integer> list = ImmutableList.of(1, 2, 3);
private final static ImmutableSet set = ImmutableSet.copyOf(list);
private final static ImmutableMap<Integer, Integer> map = ImmutableMap.of(1, 2, 3, 4);
private final static ImmutableMap<Integer, Integer> map2 = ImmutableMap.<Integer, Integer>builder()
.put(1, 2).put(3, 4).put(5, 6).build();
public static void main(String[] args) {
System.out.println(map2.get(3));
}
}