好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java空集合使用场景与填坑记录

前言

今天学学java中如何创建一个空集合以及空集合的一些 使用场景 和相关的坑。开始之前,我们先来看一下java判断集合是否为空

list.isempty() list.size()==0 list==null的区别:

1. isempty()方法是用来判断集合中有没有元素

2. size()方法是判断集合中的元素个数

3. isempty()和size()==0意思一样,没有区别,通用。

4. if(list ==null)是判断有没有这个集合

在我们判断集合是否为空的时候这样写就万无一失:

?

1

2

3

4

5

6

7

list<string> list = new arraylist<>();

 

if (list!= null &&!list.isempty()){

 

//走集合不为空的逻辑

 

}

下面开始本文的正文,你可能会问,这好像没有什么好讲的,空集合不就是new一个嘛,也就是像new arraylist<string>()这样创建一个不久行了吗?其实这也是一种创建空集合的方法,但今天小编讲下通过另外一种方式创建空集合,以及两种方式之间的差异。

一、通过collections.emptylist()创建空集合

java集合工具类中提供了一系列创建集合的静态方法,其中包括创建线程同步相关的collections.synchronizedxxx()方法、空集合相关的collections.emptyxxx()方法。通过这种方式创建的空集合,既然是空的,就不允许你往集合中添加元素和删除元素,也就是不能调用相应add()和remove()方法,我先来看看collections类创建空集合的部分源代码:

?

1

2

3

4

5

6

7

public static final list empty_list = new emptylist<>();

 

......

 

public static final <t> list<t> emptylist() {

  return (list<t>) empty_list;

}

你会发现上面的emptylist()方法默认返回的是前面的静态变量empty_list,你可能会说,既然empty_list是static的,那我直接通过collections.empty_list获取不就好了,没错,这样做也可以,只不过在某些需要泛型的场景下,调用emptylist()方法提供了相应的泛型支持。

那为什么这种方式不能添加和移除元素呢?我们来看看emptylist内部类是怎么定义的:

?

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

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

// 继承自abstractlist抽象类

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 ; }

 

  @override

  public boolean removeif(predicate<? super e> filter) {

   objects.requirenonnull(filter);

   return false ;

  }

  @override

  public void replaceall(unaryoperator<e> operator) {

   objects.requirenonnull(operator);

  }

  @override

  public void sort(comparator<? super e> c) {}

 

  // override default methods in collection

  @override

  public void foreach(consumer<? super e> action) {

   objects.requirenonnull(action);

  }

 

  @override

  public spliterator<e> spliterator() { return spliterators.emptyspliterator(); }

  // preserves singleton property

  private object readresolve() {

   return empty_list;

  }

}

从上面的源代码中我们可以发现emptylist类并没有重写父类相应的add()或者remove()方法,那么当调用空集合的add()方法时将默认调用abstractlist的add()方法,行,那么我们来看看父类abstractlist的add()方法是怎么实现的:

?

1

2

3

public void add( int index, e element) {

  throw new unsupportedoperationexception();

}

?

1

2

3

public e remove( int index) {

  throw new unsupportedoperationexception();

}

很遗憾,父类直接给你抛出unsupportedoperationexception异常,所以,小编认为,通过collections创建的空集合不能添加或删除元素也是合情合理的,因为是空集合嘛,空,那为啥还要有添加删除操作。下面说说这种方式的使用场景。

二、简单使用场景

web开发中经常使用rest + json的技术组合来进行前后端交互,那么当前端调用一个接口时,接口有可能需要返回一个空的集合给到前端,比如你根据某个条件查数据库得不到数据时,那么此时collections.emptyxxx()就非常合适了,因为使用new arraylist()的初始化还会占用相关的资源。

为了说明调用add()方法会抛出异常,下面写个小测试:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

public class removeiftest {

  private static list<object> list = collections.emptylist();

  public static void main(string[] args) {

  

   list.add( "one1" );

   list.add( "one2" );

   list.add( 1 );

   list.add( 2 );

   list.add( new object());

  

   system.err.println(arrays.tostring(list.toarray()));

  }

}

复制代码程序输出:

exception in thread "main" java.lang.unsupportedoperationexception
    at java.util.abstractlist.add(unknown source)
    at java.util.abstractlist.add(unknown source)
    at com.example.removeiftest.main(removeiftest.java:17)

三、总结

总的来说,对于如何创建空集合的问题我们不需要纠结,重要的我们要记住通过collections.emptyxxx()创建的空集合不能执行添加删除操作以及其中的原理,避免以后犯错,不过其实即使你使用错了,调试几遍你的代码估计也就会把问题发现出来,只不过这篇文章能帮你省去这个发现bug的过程啦!

好了,以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对的支持。

原文链接:https://juejin.im/post/5b29fdd16fb9a00e65266550

查看更多关于Java空集合使用场景与填坑记录的详细内容...

  阅读:48次