Guava——Immutable Collections

Example

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

        final ImmutableSet 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 COLOR_NAMES = ImmutableSet.of(
                "1", "2", "3", "4", "5", "6");
        System.out.println("COLOR_NAMES = " + COLOR_NAMES);

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

        // use builder
        final ImmutableSet immutableSet3 = ImmutableSet.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

Guava——Immutable Collections_第1张图片

你可能感兴趣的:(Guava——Immutable Collections)