01Map集合概述
A:Map集合概述:
我們通過(guò)查看Map接口描述,發(fā)現(xiàn)Map接口下的集合與Collection接口下的集合,它們存儲(chǔ)數(shù)據(jù)的形式不同
? a:Collection中的集合,元素是孤立存在的(理解為單身),向集合中存儲(chǔ)元素采用一個(gè)個(gè)元素的方式存儲(chǔ)。
?
b:Map中的集合,元素是成對(duì)存在的(理解為夫妻)。每個(gè)元素由鍵與值兩部分組成,通過(guò)鍵可以找對(duì)所對(duì)應(yīng)的值。
?
Collection中的集合稱為單列集合,Map中的集合稱為雙列集合。
? 需要注意的是,Map中的集合不能包含重復(fù)的鍵,值可以重復(fù);每個(gè)鍵只能對(duì)應(yīng)一個(gè)值。
Map
|--HashMap
|--LinkedHashMap
02Map接口中的常用方法
A:Map接口中的常用方法
/*
* Map接口中的常用方法
* 使用Map接口的實(shí)現(xiàn)類 HashMap
/
public class MapDemo {
public static void main(String[] args) {
function_2();
}
/
* 移除集合中的鍵值對(duì),返回被移除之前的值
* V remove(K)
*/
public static void function_2(){
Map<Integer,String> map = new HashMap<Integer, String>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
System.out.println(map);
String value = map.remove(33);
System.out.println(value);
System.out.println(map);
}
/*
* 通過(guò)鍵對(duì)象,獲取值對(duì)象
* V get(K)
* 如果集合中沒(méi)有這個(gè)鍵,返回null
*/
public static void function_1(){
//創(chuàng)建集合對(duì)象,作為鍵的對(duì)象整數(shù),值的對(duì)象存儲(chǔ)字符串
Map<Integer,String> map = new HashMap<Integer, String>();
map.put(1, "a");
map.put(2, "b");
map.put(3, "c");
System.out.println(map);
String value = map.get(4);
System.out.println(value);
}
/*
* 將鍵值對(duì)存儲(chǔ)到集合中
* V put(K,V) K 作為鍵的對(duì)象, V作為值的對(duì)象
* 存儲(chǔ)的是重復(fù)的鍵,將原有的值,覆蓋
* 返回值一般情況下返回null,
* 存儲(chǔ)重復(fù)鍵的時(shí)候,返回被覆蓋之前的值
*/
public static void function(){
//創(chuàng)建集合對(duì)象,HashMap,存儲(chǔ)對(duì)象,鍵是字符串,值是整數(shù)
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
System.out.println(map);
}
}
03Map集合遍歷方式keySet方法
A:Map集合遍歷方式keySet方法
1.獲取Map集合中所有的鍵,由于鍵是唯一的,所以返回一個(gè)Set集合存儲(chǔ)所有的鍵
2.遍歷鍵的Set集合,得到每一個(gè)鍵
3.根據(jù)鍵利用get(key)去Map找所對(duì)應(yīng)的值
/*
* Map集合的遍歷
* 利用鍵獲取值
* Map接口中定義方法keySet
* 所有的鍵,存儲(chǔ)到Set集合
/
public class MapDemo1 {
public static void main(String[] args) {
/
* 1. 調(diào)用map集合的方法keySet,所有的鍵存儲(chǔ)到Set集合中
* 2. 遍歷Set集合,獲取出Set集合中的所有元素 (Map中的鍵)
* 3. 調(diào)用map集合方法get,通過(guò)鍵獲取到值
*/
Map<String,Integer> map = new HashMap<String,Integer>();
map.put("a", 11);
map.put("b", 12);
map.put("c", 13);
map.put("d", 14);
//1. 調(diào)用map集合的方法keySet,所有的鍵存儲(chǔ)到Set集合中
Set<String> set = map.keySet();
//2. 遍歷Set集合,獲取出Set集合中的所有元素 (Map中的鍵)
Iterator<String> it = set.iterator();
while(it.hasNext()){
//it.next返回是Set集合元素,也就是Map中的鍵
//3. 調(diào)用map集合方法get,通過(guò)鍵獲取到值
String key = it.next();
Integer value = map.get(key);
System.out.println(key+"...."+value);
}
System.out.println("=======================");
for(String key : map.keySet()){
Integer value = map.get(key);
System.out.println(key+"...."+value);
}
}
}
04Map集合Entry對(duì)象
A:Map集合Entry對(duì)象
interface Map{
interface Entry{//Entry是Map的一個(gè)內(nèi)部接口
//由Map的子類的內(nèi)部類實(shí)現(xiàn)
}
}
class HashMap{
static class Entry<K,V> implements Map.Entry<K,V> {//Entry對(duì)象指的就是該類的對(duì)象
final K key;
V value;
}
}
在Map類設(shè)計(jì)時(shí),提供了一個(gè)嵌套接口:Entry。
Entry將鍵值對(duì)的對(duì)應(yīng)關(guān)系封裝成了對(duì)象。
即鍵值對(duì)對(duì)象,這樣我們?cè)诒闅vMap集合時(shí),就可以從每一個(gè)鍵值對(duì)(Entry)對(duì)象中獲取對(duì)應(yīng)的鍵與對(duì)應(yīng)的值。
a:Entry是Map接口中提供的一個(gè)靜態(tài)內(nèi)部嵌套接口。
b:相關(guān)方法
? getKey()方法:獲取Entry對(duì)象中的鍵
? getValue()方法:獲取Entry對(duì)象中的值
? entrySet()方法:用于返回Map集合中所有的鍵值對(duì)(Entry)對(duì)象,以Set集合形式返回。
05Map集合遍歷方式entrySet方法
A:Map集合遍歷方式entrySet方法
*
* Map集合獲取方式
* entrySet方法,鍵值對(duì)映射關(guān)系(結(jié)婚證)獲取
* 實(shí)現(xiàn)步驟:
* 1. 調(diào)用map集合方法entrySet()將集合中的映射關(guān)系對(duì)象,存儲(chǔ)到Set集合
* Set<Entry <K,V> >
* 2. 迭代Set集合
* 3. 獲取出的Set集合的元素,是映射關(guān)系對(duì)象
* 4. 通過(guò)映射關(guān)系對(duì)象方法 getKet, getValue獲取鍵值對(duì)
*
* 創(chuàng)建內(nèi)部類對(duì)象 外部類.內(nèi)部類 = new
*/
public class MapDemo2 {
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<Integer, String>();
map.put(1, "abc");
map.put(2, "bcd");
map.put(3, "cde");
//1. 調(diào)用map集合方法entrySet()將集合中的映射關(guān)系對(duì)象,存儲(chǔ)到Set集合
Set<Map.Entry <Integer,String> > set = map.entrySet();
//2. 迭代Set集合
Iterator<Map.Entry <Integer,String> > it = set.iterator();
while(it.hasNext()){
// 3. 獲取出的Set集合的元素,是映射關(guān)系對(duì)象
// it.next 獲取的是什么對(duì)象,也是Map.Entry對(duì)象
Map.Entry<Integer, String> entry = it.next();
//4. 通過(guò)映射關(guān)系對(duì)象方法 getKet, getValue獲取鍵值對(duì)
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"...."+value);
}
}
}
=======================第二節(jié)課開(kāi)始============================================
06Map集合遍歷方式增強(qiáng)for循環(huán)
A:Map集合遍歷方式增強(qiáng)for循環(huán)
A:Map集合遍歷方式entrySet方法
*
* Map集合獲取方式
* entrySet方法,鍵值對(duì)映射關(guān)系(結(jié)婚證)獲取
* 實(shí)現(xiàn)步驟:
* 1. 調(diào)用map集合方法entrySet()將集合中的映射關(guān)系對(duì)象,存儲(chǔ)到Set集合
* Set<Entry <K,V> >
* 2. 迭代Set集合
* 3. 獲取出的Set集合的元素,是映射關(guān)系對(duì)象
* 4. 通過(guò)映射關(guān)系對(duì)象方法 getKet, getValue獲取鍵值對(duì)
*
* 創(chuàng)建內(nèi)部類對(duì)象 外部類.內(nèi)部類 = new
*/
public class MapDemo2 {
public static void main(String[] args) {
Map<Integer,String> map = new HashMap<Integer, String>();
map.put(1, "abc");
map.put(2, "bcd");
map.put(3, "cde");
//1. 調(diào)用map集合方法entrySet()將集合中的映射關(guān)系對(duì)象,存儲(chǔ)到Set集合
Set<Map.Entry <Integer,String> > set = map.entrySet();
//2. 迭代Set集合
Iterator<Map.Entry <Integer,String> > it = set.iterator();
while(it.hasNext()){
// 3. 獲取出的Set集合的元素,是映射關(guān)系對(duì)象
// it.next 獲取的是什么對(duì)象,也是Map.Entry對(duì)象
Map.Entry<Integer, String> entry = it.next();
//4. 通過(guò)映射關(guān)系對(duì)象方法 getKet, getValue獲取鍵值對(duì)
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"...."+value);
}
System.out.println("=========================");
for(Map.Entry<Integer, String> entry : map.entrySet()){
System.out.println(entry.getKey()+"..."+entry.getValue());
}
}
}
注意:Map集合不能直接使用迭代器或者foreach進(jìn)行遍歷。但是轉(zhuǎn)成Set之后就可以使用了。
07HashMap集合存儲(chǔ)和遍歷
A:HashMap集合存儲(chǔ)和遍歷
/*
* 使用HashMap集合,存儲(chǔ)自定義的對(duì)象
* 自定義對(duì)象,作為鍵,出現(xiàn),作為值出現(xiàn)
/
public class HashMapDemo {
public static void main(String[] args) {
function_1();
}
/
* HashMap 存儲(chǔ)自定義對(duì)象Person,作為鍵出現(xiàn)
* 鍵的對(duì)象,是Person類型,值是字符串
* 保證鍵的唯一性,存儲(chǔ)到鍵的對(duì)象,重寫(xiě)hashCode equals
*/
public static void function_1(){
HashMap<Person, String> map = new HashMap<Person, String>();
map.put(new Person("a",20), "里約熱內(nèi)盧");
map.put(new Person("b",18), "索馬里");
map.put(new Person("b",18), "索馬里");
map.put(new Person("c",19), "百慕大");
for(Person key : map.keySet()){
String value = map.get(key);
System.out.println(key+"..."+value);
}
System.out.println("===================");
for(Map.Entry<Person, String> entry : map.entrySet()){
System.out.println(entry.getKey()+"..."+entry.getValue());
}
}
/*
* HashMap 存儲(chǔ)自定義的對(duì)象Person,作為值出現(xiàn)
* 鍵的對(duì)象,是字符串,可以保證唯一性
*/
public static void function(){
HashMap<String, Person> map = new HashMap<String, Person>();
map.put("beijing", new Person("a",20));
map.put("tianjin", new Person("b",18));
map.put("shanghai", new Person("c",19));
for(String key : map.keySet()){
Person value = map.get(key);
System.out.println(key+"..."+value);
}
System.out.println("=================");
for(Map.Entry<String, Person> entry : map.entrySet()){
String key = entry.getKey();
Person value = entry.getValue();
System.out.println(key+"..."+value);
}
}
}
08LinkedHashMap的特點(diǎn)
*A:LinkedHashMap的特點(diǎn)
/*
* LinkedHashMap繼承HashMap
* 保證迭代的順序
*/
public class LinkedHashMapDemo {
public static void main(String[] args) {
LinkedHashMap<String, String> link = new LinkedHashMap<String, String>();
link.put("1", "a");
link.put("13", "a");
link.put("15", "a");
link.put("17", "a");
System.out.println(link);
}
}
09Hashtable的特點(diǎn)
A:Hashtable的特點(diǎn)
/
* Map接口實(shí)現(xiàn)類 Hashtable
* 底層數(shù)據(jù)結(jié)果哈希表,特點(diǎn)和HashMap是一樣的
* Hashtable 線程安全集合,運(yùn)行速度慢
* HashMap 線程不安全的集合,運(yùn)行速度快
*
* Hashtable命運(yùn)和Vector是一樣的,從JDK1.2開(kāi)始,被更先進(jìn)的HashMap取代
*
* HashMap 允許存儲(chǔ)null值,null鍵
* Hashtable 不允許存儲(chǔ)null值,null鍵
*
* Hashtable他的孩子,子類 Properties 依然活躍在開(kāi)發(fā)舞臺(tái)
*/
public class HashtableDemo {
public static void main(String[] args) {
Map<String,String> map = new Hashtable<String,String>();
map.put(null, null);
System.out.println(map);
}
}
10靜態(tài)導(dǎo)入
A:靜態(tài)導(dǎo)入:如果本類中有和靜態(tài)導(dǎo)入的同名方法會(huì)優(yōu)先使用本類的
如果還想使用靜態(tài)導(dǎo)入的,依然需要類名來(lái)調(diào)用
/
* JDK1.5新特性,靜態(tài)導(dǎo)入
* 減少開(kāi)發(fā)的代碼量
* 標(biāo)準(zhǔn)的寫(xiě)法,導(dǎo)入包的時(shí)候才能使用
*
* import static java.lang.System.out;最末尾,必須是一個(gè)靜態(tài)成員
*/
import static java.lang.System.out;
import static java.util.Arrays.sort;
public class StaticImportDemo {
public static void main(String[] args) {
out.println("hello");
int[] arr = {1,4,2};
sort(arr);
}
}
11方法的可變參數(shù)
A:方法的可變參數(shù)
/
* JDK1.5新的特性,方法的可變參數(shù)
* 前提: 方法參數(shù)數(shù)據(jù)類型確定,參數(shù)的個(gè)數(shù)任意
* 可變參數(shù)語(yǔ)法: 數(shù)據(jù)類型...變量名
* 可變參數(shù),本質(zhì)就是一個(gè)數(shù)組
*/
public class VarArgumentsDemo {
public static void main(String[] args) {
//調(diào)用一個(gè)帶有可變參數(shù)的方法,傳遞參數(shù),可以任意
// getSum();
int sum = getSum(5,34,3,56,7,8,0);
System.out.println(sum);
}
/*
* 定義方法,計(jì)算10個(gè)整數(shù)和
* 方法的可變參數(shù)實(shí)現(xiàn)
*/
public static int getSum(int...a){
int sum = 0 ;
for(int i : a){
sum = sum + i;
}
return sum;
}
/*
* 定義方法,計(jì)算3個(gè)整數(shù)和
*/
/*public static int getSum(int a,int b ,int c){
return a+b+c;
}*/
/*
* 定義方法,計(jì)算2個(gè)整數(shù)和
*/
/*public static int getSum(int a,int b){
return a+b;
}*/
}
12可變參數(shù)的注意事項(xiàng)
A:可變參數(shù)的注意事項(xiàng)
/
* 可變參數(shù)的注意事項(xiàng)
* 1. 一個(gè)方法中,可變參數(shù)只能有一個(gè)
* 2. 可變參數(shù),必須寫(xiě)在參數(shù)列表的最后一位
*/
public static void function(Object...o){
}
=======================第三節(jié)課開(kāi)始=============================================
13Collections工具類
A:Collections工具類
/*
* 集合操作的工具類
* Collections
/
public class CollectionsDemo {
public static void main(String[] args) {
function_2();
}
/
* Collections.shuffle方法
* 對(duì)List集合中的元素,進(jìn)行隨機(jī)排列
*/
public static void function_2(){
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(5);
list.add(9);
list.add(11);
list.add(8);
list.add(10);
list.add(15);
list.add(20);
System.out.println(list);
//調(diào)用工具類方法shuffle對(duì)集合隨機(jī)排列
Collections.shuffle(list);
System.out.println(list);
}
/*
* Collections.binarySearch靜態(tài)方法
* 對(duì)List集合進(jìn)行二分搜索,方法參數(shù),傳遞List集合,傳遞被查找的元素
*/
public static void function_1(){
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(5);
list.add(8);
list.add(10);
list.add(15);
list.add(20);
//調(diào)用工具類靜態(tài)方法binarySearch
int index = Collections.binarySearch(list, 16);
System.out.println(index);
}
/*
* Collections.sort靜態(tài)方法
* 對(duì)于List集合,進(jìn)行升序排列
*/
public static void function(){
//創(chuàng)建List集合
List<String> list = new ArrayList<String>();
list.add("ewrew");
list.add("qwesd");
list.add("Qwesd");
list.add("bv");
list.add("wer");
System.out.println(list);
//調(diào)用集合工具類的方法sort
Collections.sort(list);
System.out.println(list);
}
}
14集合的嵌套
A:集合的嵌套
/*
* Map集合的嵌套,Map中存儲(chǔ)的還是Map集合
* 要求:
* 傳智播客
* Java基礎(chǔ)班
* 001 張三
* 002 李四
*
* Java就業(yè)班
* 001 王五
* 002 趙六
* 對(duì)以上數(shù)據(jù)進(jìn)行對(duì)象的存儲(chǔ)
* 001 張三 鍵值對(duì)
* Java基礎(chǔ)班: 存儲(chǔ)學(xué)號(hào)和姓名的鍵值對(duì)
* Java就業(yè)班:
* 傳智播客: 存儲(chǔ)的是班級(jí)
*
* 基礎(chǔ)班Map <學(xué)號(hào),姓名>
* 傳智播客Map <班級(jí)名字, 基礎(chǔ)班Map>
*/
public class MapMapDemo {
public static void main(String[] args) {
//定義基礎(chǔ)班集合
HashMap<String, String> javase = new HashMap<String, String>();
//定義就業(yè)班集合
HashMap<String, String> javaee = new HashMap<String, String>();
//向班級(jí)集合中,存儲(chǔ)學(xué)生信息
javase.put("001", "張三");
javase.put("002", "李四");
javaee.put("001", "王五");
javaee.put("002", "趙六");
//定義傳智播客集合容器,鍵是班級(jí)名字,值是兩個(gè)班級(jí)容器
HashMap<String, HashMap<String,String>> czbk =
new HashMap<String, HashMap<String,String>>();
czbk.put("基礎(chǔ)班", javase);
czbk.put("就業(yè)班", javaee);
keySet(czbk);
}
15集合的嵌套keySet遍歷
A:集合的嵌套keySet遍歷
/*
* Map集合的嵌套,Map中存儲(chǔ)的還是Map集合
* 要求:
* 傳智播客
* Java基礎(chǔ)班
* 001 張三
* 002 李四
*
* Java就業(yè)班
* 001 王五
* 002 趙六
* 對(duì)以上數(shù)據(jù)進(jìn)行對(duì)象的存儲(chǔ)
* 001 張三 鍵值對(duì)
* Java基礎(chǔ)班: 存儲(chǔ)學(xué)號(hào)和姓名的鍵值對(duì)
* Java就業(yè)班:
* 傳智播客: 存儲(chǔ)的是班級(jí)
*
* 基礎(chǔ)班Map <學(xué)號(hào),姓名>
* 傳智播客Map <班級(jí)名字, 基礎(chǔ)班Map>
*/
public class MapMapDemo {
public static void main(String[] args) {
//定義基礎(chǔ)班集合
HashMap<String, String> javase = new HashMap<String, String>();
//定義就業(yè)班集合
HashMap<String, String> javaee = new HashMap<String, String>();
//向班級(jí)集合中,存儲(chǔ)學(xué)生信息
javase.put("001", "張三");
javase.put("002", "李四");
javaee.put("001", "王五");
javaee.put("002", "趙六");
//定義傳智播客集合容器,鍵是班級(jí)名字,值是兩個(gè)班級(jí)容器
HashMap<String, HashMap<String,String>> czbk =
new HashMap<String, HashMap<String,String>>();
czbk.put("基礎(chǔ)班", javase);
czbk.put("就業(yè)班", javaee);
keySet(czbk);
}
public static void keySet(HashMap<String,HashMap<String,String>> czbk){
//調(diào)用czbk集合方法keySet將鍵存儲(chǔ)到Set集合
Set<String> classNameSet = czbk.keySet();
//迭代Set集合
Iterator<String> classNameIt = classNameSet.iterator();
while(classNameIt.hasNext()){
//classNameIt.next獲取出來(lái)的是Set集合元素,czbk集合的鍵
String classNameKey = classNameIt.next();
//czbk集合的方法get獲取值,值是一個(gè)HashMap集合
HashMap<String,String> classMap = czbk.get(classNameKey);
//調(diào)用classMap集合方法keySet,鍵存儲(chǔ)到Set集合
Set<String> studentNum = classMap.keySet();
Iterator<String> studentIt = studentNum.iterator();
while(studentIt.hasNext()){
//studentIt.next獲取出來(lái)的是classMap的鍵,學(xué)號(hào)
String numKey = studentIt.next();
//調(diào)用classMap集合中的get方法獲取值
String nameValue = classMap.get(numKey);
System.out.println(classNameKey+".."+numKey+".."+nameValue);
}
}
System.out.println("==================================");
for(String className: czbk.keySet()){
HashMap<String, String> hashMap = czbk.get(className);
for(String numKey : hashMap.keySet()){
String nameValue = hashMap.get(numKey);
System.out.println(className+".."+numKey+".."+nameValue);
}
}
}
}
16集合的嵌套entrySet遍歷
A:集合的嵌套entrySet遍歷
/*
* Map集合的嵌套,Map中存儲(chǔ)的還是Map集合
* 要求:
* 傳智播客
* Java基礎(chǔ)班
* 001 張三
* 002 李四
*
* Java就業(yè)班
* 001 王五
* 002 趙六
* 對(duì)以上數(shù)據(jù)進(jìn)行對(duì)象的存儲(chǔ)
* 001 張三 鍵值對(duì)
* Java基礎(chǔ)班: 存儲(chǔ)學(xué)號(hào)和姓名的鍵值對(duì)
* Java就業(yè)班:
* 傳智播客: 存儲(chǔ)的是班級(jí)
*
* 基礎(chǔ)班Map <學(xué)號(hào),姓名>
* 傳智播客Map <班級(jí)名字, 基礎(chǔ)班Map>
*/
public class MapMapDemo {
public static void main(String[] args) {
//定義基礎(chǔ)班集合
HashMap<String, String> javase = new HashMap<String, String>();
//定義就業(yè)班集合
HashMap<String, String> javaee = new HashMap<String, String>();
//向班級(jí)集合中,存儲(chǔ)學(xué)生信息
javase.put("001", "張三");
javase.put("002", "李四");
javaee.put("001", "王五");
javaee.put("002", "趙六");
//定義傳智播客集合容器,鍵是班級(jí)名字,值是兩個(gè)班級(jí)容器
HashMap<String, HashMap<String,String>> czbk =
new HashMap<String, HashMap<String,String>>();
czbk.put("基礎(chǔ)班", javase);
czbk.put("就業(yè)班", javaee);
entrySet(czbk);
}
public static void entrySet(HashMap<String,HashMap<String,String>> czbk){
//調(diào)用czbk集合方法entrySet方法,將czbk集合的鍵值對(duì)關(guān)系對(duì)象,存儲(chǔ)到Set集合
Set<Map.Entry<String, HashMap<String,String>>>
classNameSet = czbk.entrySet();
//迭代器迭代Set集合
Iterator<Map.Entry<String, HashMap<String,String>>> classNameIt = classNameSet.iterator();
while(classNameIt.hasNext()){
//classNameIt.next方法,取出的是czbk集合的鍵值對(duì)關(guān)系對(duì)象
Map.Entry<String, HashMap<String,String>> classNameEntry = classNameIt.next();
//classNameEntry方法 getKey,getValue
String classNameKey = classNameEntry.getKey();
//獲取值,值是一個(gè)Map集合
HashMap<String,String> classMap = classNameEntry.getValue();
//調(diào)用班級(jí)集合classMap方法entrySet,鍵值對(duì)關(guān)系對(duì)象存儲(chǔ)Set集合
Set<Map.Entry<String, String>> studentSet = classMap.entrySet();
//迭代Set集合
Iterator<Map.Entry<String, String>> studentIt = studentSet.iterator();
while(studentIt.hasNext()){
//studentIt方法next獲取出的是班級(jí)集合的鍵值對(duì)關(guān)系對(duì)象
Map.Entry<String, String> studentEntry = studentIt.next();
//studentEntry方法 getKey getValue
String numKey = studentEntry.getKey();
String nameValue = studentEntry.getValue();
System.out.println(classNameKey+".."+numKey+".."+nameValue);
}
}
System.out.println("==================================");
for (Map.Entry<String, HashMap<String, String>> me : czbk.entrySet()) {
String classNameKey = me.getKey();
HashMap<String, String> numNameMapValue = me.getValue();
for (Map.Entry<String, String> nameMapEntry : numNameMapValue.entrySet()) {
String numKey = nameMapEntry.getKey();
String nameValue = nameMapEntry.getValue();
System.out.println(classNameKey + ".." + numKey + ".." + nameValue);
}
}
}
}
=======================第四節(jié)課開(kāi)始=============================================
17斗地主的功能分析
A:斗地主的功能分析
a:具體規(guī)則:
1. 組裝54張撲克牌
2. 將54張牌順序打亂
3. 三個(gè)玩家參與游戲,三人交替摸牌,每人17張牌,最后三張留作底牌。
4. 查看三人各自手中的牌(按照牌的大小排序)、底牌
b:分析:
1.準(zhǔn)備牌:
完成數(shù)字與紙牌的映射關(guān)系:
使用雙列Map(HashMap)集合,完成一個(gè)數(shù)字與字符串紙牌的對(duì)應(yīng)關(guān)系(相當(dāng)于一個(gè)字典)。
2.洗牌:
通過(guò)數(shù)字完成洗牌發(fā)牌
3.發(fā)牌:
將每個(gè)人以及底牌設(shè)計(jì)為ArrayList<String>,將最后3張牌直接存放于底牌,剩余牌通過(guò)對(duì)3取模依次發(fā)牌。
存放的過(guò)程中要求數(shù)字大小與斗地主規(guī)則的大小對(duì)應(yīng)。
將代表不同紙牌的數(shù)字分配給不同的玩家與底牌。
4.看牌:
通過(guò)Map集合找到對(duì)應(yīng)字符展示。
通過(guò)查詢紙牌與數(shù)字的對(duì)應(yīng)關(guān)系,由數(shù)字轉(zhuǎn)成紙牌字符串再進(jìn)行展示。
18斗地主的準(zhǔn)備牌
A:斗地主的準(zhǔn)備牌
/*
* 實(shí)現(xiàn)模擬斗地主的功能
* 1. 組合牌
* 2. 洗牌
* 3. 發(fā)牌
* 4. 看牌
*/
public class DouDiZhu {
public static void main(String[] args) {
//1. 組合牌
//創(chuàng)建Map集合,鍵是編號(hào),值是牌
HashMap<Integer,String> pooker = new HashMap<Integer, String>();
//創(chuàng)建List集合,存儲(chǔ)編號(hào)
ArrayList<Integer> pookerNumber = new ArrayList<Integer>();
//定義出13個(gè)點(diǎn)數(shù)的數(shù)組
String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//定義4個(gè)花色數(shù)組
String[] colors = {"?","?","?","?"};
//定義整數(shù)變量,作為鍵出現(xiàn)
int index = 2;
//遍歷數(shù)組,花色+點(diǎn)數(shù)的組合,存儲(chǔ)到Map集合
for(String number : numbers){
for(String color : colors){
pooker.put(index, color+number);
pookerNumber.add(index);
index++;
}
}
//存儲(chǔ)大王,和小王,索引是從0~54,對(duì)應(yīng)大王,小王,...3(牌的順序從大到小)
pooker.put(0, "大王");
pookerNumber.add(0);
pooker.put(1, "小王");
pookerNumber.add(1);
}
19斗地主的洗牌
A:斗地主的洗牌
/*
* 實(shí)現(xiàn)模擬斗地主的功能
* 1. 組合牌
* 2. 洗牌
* 3. 發(fā)牌
* 4. 看牌
*/
public class DouDiZhu {
public static void main(String[] args) {
//1. 組合牌
//創(chuàng)建Map集合,鍵是編號(hào),值是牌
HashMap<Integer,String> pooker = new HashMap<Integer, String>();
//創(chuàng)建List集合,存儲(chǔ)編號(hào)
ArrayList<Integer> pookerNumber = new ArrayList<Integer>();
//定義出13個(gè)點(diǎn)數(shù)的數(shù)組
String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//定義4個(gè)花色數(shù)組
String[] colors = {"?","?","?","?"};
//定義整數(shù)變量,作為鍵出現(xiàn)
int index = 2;
//遍歷數(shù)組,花色+點(diǎn)數(shù)的組合,存儲(chǔ)到Map集合
for(String number : numbers){
for(String color : colors){
pooker.put(index, color+number);
pookerNumber.add(index);
index++;
}
}
//存儲(chǔ)大王,和小王
pooker.put(0, "大王");
pookerNumber.add(0);
pooker.put(1, "小王");
pookerNumber.add(1);
//洗牌,將牌的編號(hào)打亂
Collections.shuffle(pookerNumber);
}
}
20斗地主的發(fā)牌
A:斗地主的發(fā)牌
/*
* 實(shí)現(xiàn)模擬斗地主的功能
* 1. 組合牌
* 2. 洗牌
* 3. 發(fā)牌
* 4. 看牌
*/
public class DouDiZhu {
public static void main(String[] args) {
//1. 組合牌
//創(chuàng)建Map集合,鍵是編號(hào),值是牌
HashMap<Integer,String> pooker = new HashMap<Integer, String>();
//創(chuàng)建List集合,存儲(chǔ)編號(hào)
ArrayList<Integer> pookerNumber = new ArrayList<Integer>();
//定義出13個(gè)點(diǎn)數(shù)的數(shù)組
String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//定義4個(gè)花色數(shù)組
String[] colors = {"?","?","?","?"};
//定義整數(shù)變量,作為鍵出現(xiàn)
int index = 2;
//遍歷數(shù)組,花色+點(diǎn)數(shù)的組合,存儲(chǔ)到Map集合
for(String number : numbers){
for(String color : colors){
pooker.put(index, color+number);
pookerNumber.add(index);
index++;
}
}
//存儲(chǔ)大王,和小王
pooker.put(0, "大王");
pookerNumber.add(0);
pooker.put(1, "小王");
pookerNumber.add(1);
//洗牌,將牌的編號(hào)打亂
Collections.shuffle(pookerNumber);
//發(fā)牌功能,將牌編號(hào),發(fā)給玩家集合,底牌集合
ArrayList<Integer> player1 = new ArrayList<Integer>();
ArrayList<Integer> player2 = new ArrayList<Integer>();
ArrayList<Integer> player3 = new ArrayList<Integer>();
ArrayList<Integer> bottom = new ArrayList<Integer>();
//發(fā)牌采用的是集合索引%3
for(int i = 0 ; i < pookerNumber.size() ; i++){
//先將底牌做好
if(i < 3){
//存到底牌去
bottom.add( pookerNumber.get(i));
//對(duì)索引%3判斷
}else if(i % 3 == 0){
//索引上的編號(hào),發(fā)給玩家1
player1.add( pookerNumber.get(i) );
}else if( i % 3 == 1){
//索引上的編號(hào),發(fā)給玩家2
player2.add( pookerNumber.get(i) );
}else if( i % 3 == 2){
//索引上的編號(hào),發(fā)給玩家3
player3.add( pookerNumber.get(i) );
}
}
}
}
21斗地主的看牌
A:斗地主的看牌
/*
* 實(shí)現(xiàn)模擬斗地主的功能
* 1. 組合牌
* 2. 洗牌
* 3. 發(fā)牌
* 4. 看牌
*/
public class DouDiZhu {
public static void main(String[] args) {
//1. 組合牌
//創(chuàng)建Map集合,鍵是編號(hào),值是牌
HashMap<Integer,String> pooker = new HashMap<Integer, String>();
//創(chuàng)建List集合,存儲(chǔ)編號(hào)
ArrayList<Integer> pookerNumber = new ArrayList<Integer>();
//定義出13個(gè)點(diǎn)數(shù)的數(shù)組
String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//定義4個(gè)花色數(shù)組
String[] colors = {"?","?","?","?"};
//定義整數(shù)變量,作為鍵出現(xiàn)
int index = 2;
//遍歷數(shù)組,花色+點(diǎn)數(shù)的組合,存儲(chǔ)到Map集合
for(String number : numbers){
for(String color : colors){
pooker.put(index, color+number);
pookerNumber.add(index);
index++;
}
}
//存儲(chǔ)大王,和小王
pooker.put(0, "大王");
pookerNumber.add(0);
pooker.put(1, "小王");
pookerNumber.add(1);
//洗牌,將牌的編號(hào)打亂
Collections.shuffle(pookerNumber);
//發(fā)牌功能,將牌編號(hào),發(fā)給玩家集合,底牌集合
ArrayList<Integer> player1 = new ArrayList<Integer>();
ArrayList<Integer> player2 = new ArrayList<Integer>();
ArrayList<Integer> player3 = new ArrayList<Integer>();
ArrayList<Integer> bottom = new ArrayList<Integer>();
//發(fā)牌采用的是集合索引%3
for(int i = 0 ; i < pookerNumber.size() ; i++){
//先將底牌做好
if(i < 3){
//存到底牌去
bottom.add( pookerNumber.get(i));
//對(duì)索引%3判斷
}else if(i % 3 == 0){
//索引上的編號(hào),發(fā)給玩家1
player1.add( pookerNumber.get(i) );
}else if( i % 3 == 1){
//索引上的編號(hào),發(fā)給玩家2
player2.add( pookerNumber.get(i) );
}else if( i % 3 == 2){
//索引上的編號(hào),發(fā)給玩家3
player3.add( pookerNumber.get(i) );
}
}
//對(duì)玩家手中的編號(hào)排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
//看牌,將玩家手中的編號(hào),到Map集合中查找,根據(jù)鍵找值
//定義方法實(shí)現(xiàn)
look("劉德華",player1,pooker);
look("張曼玉",player2,pooker);
look("林青霞",player3,pooker);
look("底牌",bottom,pooker);
}
public static void look(String name,ArrayList<Integer> player,HashMap<Integer,String> pooker){
//遍歷ArrayList集合,獲取元素,作為鍵,到集合Map中找值
System.out.print(name+" ");
for(Integer key : player){
String value = pooker.get(key);
System.out.print(value+" ");
}
System.out.println();
}
}