工具类 | 集合工具类Collections介绍

前言

通过本文,你可以学习到 Collections 工具类的一些方法使用,在编码过程中都是非常实用的。

引入

CollectionsJDK 提供的工具类,不需要专门引入。

工具方法介绍

空集合

当一个方法的返回值是集合时,如果返回值为空时,建议不要返回 null,而是返回一个空集合。

但如果每次都新建一个 ArrayList 或者 HashMap,则会造成一定的内存浪费,CollectionsListSetMap 等集合类定义了相应的空集合常量。

// 空 List
Collections.emptyList();
// 空 Set
Collections.emptySet();
// 空 Map
Collections.emptyMap();

空集合使用的单独定义的数据结构,内部不存储数据,所以占用内存是最小的,但也存在一些限制:不可变更,新增、修改、删除元素都会导致异常。

一个元素的集合

在工作中经常会碰到这种情况:对方提供的接口需要的是一个集合,但我们只有一个元素,于是,我们只得创建一个集合,然后把这个元素加入集合再传过去。通常集合都会预留空间,以备后续还有元素可能加入,但对于只有一个元素的场景则造成了空间浪费,因为不会有后续元素加入。

// List
List<String> list = Collections.singletonList("Hello");
// Set
Set<String> set = Collections.singleton("Hello");
// Map
Map<String, Integer> map = Collections.singletonMap("Hello", 100);

与空集合一样,singleton 集合也使用的单独定义的数据结构,内部只存储一个元素,内存占用少,同样不可变更,新增、修改、删除元素都会导致异常。

不可变集合

Collections 工具类提供了一系列的 unmodifiableXXX 的方法,用于构建不可变集合,经过此类方法处理后的集合不可以进行新增、修改、删除元素操作,否则会抛出异常。

// List
List<String> list = Collections.unmodifiableList(l);
// Set
Set<String> set = Collections.unmodifiableSet(s);
// Map
Map<String, Integer> map = Collections.unmodifiableMap(m);

线程安全集合

Collections 工具类提供了一系列的 synchronizedXXX 的方法,用于构建线程安全集合,经过此类方法处理后的集合可以保证线程安全。但是,这种处理方法一般来说性能不高,应该优先选择集合类型中的线程安全处理类(比如:ConcurrentHashMap

// List
List<String> list = Collections.synchronizedList(l);
// Set
Set<String> set = Collections.synchronizedSet(s);
// Map
Map<String, Integer> map = Collections.synchronizedMap(m);

批量增加

public static void main(String[] args) {
    
    
    List<Integer> list = new ArrayList<>();
    Collections.addAll(list, 1, 2, 3);
    // 输出:[1,2,3]
    System.out.println(list);
}

填充

将集合中的所有元素填充为指定元素。

public static void main(String[] args) {
    
    
    List<Integer> list = new ArrayList<>();
    Collections.addAll(list, 1, 2, 3);
    Collections.fill(list, 10);
    // 输出:[10,10,10]
    System.out.println(list);
}

交换位置

public static void main(String[] args) {
    
    
    List<Integer> list = new ArrayList<>();
    Collections.addAll(list, 1, 2, 3);
    Collections.swap(list, 0, 1);
    // 输出:[2,1,3]
    System.out.println(list);
}

排序

public static void main(String[] args) {
    
    
    List<Integer> list = new ArrayList<>();
    Collections.addAll(list, 3, 2, 1);
    Collections.sort(list);
    // 输出:[1,2,3]
    System.out.println(list);
}

也可以使用自定义 Comparator 进行排序,比如,下例中使用字符串的长度进行排序。

public static void main(String[] args) {
    
    
    List<String> list = new ArrayList<>();
    Collections.addAll(list, "Hello", "Hi", "H");
    // 输出:[H, Hi, Hello]
    Collections.sort(list, Comparator.comparing(String::length));
    System.out.println(list);
}

直接使用 List.sort() 方法就可以了,Collections.sort 方法没有使用的必要。

逆序

public static void main(String[] args) {
    
    
    List<Integer> list = new ArrayList<>();
    Collections.addAll(list, 3, 2, 1);
    Collections.reverse(list);
    // 输出:[1,2,3]
    System.out.println(list);
}

随机排序

这个方法在测试的时候非常好用。使用 shuffle() 方法进行随机排序,发现每次输出的结果都不一样。

public static void main(String[] args) {
    
    
    List<Integer> list = new ArrayList<>();
    Collections.addAll(list, 3, 2, 1);
    Collections.shuffle(list);
    System.out.println(list);
}

猜你喜欢

转载自blog.csdn.net/gongm24/article/details/122456826