接下來讓我們看看collections和collection的區別以及collections的一些簡單方法的使用:
Collection和Collections的區別:
Collection:頂層次單列集合的根接口,它是一個集合,是一個接口
Collections:是針對集合操作的工具類,有一些功能:隨機置換,集合裏面的二分查找,將集合的元素進行反轉
Collections的一些簡單功能:
集合的二分查找
public static <T> int binarySearch(List<T> list, T key)
static T max(Collection coll):獲取集合中的最大值
public static void reverse(List<?> list):將集合中的元素順序反轉
public static void shuffle(List<?> list):將集合中的元素打亂
public static void sort(List<T> list)
例如:
package com.westos.collections;
import java.util.ArrayList;
import java.util.Collections;
/*
* 集合的二分查找
* public static <T> int binarySearch(List<T> list, T key)
* static T max(Collection coll):獲取集合中的最大值
* public static void reverse(List<?> list):將集合中的元素順序反轉
* public static void shuffle(List<?> list):將集合中的元素打亂
* public static void sort(List<T> list)
*
*
*/
public class collectionsDome {
public static void main(String[] args) {
//創建集合對象
ArrayList<Integer> list=new ArrayList<Integer>();
//向集合中添加元素
list.add(11);
list.add(22);
list.add(33);
list.add(44);
list.add(55);
System.out.println(list);
//調用collections集合中的二分查找方法,前提是集合中的元素必須是有序的
int index = Collections.binarySearch(list,44);
System.out.println("index:"+index);
//獲取集合中的最大值
Integer max = Collections.max(list);
System.out.println("max:"+max);
//反轉集合中的元素
Collections.reverse(list);
System.out.println("reverse:"+list);
//打亂集合中的元素
Collections.shuffle(list);
System.out.println("shuffle:"+list);
Collections.sort(list);
System.out.println("sort:"+list);
}
}
運行結果:
[11, 22, 33, 44, 55]
index:3
max:55
reverse:[55, 44, 33, 22, 11]
shuffle:[44, 55, 33, 11, 22]
sort:[11, 22, 33, 44, 55]
Collections的sort方法還可以對自定義類進行自然排序和比較器排序的兩種方法
例如:
先自定義一個學生類:
package com.westos.collections;
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=s.getAge()-this.getAge();
int num2=num==0?this.getName().compareTo(s.getName()):num;
return num2;
}
}
在定義一個測試類:
package com.westos.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class collectionDome2 {
public static void main(String[] args) {
//創建ArrayList集合對象
ArrayList<Student> list=new ArrayList<Student>();
//創建學生類對象
Student s1=new Student("迪麗熱巴",25);
Student s2=new Student("吳宣儀",23);
Student s3=new Student("孟美岐",23);
Student s4=new Student("程瀟",22);
Student s5=new Student("蕭薰兒",21);
Student s6=new Student("蕭薰兒",21);
//將學生對象添加到集合中
list.add(s1);
list.add(s2);
list.add(s3);
list.add(s4);
list.add(s5);
list.add(s6);
//自然排序
Collections.sort(list);
//比較器排序(習慣用比較器排序)
Collections.sort(list, 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 st:list) {
System.out.println(st.getName()+st.getAge());
}
}
}
運行結果:
蕭薰兒21
蕭薰兒21
程瀟22
吳宣儀23
孟美岐23
迪麗熱巴25
在上述代碼中,使用排序的話我們一般習慣比較器排序,不過根據自己的習慣來定,另外在上述代碼中我們可以看到兩種排序方法都在運行,但是運行結果會依照哪一種方法來運行呢?
他會根據你的排序的書序,當你把自然排序放在比較器排序的前面,系統就會依照自然排序來運行,反之則會按照比較器排序來運行,所以我們一般只寫一種排序方法
模擬鬥地主遊戲發牌洗牌:
思路分析:
1)先創建一個牌盒集合(ArrayList<Integer>)
2)創建顏色數組和牌號數組,將他們拼接在一起,添加到牌盒結合裏
3)創建大小王,然後一起添加到牌盒集合中
4)運用collections的shuffle()方法打亂牌盒集合中的元素
5)創建三個玩家的集合和底牌集合(ArrayList<Integer>),用來存放每個玩家該有的牌
6)給每個玩家發牌:遍歷牌盒元素,當元素的索引>=集合長度-3時,添加到底牌中;當索引%3==0 %3==1 %3==2時,分別添加到玩家1,2,3中
7)創建一個看牌的方法,在主函數中去調用,輸出三個玩家各有的牌
package com.westos.collections;
import java.util.ArrayList;
import java.util.Collections;
/**
*模擬鬥地主的洗牌和發牌
*
*
*
*思路:
* 1)創建一個牌盒(容器:集合)
* ArrayList<String>
* 2)裝牌
* 定義花色數組和點數數組(A,K)
* 紅桃A
* 黑桃A
* 梅花A
* 方片A
* ....
* 3)洗牌
* Collections中的隨機置換的功能
*
* 4)發牌
* 遍歷集合(獲取集合中的具體的牌)
* 需要判斷:選擇結構語句
* 1--->A 2--->B 3----C
* 4---A .................
*
* 鬥地主三個玩
* 發個三個人----->三個人分別ArrayList<String>
* 如果當前牌 % 3 ==0/1/2
*
* 5)看牌
* 三個人看牌:將看牌封裝成一個功能(獨立的 代碼塊)
*
*/
public class pokerDome {
public static void main(String[] args) {
//創建一個集合對象,相當於牌盒
ArrayList<String> list=new ArrayList<String>();
//創建顏色和牌號
String[] color= {"♣","♥","♠","♦"};
//創建牌號數組
String[] number= {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
//遍歷
for(String col:color) {
for(String num:number) {
String poker = col.concat(num);
list.add(poker);
}
}
String da="大王";
String xiao="小王";
list.add(da);
list.add(xiao);
//打亂集合中的順序相當於洗牌
Collections.shuffle(list);
//發牌
//創建三個玩家集合
ArrayList<String> player1=new ArrayList<String>();
ArrayList<String> player2=new ArrayList<String>();
ArrayList<String> player3=new ArrayList<String>();
ArrayList<String> dipai=new ArrayList<String>();
//遍歷牌盒中的元素
for(int x=0;x<list.size();x++) {
if(x>=list.size()-3) {
dipai.add(list.get(x));
}else if(x%3==0) {
player1.add(list.get(x));
}else if(x%3==1){
player2.add(list.get(x));
}else if(x%3==2){
player3.add(list.get(x));
}
}
System.out.println("開始發牌了...");
lookPoker("玩家1",player1);
lookPoker("玩家2",player2);
lookPoker("玩家3",player3);
lookPoker("底牌",dipai);
}
//創建方法去遍歷玩家手中的牌
public static void lookPoker(String player, ArrayList<String> array) {
System.out.println(player+"的牌是:");
for(String poker:array) {
System.out.print(poker+" ");
}
System.out.println();
}
}
運行結果:
開始發牌了...
玩家1的牌是:
♦3 ♥Q ♥8 ♥3 ♦2 ♠7 ♦5 ♣J ♦K ♥6 ♣8 ♣Q ♦4 ♣3 ♠5 ♥5 ♥7
玩家2的牌是:
♦9 ♣5 ♣4 ♦10 ♠6 ♠3 ♠8 ♠Q ♠A ♦8 ♦Q ♠2 ♣9 ♥A ♦6 ♦A ♠4
玩家3的牌是:
♣7 ♠K ♠10 ♥4 ♥9 ♣6 ♣K ♣10 ♦7 ♥K ♠9 ♦J 大王 ♥2 ♣A ♥10 ♥J
底牌的牌是:
♣2 小王 ♠J
當我們想把每個玩家手中的牌讓它按順序排好,應該怎麼做呢?這時我們應該要運用到TreeSet的自然排序
思路:
1)運用HashMap集合創建牌盒結合,將54張牌的數量當做鍵,每一張牌當做值,通過鍵去找對應的值。再創建一個ArrayList集合去裝牌的數量
2)創建顏色數組和牌號數組將他們添加到牌盒集合中
3)洗牌
4)發牌(運用TreeSet集合使牌變得有序)
例如:
package com.westos.collections;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.TreeSet;
/**
* @author 傑哥
*模擬鬥地主發牌讓牌變得有順序
*/
public class Poker2 {
public static void main(String[] args) {
//創建一個集合去裝牌
HashMap<Integer,String> map=new HashMap<Integer,String>();
//創建顏色數組
String[] color= {"♣","♥","♠","♦"};
//創建牌號數組
String[] number= {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
ArrayList<Integer> list=new ArrayList<Integer>();
int index=0;
for(String num:number) {
for(String col:color) {
String poker = num.concat(col);
map.put(index, poker);
list.add(index);
index++;
}
}
map.put(index,"小王");
list.add(index);
index++;
map.put(index, "大王");
//這裏不能再index++了,防止角標越界
list.add(index);
Collections.shuffle(list);
//去創建玩家對象TreeSet
TreeSet<Integer> player1=new TreeSet<Integer>();
TreeSet<Integer> player2=new TreeSet<Integer>();
TreeSet<Integer> player3=new TreeSet<Integer>();
TreeSet<Integer> dipai=new TreeSet<Integer>();
//發牌
for(int x=0;x<list.size();x++) {
if(x>=list.size()-3) {
dipai.add(list.get(x));
}else if(x%3==0) {
player1.add(list.get(x));
}else if(x%3==1) {
player2.add(list.get(x));
}else if(x%3==2){
player3.add(list.get(x));
}
}
lookpoker("玩家1",player1,map);
lookpoker("玩家2",player2,map);
lookpoker("玩家3",player3,map);
lookpoker("底牌",dipai,map);
}
public static void lookpoker(String name,TreeSet<Integer> player,HashMap<Integer,String> hm) {
System.out.println(name+"的牌是:");
//獲取HashMap集合中的鍵
for(Integer val:player) {
String value = hm.get(val);
System.out.print(value+" ");
}
System.out.println();
}
}
運行結果:
玩家1的牌是:
4♠ 4♦ 5♦ 7♠ 8♣ 8♥ J♣ Q♥ Q♦ K♣ K♠ K♦ A♥ A♦ 2♣ 2♠ 2♦
玩家2的牌是:
3♣ 3♦ 4♣ 6♥ 6♠ 7♦ 8♠ 9♦ 10♠ 10♦ Q♠ K♥ A♣ A♠ 2♥ 小王 大王
玩家3的牌是:
3♥ 3♠ 4♥ 5♣ 5♥ 6♦ 7♣ 7♥ 8♦ 9♣ 9♥ 9♠ 10♣ 10♥ J♥ J♠ Q♣
底牌的牌是:
5♠ 6♣ J♦