gpt4 book ai didi

Java中Collections.emptyList()的注意事项

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 29 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章Java中Collections.emptyList()的注意事项由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

偶然发现有小伙伴错误地使用了Collections.emptyList()方法,这里记录一下。她的使用方式是:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void run() {
   ......
   List list = buildList(param);
   ......
   Object newNode = getNode(...);
   list.add(newNode);
   ......
}
 
public List buildList(Object param) {
   if (isInValid(param)) {
     return Collections.emptyList();
   } else {
     ......
   }
}

buildList方法中可能会返回一个"空的List",后续还可能往这个List添加元素(或者移除元素),但是没有注意Collections.emptyList方法返回的是一个EMPTY_LIST:

?
1
2
3
public static final <T> List<T> emptyList() {
   return (List<T>) EMPTY_LIST;
}

它是一个static final修饰的成员变量,是一个EmptyList类的实例:

?
1
public static final List EMPTY_LIST = new EmptyList<>();

这个EmptyList是一个静态内部类,和ArrayList一样继承自AbstractList:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
private static class EmptyList<E>
     extends AbstractList<E>
     implements RandomAccess, Serializable {
     private static final long serialVersionUID = 8842843931221139166L;
 
     public Iterator<E> iterator() {
       return emptyIterator();
     }
     public ListIterator<E> listIterator() {
       return emptyListIterator();
     }
 
     public int size() { return 0 ;}
     public boolean isEmpty() { return true ;}
 
     public boolean contains(Object obj) { return false ;}
     public boolean containsAll(Collection<?> c) { return c.isEmpty(); }
 
     public Object[] toArray() { return new Object[ 0 ]; }
 
     public <T> T[] toArray(T[] a) {
       if (a.length > 0 )
         a[ 0 ] = null ;
       return a;
     }
 
     public E get( int index) {
       throw new IndexOutOfBoundsException( "Index: " +index);
     }
 
     public boolean equals(Object o) {
       return (o instanceof List) && ((List<?>)o).isEmpty();
     }
 
     public int hashCode() { return 1 ; }
 
     // Preserves singleton property
     private Object readResolve() {
       return EMPTY_LIST;
     }
   }

可以看到这个EmptList没有重写add方法,并且get方法也是直接抛出一个IndexOutOfBoundsException异常。既然没有重写add方法,那么看看父类AbstractList中的add方法:

?
1
2
3
4
5
6
7
8
public boolean add(E e) {
   add(size(), e);
   return true ;
}
 
public void add( int index, E element) {
   throw new UnsupportedOperationException();
}

可以看到直接抛出的UnsupportedOperationException异常。再回到EmptyList类中,它对外提供的一些方法也很明显地限制了它的使用范围.

对于Collections.emptyList(),或者说Collections.EMPTY_LIST,最好只把它当做一个空列表的标识(可以想象成一个frozen过的空List),不要对其做一些增删改查的操作。如果程序中的一些分支逻辑返回了这种实例,测试的时候又没有覆盖到,在生产环境如果走到了这个分支逻辑,那就麻烦了~ 。

总结 。

到此这篇关于Java中Collections.emptyList()注意事项的文章就介绍到这了,更多相关Java Collections.emptyList()注意内容请搜索我以前的文章或继续浏览下面的相关文章希望大家以后多多支持我! 。

原文链接:https://blog.csdn.net/huangzhilin2015/article/details/115181718 。

最后此篇关于Java中Collections.emptyList()的注意事项的文章就讲到这里了,如果你想了解更多关于Java中Collections.emptyList()的注意事项的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com