Collections
Collections
将所有指定的元素添加到指定的集合
static<T> addAll(Collection<? super T> c,T... elements)
//将所有指定的元素添加到指定的集合
Array<String> arr = new ArrayList<>();
Collections.addAll(arr,"a","b","c");
排序
static<T> addAll(Collection<? super T> c,T... elements)
//将所有指定的元素添加到指定的集合
ArrayList<String> arr = new ArrayList<>();
Collections.addAll(arr,"a","b","c");
Collections.sort(arr);
//排序
System.out.println(arr);
ArrayList<Person> array=new ArrayList<>();
array.add(new Person("张三",18));
array.add(new Person("李四",15));
array.add(new Person("王五",20);
//自定义排序规则
Collections.sort(array, newComparator<Person>(){
@Override
public int compare(Person o1, Person o2){
return o2.getAge()-o1.getAge();
}
});
System.out.println(array);
ArrayList<String> array=new ArrayList<>();
array.add("z");
array.add("a");
array.add("t");
//根据哈希值排序
Collections.sort(array, newComparator<String>(){
@Override
public int compare(String o1,String o2){
return o1.hashCode()-o2.hashCode();
}
});
System.out.println(array);