Collection 是集合的接口, 而Collections 是一个集合的操作工具类.在这个类中里面提供有集合的基础操作: 如 反转, 排序等.
范例: 创建空集合(可以作为标记)
package com.beyond.nothing;
import java.util.Collections;
import java.util.List;
public class test {
public static void main(String[] args) throws Exception {
List<String> all = Collections.EMPTY_LIST;
all.add("A");
}
}
范例: 利用Collections 进行集合操作
package com.beyond.nothing;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class test {
public static void main(String[] args) throws Exception {
List<String> all = new ArrayList<>();
Collections.addAll(all, "A","B","C");
System.out.println(all);
Collections.reverse(all);
ArrayList<String> list = new ArrayList<>();
list.add("");
list.add("");
list.add("");
Collections.copy(list, all);
System.out.println(all);
System.out.println(list);
}
}
