BaiFan
文章目录
  1. 1. CollectionUtil
    1. 1.1. 1. 使用指南
      1. 1.1.1. 1.1 声明引用
      2. 1.1.2. 1.2 判空
      3. 1.1.3. 1.3 示例

对Apache commons-collections工具包做了简单使用介绍。

CollectionUtil

1. 使用指南

对集合判定‘空’的操作使用Apache的commons-collection的工具包。

1.1 声明引用

Collection接口使用

1
import org.apache.commons.collections.CollectionUtils;

Map接口使用

1
import org.apache.commons.collections.MapUtils;

1.2 判空

Object满足以下任意条件即为empty

  1. Object为null
  2. Object调用自身的isEmpty()方法返回true。

1.3 示例

以下测试代码运行Test没有问题。

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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.junit.Assert;
import org.junit.Test;
/**
* 测试Empty
*
* @author bash
* @version V1.0
* @since 2015-11-12 10:50
*/
public class CollectionUtilSample {
@Test
public void testEmpty() {
Map<String, String> emptyMap = new HashMap<>();
List<String> emptyList = new ArrayList<>();
Set<String> emptySet = new HashSet<>();
Assert.assertTrue(MapUtils.isEmpty(emptyMap));
Assert.assertTrue(CollectionUtils.isEmpty(emptyList));
Assert.assertTrue(CollectionUtils.isEmpty(emptySet));
}
}

文章目录
  1. 1. CollectionUtil
    1. 1.1. 1. 使用指南
      1. 1.1.1. 1.1 声明引用
      2. 1.1.2. 1.2 判空
      3. 1.1.3. 1.3 示例