Guava记录 - Collections(一) - 不可变集合(Immutable Collections)

一、简介

官网对Immutable Collections的简介如下:

Immutable collections, for defensive programming, constant collections, and improved efficiency.
大致意思就是可以通过不可变集合应用于防御式编程、常量集合以及提高效率.

二、为什么使用 Immutable Collections?

guava gitlab wiki上给出的解释是不可变对象有很多优点,包括:

1、对于不受信任的库来说是安全的。
2、线程安全:可以由许多线程使用,没有竞争条件的风险。
3、不需要支持变异,可以节省时间和空间的假设。所有不可变集合实现都比它们的可变同类型的实现更节省内存。(分析)
3、可以用作常数,期望它保持不变。
4、创建对象的不可变副本是一种很好的防御性编程技术。Guava提供每个标准集合类型的简单、易于使用的不可变版本,包括Guava自己的集合变体。

当我们不期望修改一个集合,或者期望一个集合保持不变时,将它复制到一个不可变的集合是一个很好的办法。

重要提示:每个Guava不可变集合实现都拒绝null值的。谷歌的内部代码库进行了详尽的研究,结果表明,在大约5%的情况下,集合中允许使用null元素,而在95%的情况下,在null上快速失败是最好的方法。如果需要使用null值,可以考虑使用集合。

三、如何使用 Immutable Collections?

Immutable Collections支持多种创建方式

1、通过of()方法创建

通过of()方法创建的不可变集合,除了已排序的集合外,其他的都是按照时间构建来排序的;

简单实验:

 ImmutableList<String> createByOf = ImmutableList.of("a", "b");

思考点 - 为什么是以这种方式创建?为什么当节点是13个节点的时候使用数组?

/**
   * Returns an immutable list containing a single element. This list behaves
   * and performs comparably to {@link Collections#singleton}, but will not
   * accept a null element. It is preferable mainly for consistency and
   * maintainability of your code.
   *
   * @throws NullPointerException if {@code element} is null
   */
  public static <E> ImmutableList<E> of(E element) {
    return new SingletonImmutableList<E>(element);
  }

  /**
   * Returns an immutable list containing the given elements, in order.
   *
   * @throws NullPointerException if any element is null
   */
  public static <E> ImmutableList<E> of(E e1, E e2) {
    return construct(e1, e2);
  }


  /**
   * Returns an immutable list containing the given elements, in order.
   *
   * @throws NullPointerException if any element is null
   * @since 3.0 (source-compatible since 2.0)
   */
  @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);
  }

2、通过copyOf()方法创建

简单实验:

  ImmutableList<String> createByOf = ImmutableList.of("a", "b");

  ImmutableList<String> createByCopyOf = ImmutableList.copyOf(normalList);
3、通过builder()方法创建

简单实验

  ImmutableList<String> createByBuilder = ImmutableList.<String>builder()
                .add("A")
                .add("B")
                .build();

未完待续......

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 这一篇文章我们来讲Google Guava集合,这是Guava最成熟和为人所知的部分。里面给我们提供各个集合...
    tuacy阅读 5,394评论 0 6
  • 目前Google Guava在实际应用中非常广泛,本篇博客将以博主对Guava使用的认识以及在项目中的经验来给大家...
    张丰哲阅读 15,355评论 12 184
  • 目前Google Guava在实际应用中非常广泛,本篇博客将以博主对Guava使用的认识以及在项目中的经验来给大家...
    java高并发阅读 1,666评论 0 8
  • 对象的创建与销毁 Item 1: 使用static工厂方法,而不是构造函数创建对象:仅仅是创建对象的方法,并非Fa...
    孙小磊阅读 2,031评论 0 3
  • 华夏阅读人格心理学,做自己的心理咨询师。wzp 按照弗洛伊德的性心理发展阶段,肛门期没发展好,放东西杂乱无章...
    爬行小王的故事阅读 2,447评论 0 0