1、Collections类概述
针对集合操作 的工具类,都是静态方法
2、Collections成员方法
public static <T> void sort(List<T> list):排序 默认情况下是自然顺序。
public static <T> int binarySearch(List<?> list,T key):二分查找
public static <T> T max(Collection<?> coll):最大值
public static void reverse(List<?> list):反转
public static void shuffle(List<?> list):随机置换
例子1:存储基本包装类
package collectiondemos;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
/**
* Created by gao on 15-12-22.
*/
public class CollectionsDemo01 {
public static void main(String[] args) {
// 创建集合对象
List<Integer> list = new ArrayList<Integer>();
// 添加元素
list.add(30);
list.add(20);
list.add(50);
list.add(10);
list.add(40);
System.out.println("list:" + list); //list:[30, 20, 50, 10, 40]
// public static <T> void sort(List<T> list):排序 默认情况下是自然顺序。
//Collections.sort(list);
//System.out.println("list:" + list); //list:[10, 20, 30, 40, 50]
// public static <T> int binarySearch(List<?> list,T key):二分查找
System.out.println("binarySearch:" + Collections.binarySearch(list, 30)); //binarySearch:2
System.out.println("binarySearch:" + Collections.binarySearch(list, 300)); //binarySearch:-6
// public static <T> T max(Collection<?> coll):最大值
System.out.println("max:" + Collections.max(list)); //max:50
// public static void reverse(List<?> list):反转
// Collections.reverse(list);
// System.out.println("list:"+list); //list:[40, 10, 50, 20, 30]
//public static void shuffle(List<?> list):随机置换
Collections.shuffle(list);
System.out.println("list:" + list); //list:[20, 50, 10, 30, 40]
}
}
例子2:存储自定义对象
学生类:
package collectiondemos;
public class Student implements Comparable<Student>{
private String name;
private int age;
public Student() {
super();
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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(Student s) {
int num = this.age - s.getAge();
int num2 = num == 0 ? this.name.compareTo(s.getName()) : num;
return num2;
}
}
测试类:
package collectiondemos;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/**
* Created by gao on 15-12-22.
*/
public class CollectionsDemo02 {
public static void main(String[] args) {
// 创建集合对象
List<Student> list = new ArrayList<Student>();
// 创建学生对象
Student s1 = new Student("林青霞", 27);
Student s2 = new Student("风清扬", 30);
Student s3 = new Student("刘晓曲", 28);
Student s4 = new Student("武鑫", 29);
Student s5 = new Student("林青霞", 27);
// 添加元素对象
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
// 排序
// 方式一:自然排序
// Collections.sort(list);
// 方式二:比较器排序
// 如果同时有自然排序和比较器排序,以比较器排序为主
Collections.sort(list, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s2.getAge() - s1.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
return num2;
}
});
// 遍历集合
for (Student s : list) {
System.out.println(s.getName() + "---" + s.getAge());
}
}
}
输出结果:
风清扬---30
武鑫---29
刘晓曲---28
林青霞---27
林青霞---27