今日內(nèi)容介紹
1、Map集合
2谊却、Map集合遍歷方式
3胰舆、靜態(tài)導(dǎo)入
4妹田、方法可變參
5、模擬斗地主洗牌發(fā)牌
01Map集合
A:Map集合概述
Map接口下的集合與Collection接口下的集合比較
a:Collection中的集合,元素是孤立存在的蹋嵌,向集合中存儲元素采用一個個元素的方式存儲哎垦。
Collection中的集合稱為單列集合柒昏,Map中的集合稱為雙列集合显蝌。
b:Map中的集合,元素是成對存在的勿决。每個元素由鍵與值兩部分組成乒躺,通過鍵可以找對所對應(yīng)的值。
Map中的集合不能包含重復(fù)的鍵低缩,值可以重復(fù)嘉冒;每個鍵只能對應(yīng)一個值。
B:Map集合常用實現(xiàn)類
HashMap
LinkedHashMap
C:Map接口中的常用方法
1咆繁、 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);
}
2讳推、 V get(K) 通過鍵對象,獲取值對象.如果集合中沒有這個鍵,返回null
public static void function_1(){
//創(chuàng)建集合對象,作為鍵的對象整數(shù),值的對象存儲字符串
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);
}
3、V put(K,V) 將鍵值對存儲到集合中,(K作為鍵的對象,V作為值的對象)玩般。存儲的是重復(fù)的鍵,將原有的值,覆蓋
public static void function(){
//創(chuàng)建集合對象,HashMap,存儲對象,鍵是字符串,值是整數(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);
}
02集合遍歷方式
A:Map集合遍歷方式keySet方法
1.獲取Map集合中所有的鍵银觅,由于鍵是唯一的,所以返回一個Set集合存儲所有的鍵
2.遍歷鍵的Set集合坏为,得到每一個鍵
3.根據(jù)鍵利用get(key)去Map找所對應(yīng)的值
public class MapDemo1 {
public static void main(String[] args) {
/*
* 1. 調(diào)用map集合的方法keySet,所有的鍵存儲到Set集合中
* 2. 遍歷Set集合,獲取出Set集合中的所有元素 (Map中的鍵)
* 3. 調(diào)用map集合方法get,通過鍵獲取到值
*/
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,所有的鍵存儲到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,通過鍵獲取到值
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);
}
}
}
B:Map集合遍歷方式entrySet方法
entrySet方法,鍵值對映射關(guān)系(結(jié)婚證)獲取
實現(xiàn)步驟:
1. 調(diào)用map集合方法entrySet()將集合中的映射關(guān)系對象,存儲到Set集合Set<Entry <K,V> >
2. 迭代Set集合
3. 獲取出的Set集合的元素,是映射關(guān)系對象
4. 通過映射關(guān)系對象方法 getKet, getValue獲取鍵值對
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)系對象,存儲到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)系對象
// it.next 獲取的是什么對象,也是Map.Entry對象
Map.Entry<Integer, String> entry = it.next();
//4. 通過映射關(guān)系對象方法 getKet, getValue獲取鍵值對
Integer key = entry.getKey();
String value = entry.getValue();
System.out.println(key+"...."+value);
}
}
}
C:Map集合遍歷方式增強for循環(huán)
Map集合獲取方式:entrySet方法,鍵值對映射關(guān)系(結(jié)婚證)獲取
實現(xiàn)步驟:
1. 調(diào)用map集合方法entrySet()將集合中的映射關(guān)系對象,存儲到Set集合Set<Entry <K,V> >
2. 迭代Set集合
3. 獲取出的Set集合的元素,是映射關(guān)系對象
4. 通過映射關(guān)系對象方法 getKet, getValue獲取鍵值對
Map集合不能直接使用迭代器或者foreach進行遍歷究驴。但是轉(zhuǎn)成Set之后就可以了镊绪。
創(chuàng)建內(nèi)部類對象 外部類.內(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)系對象,存儲到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)系對象
// it.next 獲取的是什么對象,也是Map.Entry對象
Map.Entry<Integer, String> entry = it.next();
//4. 通過映射關(guān)系對象方法 getKet, getValue獲取鍵值對
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());
}
}
}
D:Map集合Entry對象
在Map類設(shè)計時,提供了一個嵌套接口:Entry洒忧。
Entry將鍵值對的對應(yīng)關(guān)系封裝成了對象蝴韭。
即鍵值對對象,在遍歷Map集合時熙侍,可以從每一個鍵值對(Entry)對象中獲取對應(yīng)的鍵與對應(yīng)的值榄鉴。
a:Entry是Map接口中提供的一個靜態(tài)內(nèi)部嵌套接口。
b:相關(guān)方法
? getKey()方法:獲取Entry對象中的鍵
? getValue()方法:獲取Entry對象中的值
? entrySet()方法:用于返回Map集合中所有的鍵值對(Entry)對象蛉抓,以Set集合形式返回庆尘。
interface Map{
interface Entry{//Entry是Map的一個內(nèi)部接口
//由Map的子類的內(nèi)部類實現(xiàn)
}
}
class HashMap{
static class Entry<K,V> implements Map.Entry<K,V> {//Entry對象指的就是該類的對象
final K key;
V value;
}
}
E:使用HashMap集合,存儲自定義的對象
public class HashMapDemo {
public static void main(String[] args) {
function_1();
}
/*
* HashMap 存儲自定義對象Person,作為鍵出現(xiàn)
* 鍵的對象,是Person類型,值是字符串
* 保證鍵的唯一性,存儲到鍵的對象,重寫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 存儲自定義的對象Person,作為值出現(xiàn)
* 鍵的對象,是字符串,可以保證唯一性
*/
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);
}
}
}
03 LinkedHashMap的特點
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);
}
}
04 Hashtable的特點
Map接口實現(xiàn)類 Hashtable.底層數(shù)據(jù)結(jié)果哈希表,特點和HashMap是一樣的
Hashtable、HashMap比較
Hashtable 線程安全集合,運行速度慢
Hashtable命運和Vector是一樣的,從JDK1.2開始,被更先進的HashMap取代
Hashtable 不允許存儲null值,null鍵
Hashtable的子類 Properties 依然活躍在開發(fā)舞臺
HashMap 線程不安全的集合,運行速度快
HashMap 允許存儲null值,null鍵
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);
}
}
05 靜態(tài)導(dǎo)入
靜態(tài)導(dǎo)入特點:
如果本類中有和靜態(tài)導(dǎo)入的同名方法會優(yōu)先使用本類的
如果還想使用靜態(tài)導(dǎo)入的,依然需要類名來調(diào)用
JDK1.5的新特性,可以減少開發(fā)的代碼量
如:import static java.lang.System.out;最末尾,必須是一個靜態(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);
}
}
06方法的可變參數(shù)
A:方法的可變參數(shù)使用
JDK1.5新的特性,即方法參數(shù)數(shù)據(jù)類型確定,參數(shù)的個數(shù)任意
可變參數(shù)語法: 數(shù)據(jù)類型...變量名巷送。本質(zhì)就是一個數(shù)組
public class VarArgumentsDemo {
public static void main(String[] args) {
//調(diào)用一個帶有可變參數(shù)的方法,傳遞參數(shù),可以任意
// getSum();
int sum = getSum(5,34,3,56,7,8,0);
System.out.println(sum);
}
/*
* 定義方法,計算10個整數(shù)和
* 方法的可變參數(shù)實現(xiàn)
*/
public static int getSum(int...a){
int sum = 0 ;
for(int i : a){
sum = sum + i;
}
return sum;
}
/*
* 定義方法,計算3個整數(shù)和
*/
/*public static int getSum(int a,int b ,int c){
return a+b+c;
}*/
/*
* 定義方法,計算2個整數(shù)和
*/
/*public static int getSum(int a,int b){
return a+b;
}*/
}
B:可變參數(shù)的注意事項
1. 一個方法中,可變參數(shù)只能有一個
2. 可變參數(shù),必須寫在參數(shù)列表的最后一位
public static void function(Object...o){
}
07 Collections工具類
A:Collections.shuffle() 對List集合中的元素,進行隨機排列
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對集合隨機排列
Collections.shuffle(list);
System.out.println(list);
}
B:Collections.binarySearch()對List集合進行二分搜索,方法參數(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);
}
C:Collections.sort靜態(tài)方法,對于List集合,進行升序排列
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);
}
}
08斗地主的功能
A:斗地主的功能分析
a:具體規(guī)則:
1. 組裝54張撲克牌
2. 將54張牌順序打亂
3. 三個玩家參與游戲驶忌,三人交替摸牌,每人17張牌笑跛,最后三張留作底牌位岔。
4. 查看三人各自手中的牌(按照牌的大小排序)、底牌
b:分析:
1.準備牌:
完成數(shù)字與紙牌的映射關(guān)系:
使用雙列Map(HashMap)集合堡牡,完成一個數(shù)字與字符串紙牌的對應(yīng)關(guān)系(相當于一個字典)。
2.洗牌:
通過數(shù)字完成洗牌發(fā)牌
3.發(fā)牌:
將每個人以及底牌設(shè)計為ArrayList<String>,將最后3張牌直接存放于底牌杨刨,剩余牌通過對3取模依次發(fā)牌晤柄。
存放的過程中要求數(shù)字大小與斗地主規(guī)則的大小對應(yīng)。
將代表不同紙牌的數(shù)字分配給不同的玩家與底牌妖胀。
4.看牌:
通過Map集合找到對應(yīng)字符展示芥颈。
通過查詢紙牌與數(shù)字的對應(yīng)關(guān)系,由數(shù)字轉(zhuǎn)成紙牌字符串再進行展示赚抡。
B:斗地主的準備牌
public class DouDiZhu {
public static void main(String[] args) {
//1. 組合牌
//創(chuàng)建Map集合,鍵是編號,值是牌
HashMap<Integer,String> pooker = new HashMap<Integer, String>();
//創(chuàng)建List集合,存儲編號
ArrayList<Integer> pookerNumber = new ArrayList<Integer>();
//定義出13個點數(shù)的數(shù)組
String[] numbers = {"2","A","K","Q","J","10","9","8","7","6","5","4","3"};
//定義4個花色數(shù)組
String[] colors = {"?","?","?","?"};
//定義整數(shù)變量,作為鍵出現(xiàn)
int index = 2;
//遍歷數(shù)組,花色+點數(shù)的組合,存儲到Map集合
for(String number : numbers){
for(String color : colors){
pooker.put(index, color+number);
pookerNumber.add(index);
index++;
}
}
//存儲大王,和小王,索引是從0~54,對應(yīng)大王,小王,...3(牌的順序從大到小)
pooker.put(0, "大王");
pookerNumber.add(0);
pooker.put(1, "小王");
pookerNumber.add(1);
}
C:斗地主的洗牌
//洗牌,將牌的編號打亂
Collections.shuffle(pookerNumber);
D:斗地主的發(fā)牌
//發(fā)牌功能,將牌編號,發(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));
//對索引%3判斷
}else if(i % 3 == 0){
//索引上的編號,發(fā)給玩家1
player1.add( pookerNumber.get(i) );
}else if( i % 3 == 1){
//索引上的編號,發(fā)給玩家2
player2.add( pookerNumber.get(i) );
}else if( i % 3 == 2){
//索引上的編號,發(fā)給玩家3
player3.add( pookerNumber.get(i) );
}
}
//對玩家手中的編號排序
Collections.sort(player1);
Collections.sort(player2);
Collections.sort(player3);
E:斗地主的看牌
//看牌,將玩家手中的編號,到Map集合中查找,根據(jù)鍵找值
//定義方法實現(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();
}