Guava——Immutable Collections

Example

public static void testImmutableCollect(){
        final ImmutableSet<String> COLOR_NAMES = ImmutableSet.of(
                "1", "2", "3", "4", "5", "6");
        System.out.println("COLOR_NAMES = " + COLOR_NAMES);

        final ImmutableSet<String> immutableSet2 = ImmutableSet.copyOf(COLOR_NAMES);

        System.out.println("immutableSet2 = " + immutableSet2);
    }

Why?

Immutable objects have many advantages, including:

  • Safe for use by untrusted libraries.
  • Thread-safe: can be used by many threads with no risk of race conditions.
  • Doesn't need to support mutation, and can make time and space savings with that assumption. All immutable collection implementations are more memory-efficient than their mutable siblings.
  • Can be used as a constant, with the expectation that it will remain fixed.

The JDK provides Collections.unmodifiableXXX methods, but in our opinion, these can be

  • unwieldy and verbose; unpleasant to use everywhere you want to make defensive copies
  • unsafe: the returned collections are only truly immutable if nobody holds a reference to the original collection
  • inefficient: the data structures still have all the overhead of mutable collections, including concurrent modification checks, extra space in hash tables, etc.

When you don't expect to modify a collection, or expect a collection to remain constant, it's a good practice to defensively copy it into an immutable collection.

Important: Each of the Guava immutable collection implementations rejects null values. We did an exhaustive study on Google's internal code base that indicated that null elements were allowed in collections about 5% of the time, and the other 95% of cases were best served by failing fast on nulls. If you need to use null values, consider using Collections.unmodifiableList and its friends on a collection implementation that permits null

How?

// use of
        final ImmutableSet<String> COLOR_NAMES = ImmutableSet.of(
                "1", "2", "3", "4", "5", "6");
        System.out.println("COLOR_NAMES = " + COLOR_NAMES);

        // use copyOf
        final ImmutableSet<String> immutableSet2 = ImmutableSet.copyOf(COLOR_NAMES);
        System.out.println("immutableSet2 = " + immutableSet2);

        // use builder
        final ImmutableSet<String> immutableSet3 = ImmutableSet.<String>builder()
                .addAll(COLOR_NAMES)
                .add("a", "b", "c").build();
        System.out.println("immutableSet3 = " + immutableSet3);

asList

All immutable collections provide an ImmutableList view via asList(), so -- for example -- even if you have data stored as an ImmutableSortedSet, you can get the kth smallest element with sortedSet.asList().get(k).

The returned ImmutableList is frequently -- not always, but frequently -- a constant-overhead view, rather than an explicit copy. That said, it's often smarter than your average List -- for example, it'll use the efficient contains methods of the backing collection.

List

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

推荐阅读更多精彩内容