前言
定义这些API是为了方便创建具有少量元素的集合和MAP实例,为了减轻Java编程语言中没有集合字面常量带来的痛苦1
List
JAVA中创建List的几种方法
JAVA中自带创建List方法还是比较多的,我总结一些常用的创建方法
1. 最简单的new ArrayList() 然后一个一个add
这种创建方式还是比较冗长的,这也是为什么会有新的创建List方法的原因
List<Integer> list1 = new ArrayList<>();
list1.add(1);
list1.add(2);
list1.add(3);
2. 使用Arrays.asList()创建不可变List
这种方法创建List看起来比较简洁,但是还是有些小坑的2
List<Integer> list2 = Arrays.asList(1, 2, 3, 4);
3. 使用 Collections.unmodifiableList创建不可变List
List<Integer> list3 = Collections.unmodifiableList(list1);
List<Integer> list4 = Collections.unmodifiableList(new ArrayList<Integer>() {
{
add(1); add(2); add(3);
}});
4. 使用Java8 Lambda创建不可变List
List<Integer> list5 = Collections.unmodifiableList(Stream.of(1,2,3,4).collect(Collectors.toList()));
5. 使用guava、hutool 第三方JAR包创建List
//guava 可变LIST
List<Integer> list6 = com.google.common.collect.Lists.newArrayList(1, 2, 3);
//guava 不可变LIST
List<Integer> list7 = com.google.common.collect.ImmutableList.of(1, 2, 3, 4);
//hutool
List<Integer> list8 = cn.hutool.core.collection.CollUtil.newArrayList(1, 2, 3, 4);
JDK9+新的创建不可变List方法
从JDK9开始就可以使用新的创建不可变List方法了,使用方法和Guava的一样
//JDK9+
List<Integer> newList = List.of(1,2,3,4);
从源码来看,用这个方法创建的集合 调用add()、addAll()、remove()、replaceAll()、set()、sort()都会抛出UnsupportedOperationException
static <E> List<E> of(E e1, E e2) {
return new ImmutableCollections.List12<>(e1, e2);
}
static final class List12<E> extends AbstractImmutableList<E> implements Serializable {
@Stable
private final E e0;
@Stable
private final Object e1;
List12(E e0) {
this.e0 = Objects.requireNonNull(e0);
// Use EMPTY as a sentinel for an unused element: not using null
// enables constant folding optimizations over single-element lists
this.e1 = EMPTY;
}
}
static abstract class AbstractImmutableList<E> extends AbstractImmutableCollection<E> implements List<E>, RandomAccess {
// all mutating methods throw UnsupportedOperationException
@Override public void add(int index, E element) {
throw uoe(); }
@Override public boolean addAll(int index, Collection<? extends E> c) {
throw uoe(); }
@Override public E remove(int index) {
throw uoe(); }
@Override public void replaceAll(UnaryOperator<E> operator) {
throw uoe(); }
@Override public E set(int index, E element) {
throw uoe(); }
@Override public void sort(Comparator<? super E> c) {
throw uoe(); }
}
Set
JAVA中创建Set的几种方法
JAVA中创建一个Set方法和List类似
1. 最简单的new HashSet()
Set<String> set = new HashSet<>();
set.add("a");
set.add("b");
set.add("c");
2. Collections.unmodifiableSet
Set<String> set2 = Collections.unmodifiableSet(set);
Set<String> set3 = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("a", "b", "c")));
Set<String> set4 = Collections.unmodifiableSet(new HashSet<String>() {
{
add("a"); add("b"); add("c");
}});
3. 使用guava、hutool 第三方JAR包创建Set
//guava不可变SET
Set<String> set5 = com.google.common.collect.ImmutableSet.of("a","b","c");
//guava可变SET
Set<String> set6 = com.google.common.collect.Sets.newHashSet("a","b","c");
//hutool
Set<String> list7 = cn.hutool.core.collection.CollUtil.newHashSet("a","b","c");
4. 使用Java8 Lambda创建不可变Set
Set<String> set8 = Collections.unmodifiableSet(Stream.of("a", "b", "c").collect(toSet()));
JDK9+新的创建不可变Set方法
Set<String> set9 = Set.of("a", "b", "c");
Map
JAVA常用创建Map方法
1. 最简单new HashMap()方法
Map<String, Integer> map1=new HashMap<>();
map1.put("a",1);
map1.put("b",2);
map1.put("c",3);
2. 使用Collections.unmodifiableMap
这里可以使用两个{ {}}匿名内部类的方式来实现HashMap的初始化
Map<String, Integer> map2 = Collections.unmodifiableMap(map1);
Map<String, Integer> map3 = Collections.unmodifiableMap(new HashMap <String,Integer>() {
{
put("a",1); put("b",2); put("c",3);
}});
3. 使用guava第三方JAR包创建Map
Map<String, Integer> map4= ImmutableMap.of("a",1,"b",2,"c",3);
JDK9+新的创建不可变Map方法
//类型guava的方式
Map<String, Integer> map5 = Map.of("a", 1, "b", 2, "c", 3);
//用entry
Map<String, Integer> map6 = Map.ofEntries(
Map.entry("a", 1),
Map.entry("b", 2),
Map.entry("c", 3)
);
总结
JDK9+后JAVA为我们提供了List.of()、Set.of()、Map.of()方法很方便、快速的创建字面常用的不可变集合,但是仅仅是为了方便创建而已,并不是提供了一套新的不可变集合类1,也不能带能性能的提升,所以想使用不可变集合来提升性能建议还是使用guava。