Collections
1. 概述
- 注意区别Collection:是单列集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素
- 由静态方法组成
- 是针对集合操作的工具类
2. 常用方法
方法名 | 说明 |
---|
public static <T extends Comparable<? super T>> void sort(List list) | 将指定的列表按升序排序 |
public static void reverse(List<?> list) | 反转指定列表中元素的顺序 |
public static void shuffle(List<?> list) | 使用默认的随机源随机排列指定的列表 |
2.1 sort()
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Demo {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(30);
list.add(20);
list.add(50);
list.add(10);
System.out.println(list);
Collections.sort(list);
System.out.println(list);
}
}
2.2 reverse()
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Demo {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(30);
list.add(20);
list.add(50);
list.add(10);
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
}
}
2.3 shuffle()
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Demo {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(30);
list.add(20);
list.add(50);
list.add(10);
System.out.println(list);
Collections.shuffle(list);
System.out.println(list);
}
}
3. 案例
3.1 ArrayList存储学生对象并排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
ArrayList<Student> array = new ArrayList<>();
Student s1 = new Student("zhangsan", 16);
Student s2 = new Student("lisi", 17);
Student s3 = new Student("wangwu", 18);
Student s4 = new Student("zhaoliu", 19);
Student s5 = new Student("heqi", 17);
array.add(s1);
array.add(s2);
array.add(s3);
array.add(s4);
array.add(s5);
Collections.sort(array, new Comparator<Student>() {
@Override
public int compare(Student s1, Student s2) {
int num = s1.getAge() - s2.getAge();
int num2 = num == 0 ? s1.getName().compareTo(s2.getName()) : num;
return num2;
}
});
for (Student s : array) {
System.out.println(s.getName() + "," + s.getAge());
}
}
}
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
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;
}
}
3.2 模拟斗地主发牌、洗牌、看牌
import java.util.ArrayList;
import java.util.Collections;
public class Test01 {
public static void main(String[] args) {
ArrayList<String> array = new ArrayList<>();
String[] color = {"♦","♠","♥","♣"};
String[] number = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
for (String s : color) {
for (String s1 : number) {
String s2 = s + s1;
array.add(s2);
}
}
array.add("大王");
array.add("小王");
Collections.shuffle(array);
ArrayList<String> wx = new ArrayList<>();
ArrayList<String> hj = new ArrayList<>();
ArrayList<String> xlj = new ArrayList<>();
ArrayList<String> dp = new ArrayList<>();
for (int i = 0; i < array.size(); i++) {
String poker = array.get(i);
if (i>=array.size()-3){
dp.add(poker);
}else if (i%3 == 0){
wx.add(poker);
}else if (i%3 == 1){
hj.add(poker);
}else if (i%3 == 2){
xlj.add(poker);
}
}
lookpoker("王",wx);
lookpoker("何",hj);
lookpoker("辣鸡",xlj);
lookpoker("底牌",dp);
}
public static void lookpoker(String name,ArrayList<String> array){
System.out.println(name + "的牌是:");
for (String s : array) {
System.out.print(s + " ");
}
System.out.println();
}
}
3.2 模拟斗地主Plus
import java.util.*;
public class TestPlus01 {
public static void main(String[] args) {
HashMap<Integer, String> hm = new HashMap<>();
ArrayList<Integer> array = new ArrayList<>();
String[] color = {"♦","♠","♥","♣"};
String[] number = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
int index = 0;
for (String s : number) {
for (String s1 : color) {
String s2 = s1 + s;
hm.put(index,s2);
array.add(index);
index++;
}
}
hm.put(index,"小王");
array.add(index);
index++;
hm.put(index,"大王");
array.add(index);
TreeSet<Integer> wx = new TreeSet<Integer>();
TreeSet<Integer> hj = new TreeSet<Integer>();
TreeSet<Integer> xlj = new TreeSet<Integer>();
TreeSet<Integer> dp = new TreeSet<Integer>();
Collections.shuffle(array);
for (int i = 0; i < array.size(); i++) {
Integer key = array.get(i);
if (i>= array.size()-3){
dp.add(key);
}else if(i%3==0){
wx.add(key);
}else if(i%3==1){
hj.add(key);
}else if(i%3==2){
xlj.add(key);
}
}
lookpoker("王",wx,hm);
lookpoker("何",hj,hm);
lookpoker("辣鸡",xlj,hm);
}
public static void lookpoker(String name,TreeSet<Integer> ts,HashMap<Integer, String> hm){
System.out.println(name + "的牌是:");
for (Integer key : ts) {
String poker = hm.get(key);
System.out.print(poker + " ");
}
System.out.println();
}
}