Collections工具类
针对集合操作的工具类
有对集合进行排序和二分查找的方法
Collection 是单列集合的顶层接口,有子接口List 和 Set
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) //随机置换
范例01:
package cn.itcast_01;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/*
* 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) //随机置换
*/
public class CollectionsDemo {
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);//[30, 20, 50, 10, 40]
//public static <T> void sort (List < T> list) //排序,默认排序
Collections.sort(list);
//System.out.println(list);//[10, 20, 30, 40, 50]
//public static <T> int binarySearch(List<?> list T key) //二分查找
int i = Collections.binarySearch(list, 30);
System.out.println(i);//返回结果:2
//public static <T> T max(Collection<?> coll) //最大值
System.out.println(Collections.max(list)); //返回结果:50
//public static void reverse(List<?>list) //反转
Collections.reverse(list);
System.out.println(list);//[50, 40, 30, 20, 10]
//public static void shuffle(List<?>list) //随机置换
Collections.shuffle(list);
System.out.println(list);
}
}
范例02:对自定义对象的排序
package cn.itcast_01;
/*
* Student
*/
public class Student implements Comparable<Student>{//实现Comparable接口
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) {////重写Comparable接口compareTo()方法
int num = this.age - s.age;
int num2 = num ==0 ? this.name.compareTo(s.name):num;
return num2;
}
}
package cn.itcast_01;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
/*
* Collections对自定义对象的排序
* Coleections可以对ArrayList存储基本包装类的元素排序
*/
public class CollectionsDemo02 {
public static void main(String[] args) {
//创建集合对象
List<Student> list = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("唐僧",25);
Student s2 = new Student("孙悟空",600);
Student s3 = new Student("猪八戒",300);
Student s4 = new Student("沙僧",150);
//添加对象
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
//自然排序
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());
}
}
}
练习01:模拟斗地主洗牌和发牌
package cn.itcast_02;
import java.util.ArrayList;
import java.util.Collections;
/*
* 模拟斗地主洗牌和发牌
* 思路:
* A / 创建一个牌盒(集合)
* B / 装牌
* C / 洗牌
* D / 发牌
* E / 看牌
*
*/
public class CollectionsTest_01 {
public static void main(String[] args) {
//创建牌合
ArrayList<String> array = new ArrayList<String>();
//装牌
//黑桃A,黑桃2,黑桃3...
//红桃A....
//梅花A....
//方块A....
//定义一个花色数组
String[] colors = {"♠","♥","♣","♦"};
//定义一个点数数组
String[] numbers = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
//拼接
for (String color : colors){
for (String number : numbers){
array.add(color.concat(number));
}
}
array.add("大王");
array.add("小王");
System.out.println(array);
//洗牌
Collections.shuffle(array);
System.out.println(array);
//发牌 (定义三个玩家)
ArrayList<String> fengQingYang = new ArrayList<String>();
ArrayList<String> zhouXingChi = new ArrayList<String>();
ArrayList<String> liuYi = new ArrayList<String>();
ArrayList<String> dipai = new ArrayList<String>();
for(int x = 0 ; x<array.size();x++){
if (x>=array.size()-3){
dipai.add(array.get(x));
}
else if (x%3==0){
fengQingYang.add(array.get(x));
}
else if (x%3 ==1){
zhouXingChi.add(array.get(x));
}
else if (x%3==2){
liuYi.add(array.get(x));
}
}
//调方法各人看牌
lookPoker("风清扬",fengQingYang);
lookPoker("周星驰",zhouXingChi);
lookPoker("刘毅",liuYi);
lookPoker("底牌",dipai);
}
//写个看牌的功能
public static void lookPoker(String name,ArrayList<String> array){
System.out.println(name+"的牌是:");
for (String s : array){
System.out.print(s+" ");
}
System.out.println();
}
}