Guava的不可变集合

1、不可变集合的优势java

不可变对象有不少优势,包括:编程

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

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

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

2、建立不可变集合ide

1.使用of方法工具

如:I测试

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

2.使用copyOfthis

如:google

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();

3、如何实现不可变

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

 

4、源码中有趣的地方

不可变集合能够经过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