Guava的不可变集合

原创
2016/11/02 17:53
阅读数 406

一、不可变集合的优点

不可变对象有很多优点,包括:

  • 当对象被不可信的库调用时,不可变形式是安全的;
  • 不可变对象被多个线程调用时,不存在竞态条件问题
  • 不可变集合不需要考虑变化,因此可以节省时间和空间。所有不可变的集合都比它们的可变形式有更好的内存利用率(分析和测试细节);
  • 不可变对象因为有固定不变,可以作为常量来安全使用。

创建对象的不可变拷贝是一项很好的防御性编程技巧。Guava为所有JDK标准集合类型和Guava新集合类型都提供了简单易用的不可变版本。

摘自:http://ifeve.com/google-guava-immutablecollections/

二、创建不可变集合

1.使用of方法

如:I

mmutableList<String> ilist = ImmutableList.of("a","b","c","d","e");

2.使用copyOf

如:

List<String> slist = new ArrayList<String>(20);

slist.add("a");

slist.add("b");

slist.add("c");

slist.add("d");

slist.add("e");

//通过已有List创建不可变list

ImmutableList<String> ilist = ImmutableList.copyOf(slist);

3.使用Builder工具

如:

List<String> slist = new ArrayList<String>(20);

slist.add("a");

slist.add("b");

slist.add("c");

slist.add("d");

slist.add("e");

ImmutableList<String> ilist = ImmutableList.<String>builder().addAll(list).build();

三、如何实现不可变

1.对不可变集合调用addAll、add、set、remove都会抛出UnsupportedOperationException异常

如下是ImmutableList类的源码:

/**

* Guaranteed to throw an exception and leave the list unmodified.

*

* @throws UnsupportedOperationException always

* @deprecated Unsupported operation.

*/

@CanIgnoreReturnValue

@Deprecated

@Override

public final boolean addAll(int index, Collection<? extends E> newElements) {

throw new UnsupportedOperationException();

}

 

/**

* Guaranteed to throw an exception and leave the list unmodified.

*

* @throws UnsupportedOperationException always

* @deprecated Unsupported operation.

*/

@CanIgnoreReturnValue

@Deprecated

@Override

public final E set(int index, E element) {

throw new UnsupportedOperationException();

}

 

/**

* Guaranteed to throw an exception and leave the list unmodified.

*

* @throws UnsupportedOperationException always

* @deprecated Unsupported operation.

*/

@Deprecated

@Override

public final void add(int index, E element) {

throw new UnsupportedOperationException();

}

 

/**

* Guaranteed to throw an exception and leave the list unmodified.

*

* @throws UnsupportedOperationException always

* @deprecated Unsupported operation.

*/

@CanIgnoreReturnValue

@Deprecated

@Override

public final E remove(int index) {

throw new UnsupportedOperationException();

}

 

2.集合中存储的可变对象仍可改变

Users[] arr = new Users[3];

arr[0] = new Users();

arr[0].setId(1);//id=1

arr[1] = new Users();

arr[1].setId(2);

arr[2] = new Users();

arr[2].setId(3);

//copyof 是浅拷贝,因此集合中存放可变对象时,原对象的属性修改了,集合中对象的属性也会修改

ImmutableList<Users> alist = ImmutableList.copyOf(arr);

arr[0].setId(5);

System.out.println(alist.get(0).getId());//id=5

 

四、源码中有趣的地方

不可变集合可以通过of方法来创建,of方法是包含12个重载方法如下:

@SuppressWarnings("unchecked")

public static <E> ImmutableList<E> of() {

return (ImmutableList<E>) EMPTY;

}

 

public static <E> ImmutableList<E> of(E element) {

return new SingletonImmutableList<E>(element);

}

 

public static <E> ImmutableList<E> of(E e1, E e2) {

return construct(e1, e2);

}

 

public static <E> ImmutableList<E> of(E e1, E e2, E e3) {

return construct(e1, e2, e3);

}

 

public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4) {

return construct(e1, e2, e3, e4);

}

 

public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5) {

return construct(e1, e2, e3, e4, e5);

}

 

public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6) {

return construct(e1, e2, e3, e4, e5, e6);

}

 

public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7) {

return construct(e1, e2, e3, e4, e5, e6, e7);

}

public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8) {

return construct(e1, e2, e3, e4, e5, e6, e7, e8);

}

public static <E> ImmutableList<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9) {

return construct(e1, e2, e3, e4, e5, e6, e7, e8, e9);

}

public static <E> ImmutableList<E> of(

E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10) {

return construct(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10);

}

 

public static <E> ImmutableList<E> of(

E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10, E e11) {

return construct(e1, e2, e3, e4, e5, e6, e7, e8, e9, e10, e11);

}

 

// These go up to eleven. After that, you just get the varargs form, and

// whatever warnings might come along with it. :(

 

@SafeVarargs // For Eclipse. For internal javac we have disabled this pointless type of warning.

public static <E> ImmutableList<E> of(

E e1, E e2, E e3, E e4, E e5, E e6, E e7, E e8, E e9, E e10, E e11, E e12, E... others) {

Object[] array = new Object[12 + others.length];

array[0] = e1;

array[1] = e2;

array[2] = e3;

array[3] = e4;

array[4] = e5;

array[5] = e6;

array[6] = e7;

array[7] = e8;

array[8] = e9;

array[9] = e10;

array[10] = e11;

array[11] = e12;

System.arraycopy(others, 0, array, 12, others.length);

return construct(array);

}

 

java方法是支持可变参数的,完全可以写成,public static <E> ImmutableList<E> of(E... others);这样实现的原因大神在注释中给出了,为了避免调用者处理类型检测警告。

当方法声明为泛型可变参数时,调用该方法时,编译器就会提示该警告,在JDK1.6之前,可以在方法调用的地方添加@SuppressWarnings("unchecked")注解,但这样比较麻烦,项目组每个调用的地方都需要处理,当然也可以忽略这个问题。

此处ImmutableList中of方法的12个重载的前11个就是为了尽量减少类型检测警告,大神还是很贴心滴为我们写了11个重载,而第12个方法通过JDK1.7的新注解@SafeVarargs 来避免类型检测警告。

@SafeVarargs的用法请参考:http://book.51cto.com/art/201205/339154.htm

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部