Guava提供了ImmutableList, ImmutableSet,ImmutableMap等,下面以ImmutableList为例看看有哪些创建方法。
of方法
直接提供Element,最多12个参数。
ImmutableList<Integer> list = ImmutableList.of(1, 2, 3);
copyOf方法
从一个List获取Element,生成爽哦ImmutableList。
// Integer[] nums1 = {1, 2, 3};
List<Integer> nums2 = Lists.newArrayList(1, 2, 3);
ImmutableList<Integer> list = ImmutableList.copyOf(nums2);
builder –> build
创建一个builder,然后添加元素,最后build。
ImmutableList.<Integer>builder()
    .add(1)
    .add(2)
    .add(3)
    .build();