前面我们讲过Collections接口,它是集合类的顶级接口,提供对集合对象进行基本操作的通用接口方法。本节介绍的Collections类是一个工具类,用来对集合对象进行操作,提供对List、Map、Set等集合元素的排序、查询和修改等操作方法。
Collections类提供了一些静态方法,实现了基于List容器的一些常用算法,分别说明如下:
1、对List集合的排序操作
Collections类提供了对集合元素进行排序、反转方法。
● void sort(List)
该方法用于对List内的元素排序。
● void shuffle(List)
该方法用于对List内的元素进行随机排序。
● void reverse(List)
该方法用于对List内的元素进行逆序排序。
下面通过一个实例程序,通过Collections对List按照升序和降序排序。
(1)首先创建一个测试对象,新建一个UserInfoBean类,并实现Comparable接口。代码如下:
package com.milihua.collectionsdemo;
public class UserInfoBean implements Comparable<UserInfoBean>{
private int id;
private String name;
private int age;
public UserInfoBean(int id, String name, int age)
{
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
@Override
public int compareTo(UserInfoBean o) {
// TODO Auto-generated method stub
if (this.id < o.id)
return -1;
else if (this.id > o.id)
return 1;
else
return 0;
}
@Override
public String toString()
{
return "UserInfoBean [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}(2)创建主运行类,实例化List对象,并添加三个UserInfoBean对象,代码如下:
package com.milihua.collectionsdemo;
import java.util.ArrayList;
import java.util.List;
public class CollectionsDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<UserInfoBean> userInfos = new ArrayList<UserInfoBean>();
userInfos.add(new UserInfoBean(2, "张三", 10));
userInfos.add(new UserInfoBean(1, "李四", 25));
userInfos.add(new UserInfoBean(3, "王五", 40));
for (int i = 0; i < userInfos.size(); i++)
{
System.out.println(userInfos.get(i).toString());
}
}
}程序输出结果如下图所示:

图 14-28 CollectionsDemo输出结果
从上图输出结果可以看出,userInfos按照添加的顺序输出,我们希望userInfos能够按照UserInfoBean对象的id属性进行排序。
(3)在主运行类中,添加排序代码,按照升序对userInfos排序,代码如下:
package com.milihua.collectionsdemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<UserInfoBean> userInfos = new ArrayList<UserInfoBean>();
userInfos.add(new UserInfoBean(2, "张三", 10));
userInfos.add(new UserInfoBean(1, "李四", 25));
userInfos.add(new UserInfoBean(3, "王五", 40));
// 添加排序代码
System.out.println("升序:");
Collections.sort(userInfos);
for (int i = 0; i < userInfos.size(); i++)
{
System.out.println(userInfos.get(i).toString());
}
}
}在主运行类中,添加了排序代码,调用Collections类的sort方法对userInfos按照升序排序,因为Collections类提供的都是静态方法,因此可以不用实例化,而通过类名直接调用sort方法。程序输出结果如下图所示:

图 14-29 userInfos升序输出结果
从上图输出结果可以看出,userInfos按照UserInfoBean对象的id属性值升序输出。
(4)在主运行类中,添加排序代码,按照降序对userInfos排序,如果需要降序输出,则可以调用Collections类的sort方法后,再调用Collections类的reverse方法反转userInfos的元素。代码如下:
package com.milihua.collectionsdemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<UserInfoBean> userInfos = new ArrayList<UserInfoBean>();
userInfos.add(new UserInfoBean(2, "张三", 10));
userInfos.add(new UserInfoBean(1, "李四", 25));
userInfos.add(new UserInfoBean(3, "王五", 40));
// 添加排序代码
System.out.println("降序:");
Collections.sort(userInfos);
Collections.reverse(userInfos);
for (int i = 0; i < userInfos.size(); i++)
{
System.out.println(userInfos.get(i).toString());
}
}
}在主运行类中,在Collections类的sort方法后面,又调用了Collections类的reverse方法,反转userInfos,实现降序排序。程序输出结果如下图所示:

图 14-30 userInfos降序输出结果
2、对List集合元素的查找操作
Collections类提供了对集合元素进行二分查找、求最大值和最小值的方法。
● void max (List)
该方法用于找出List集合的最大元素。
● void min(List)
该方法用于找出List集合的最小元素。
● void binarySearch(List,Object)
该方法应用二分查找法返回元素在List中的位置。
结合上面的实例程序,下面给出上述方法的用法。代码如下:
package com.milihua.collectionsdemo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CollectionsDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<UserInfoBean> userInfos = new ArrayList<UserInfoBean>();
userInfos.add(new UserInfoBean(2, "张三", 10));
userInfos.add(new UserInfoBean(1, "李四", 25));
userInfos.add(new UserInfoBean(3, "王五", 40));
// 添加排序代码
System.out.println("升序:");
Collections.sort(userInfos);
Collections.reverse(userInfos);
for (int i = 0; i < userInfos.size(); i++)
{
System.out.println(userInfos.get(i).toString());
}
//求最大值
UserInfoBean userInfo_max = Collections.max(userInfos);
System.out.println("最大值为:" + userInfo_max);
//求最小值
UserInfoBean userInfo_min = Collections.min(userInfos);
System.out.println("最小值为:" + userInfo_min);
//二分查找
UserInfoBean tempUserInfo = new UserInfoBean(2, "张三", 10);
int index = Collections.binarySearch(userInfos, tempUserInfo);
System.out.println("查找到索引位置" + index);
}
}程序分别调用Collections的max、min方法找出userInfos集合中的最大和最小元素,调用binarySearch方法查找tempUserInfo对象是否在userInfos集合中有相同的元素。注意,在调用binarySearch方法之前,一定要先对userInfos排序,否则查找结果不正确。程序输出结果如下图所示:

图 14-31 查找实例输出结果
3、List集合元素的复制操作
Collections类提供了对集合的复制操作,可以将原集合中的内容复制到新的集合中。
● void copy (List dest, List src)
该方法将src的内容复制到dest中。
复制方法应用非常简单,其用法可以参考前面的调用方法代码,在这里就不给出示例代码了。
■ 知识点拨
Collection类是应用工具类,在集合操作中应用非常广泛。当程序需要对集合进行排序、复制、查找等操作时,可以使用Collection类完成对集合的操作。