package cd_one.code16.exer2;
import org.junit.Test;
import java.util.*;
public class CollectionTest {
@Test
public void test(){
List list = Arrays.asList(45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23);
System.out.println(list);
//[45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23]:
Collections.reverse(list);
System.out.println(list);
//[23, 34, 56, 45, 32, 2, 43, 6, 65, 43, 45]:反转
Collections.shuffle(list);
System.out.println(list);
//[43, 43, 45, 65, 34, 6, 23, 2, 56, 45, 32]随机排序
Collections.sort(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Integer && o2 instanceof Integer){
Integer i1 = (Integer) o1;
Integer i2 = (Integer) o2;
return (i1.intValue() - i2.intValue());
}
throw new RuntimeException("类型不匹配");
}
});
System.out.println(list);
//[2, 6, 23, 32, 34, 43, 43, 45, 45, 56, 65]
// [65, 56, 45, 45, 43, 43, 34, 32, 23, 6, 2]
}
@Test
public void test2(){
List list = Arrays.asList(45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23);
System.out.println(list);
Object max = Collections.max(list);
Object max1 = Collections.max(list, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
if(o1 instanceof Integer && o2 instanceof Integer){
Integer i1 = (Integer) o1;
Integer i2 = (Integer) o2;
return -(i1.intValue() - i2.intValue());
}
throw new RuntimeException("类型不匹配");
}
});
System.out.println(max);//65
System.out.println(max1);//2
int count = Collections.frequency(list,45);
System.out.println("45出现了"+ count +"次");
}
@Test
public void test3(){
List src = Arrays.asList(45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23);
List dest = Arrays.asList(new Object[src.size()]);
Collections.copy(dest,src);
System.out.println(dest);//[45, 43, 65, 6, 43, 2, 32, 45, 56, 34, 23]
}
@Test
public void test4(){
List list1 = new ArrayList();
list1.add(34);
list1.add(12);
list1.add(45);
List list2 = Collections.unmodifiableList(list1);
System.out.println(list2.get(0));//34
}
@Test
public void test5(){
List list1 = new ArrayList<>();
List list2 = Collections.synchronizedList(list1);
list2.add(123);
HashMap map1 = new HashMap<>();
Map map2 = Collections.synchronizedMap(map1);
}
}
排序
package cd_one.code16.exer2;
import java.util.ArrayList;
import java.util.Collections;
public class PokerTest {
public static void main(String[] args) {
//1.组成一幅扑克牌
String[] num = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
String[] color = {"方片◆","梅花▲","红桃●","黑桃★"};
ArrayList poker = new ArrayList<>();
for (int i = 0; i < color.length; i++) {
for (int j = 0; j < num.length; j++) {
poker.add(color[i] + " " + num[j]);
}
}
//添加大小王
poker.add("小王");
poker.add("大王");
//2.洗牌
Collections.shuffle(poker);
System.out.println(poker);
System.out.println();
//3.发牌
//3.1创建3个角色和1个底牌对应的4个ArrayList
ArrayList tom = new ArrayList<>();
ArrayList jerry = new ArrayList<>();
ArrayList me = new ArrayList<>();
ArrayList lastCards = new ArrayList<>();
for (int i = 0; i < poker.size(); i++) {
if(i >= poker.size() - 3){
lastCards.add(poker.get(i));
}
if(i % 3 == 0){
tom.add(poker.get(i));
} else if (i % 3 == 1) {
jerry.add(poker.get(i));
} else if (i % 3 == 2) {
me.add(poker.get(i));
}
}
// 3.2遍历显示4个ArrayList
System.out.print("Tom:");
System.out.print(tom+"\n");
System.out.println();
System.out.print("Jerry:");
System.out.print(jerry+"\n");
System.out.println();
System.out.print("Me:");
System.out.print(me+"\n");
System.out.println();
System.out.print("LastCards:");
System.out.print(lastCards);
}
}