Collections是一个操作Set、List和Map等集合的工具类。
Collections中提供了大量方法对集合元素进行排序、查询和修改等操作,还提供了对集合对象设置不可变、对集合对象实现同步控制等方法。
排序操作:
- reverse(List):反转List中元素的顺序;
- shuffle(List):对List集合元素进行随机排序;
- sort(List):根据元素的自然顺序对指定List集合元素按升序排序;
- sort(List,Comparator):根据指定的Conparator产生的顺序对List集合进行排序;
- swap(List,int,int):将指定List集合中的i和j处的元素进行交换;
package collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test5 {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<Integer>();
list1.add(2);
list1.add(1);
list1.add(4);
list1.add(3);
System.out.println("原始的list1:" list1);
Collections.reverse(list1);
System.out.println("反转之后的list1:" list1);
Collections.sort(list1);
System.out.println("排序之后的list1:" list1);
Collections.shuffle(list1);
System.out.println("打乱之后的list1:" list1);
List<Student> list2 = new ArrayList<Student>();
Student s1 = new Student("tom",21);
Student s2 = new Student("jack",16);
Student s3 = new Student("bob",32);
list2.add(s1);
list2.add(s2);
list2.add(s3);
Collections.sort(list2, new Student());
System.out.println("按照年龄降序进行排序:");
for(Student stu:list2) {
System.out.println("name=" stu.name "," "age=" stu.age);
}
}
}
class Student implements Comparator<Student>{
String name;
int age;
public Student(){}
public Student(String name,int age){
this.name = name;
this.age = age;
}
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
if(o1.age>o2.age) {
return -1;
}else if (o1.age<o2.age) {
return 1;
}else {
return 0;
}
}
}
输出:
原始的list1:[2, 1, 4, 3] 反转之后的list1:[3, 4, 1, 2] 排序之后的list1:[1, 2, 3, 4] 打乱之后的list1:[1, 3, 2, 4] 按照年龄降序进行排序: name=bob,age=32 name=tom,age=21 name=jack,age=16
查找、替换操作:
- Object.max(Collection):根据元素的自然顺序返回指定集合中的最大元素;
- Object.max(Collection,Comparator):根据Comparator指定的顺序,返回指定集合中的最大元素;
- Object.min(Collection):根据元素的自然顺序返回指定集合中的最小元素;
- Object.max(Collection):根据Comparator指定的顺序,返回指定集合中的最小元素;
- int frequency(Collection,Object):返回指定集合中指定元素的出现次数;
- boolean replaceAll(List list,Object oldVal,object newVal):使用新值替换原来的值;
package collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Test5 {
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<Integer>();
list1.add(2);
list1.add(1);
list1.add(4);
list1.add(3);
list1.add(1);
System.out.println(list1);
System.out.println(Collections.max(list1));
System.out.println(Collections.min(list1));
System.out.println(Collections.frequency(list1,1));
System.out.println(Collections.replaceAll(list1, 1,2));
System.out.println(list1);
List<Student> list2 = new ArrayList<Student>();
Student s1 = new Student("tom",21);
Student s2 = new Student("jack",16);
Student s3 = new Student("bob",32);
list2.add(s1);
list2.add(s2);
list2.add(s3);
for(Student stu:list2) {
System.out.println("name=" stu.name "," "age=" stu.age);
}
System.out.println(Collections.max(list2,new Student()).name
"/" Collections.max(list2,new Student()).age);
System.out.println(Collections.min(list2,new Student()).name
"/" Collections.min(list2,new Student()).age);
// Collections.sort(list2, new Student());
// System.out.println("按照年龄降序进行排序:");
// for(Student stu:list2) {
// System.out.println("name=" stu.name "," "age=" stu.age);
// }
}
}
class Student implements Comparator<Student>{
String name;
int age;
public Student(){}
public Student(String name,int age){
this.name = name;
this.age = age;
}
@Override
public int compare(Student o1, Student o2) {
// TODO Auto-generated method stub
if(o1.age>o2.age) {
return 1;
}else if (o1.age<o2.age) {
return -1;
}else {
return 0;
}
}
}
输出:
[2, 1, 4, 3, 1] 4 1 2 true [2, 2, 4, 3, 2] name=tom,age=21 name=jack,age=16 name=bob,age=32 bob/32 jack/16
同步控制:Collections中提供了多个synchronizedXxx()方法,该方法可以使指定集合包装成线程同步的集合,从而可以解决多线程并发访问集合时线程安全问题。