Arrays 是用来操作数组的工具类, Objects 是用来操作 Object 的工具类
Collections 是用来操作 Collection 的工具类
addAll(Collection<? super T> c,T...elements): 批量的给 List/Set 添加元素
sort(List<T> list): 不能对 Set进行排序,Set有自己的排序依据
只能对 List排序,而且 要求 List里存储的元素必须要实现 Comparable 接口!
sort(List<T> list,Comparator comparator):
binarySearch(List<? extends Comparable<? super T>> list, T key):
binarySearch(List<? extends T> list, T key, Comparator<? super T> c):
reverse(List<?> list):
shuffle(List<?> list):
fill(List<? super T> list, T obj)
public class CollectionsDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
Collections.addAll(list, "hello", "good", "ok", "yes");
Collections.shuffle(list);
System.out.println(list);
List<Person> people = new ArrayList<>();
Collections.addAll(people,
new Person("张三", 18, 1.7, "上海"),
new Person("李四", 16, 1.72, "北京"),
new Person("王五", 20, 1.8, "南京"),
new Person("赵六", 19, 1.82, "天津")
);
// Collections.sort(people); 报错
Collections.sort(people, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
return o1.getAge() - o2.getAge();
}
});
List<Integer> nums = new ArrayList<>();
nums.add(4);
nums.add(6);
Collections.fill(nums, 10);
System.out.println(nums);
Set<String> set = new HashSet<>();
Collections.addAll(set, "hi", "you", "are", "she");
// Collections.sort(set);
}
}