天天看點

java map接口,可變參數,Collections集合工具類

map接口的實作類存儲成對的值,鍵——值。通過鍵來找到對應的值。

Collection中的集合稱為單列集合,Map中的集合稱為雙列集合

 Map中常用的集合為HashMap集合、LinkedHashMap集合。

 HashMap<K,V>:存儲資料采用的哈希表結構,元素的存取順序不能保證一緻。由于要保證鍵的唯一、不重複,需要重寫鍵的hashCode()方法、equals()方法。

LinkedHashMap<K,V>:HashMap下有個子類LinkedHashMap,存儲資料采用的哈希表結構+連結清單結構。通過連結清單結構可以保證元素的存取順序一緻;通過哈希表結構可以保證的鍵的唯一、不重複,需要重寫鍵的hashCode()方法、equals()方法。

注意:Map接口中的集合都有兩個泛型變量<K,V>,在使用時,要為兩個泛型變量賦予資料類型。兩個泛型變量<K,V>的資料類型可以相同,也可以不同

java map接口,可變參數,Collections集合工具類

l put方法:将指定的鍵與值對應起來,并添加到集合中

n 方法傳回值為鍵所對應的值

使用put方法時,若指定的鍵(key)在集合中沒有,則沒有這個鍵對應的值,傳回null,并把指定的鍵值添加到集合中;

使用put方法時,若指定的鍵(key)在集合中存在,則傳回值為集合中鍵對應的值(該值為替換前的值),并把指定鍵所對應的值,替換成指定的新值。

l get方法:擷取指定鍵(key)所對應的值(value)

l remove方法:根據指定的鍵(key)删除元素,傳回被删除元素的值(value)。

1 public static void method01(){
 2         Map<String,String> map =new HashMap<String,String>();
 3         map.put("黃曉明", "baby"); //添加
 4         map.put("鄧超", "孫俪");
 5         map.put("李晨", "範冰冰");
 6         map.put("徐峥", "桃紅");
 7         map.put("徐峥", "紅");
 8         //map.remove("徐峥");
 9         System.out.println(map.get("徐峥"));
10     }      

周遊

java map接口,可變參數,Collections集合工具類
1 public static void method02(){
 2         Map<String,String> map =new HashMap<String,String>();
 3         map.put("黃曉明", "baby"); //添加
 4         map.put("鄧超", "孫俪");
 5         map.put("李晨", "範冰冰");
 6         map.put("徐峥", "桃紅");
 7         map.put("徐峥", "紅");
 8         //周遊map集合
 9         //1、先擷取所有存有map鍵的集合
10         //2、周遊set集合擷取到每一個鍵
11         //3、根據鍵調用get方法擷取到每一個值
12         //先建立一個set集合,調用keyset方法
13         Set<String> set =map.keySet();
14         //循環鍵,用get方法得到鍵所對應的值
15         for(Iterator<String> it=set.iterator();it.hasNext();){
16             System.out.println(map.get(it.next()));
17         }
18     }      

 Entry鍵值對對象

java map接口,可變參數,Collections集合工具類
java map接口,可變參數,Collections集合工具類
java map接口,可變參數,Collections集合工具類
1 public static void method04(){
 2         Map<String,String> map =new HashMap<String,String>();
 3         map.put("黃曉明", "baby"); //添加
 4         map.put("鄧超", "孫俪");
 5         map.put("李晨", "範冰冰");
 6         map.put("徐峥", "桃紅");
 7         //周遊結婚證
 8     /*    1、先獲得結婚證存有set集合
 9         2、周遊結婚證集合得到每一個集合 
10         3、得到每一個結婚證以後擷取這個結婚證中的男方和女方  */
11         Set<Map.Entry<String, String>> se = map.entrySet();
12         for(Map.Entry<String, String> entry:se){
13             System.out.println(entry.getKey()+entry.getValue());
14         }
15     }
    2種方法
16     public static void method05(){
17         Map<String,String> map =new HashMap<String,String>();
18         map.put("黃曉明", "baby"); //添加
19         map.put("鄧超", "孫俪");
20         map.put("李晨", "範冰冰");
21         map.put("徐峥", "桃紅");
22         Set<Map.Entry<String, String>> se = map.entrySet();
23         for(Iterator<Entry<String, String>> it= se.iterator();it.hasNext();){
24             Entry<String, String> str =it.next();
25             System.out.println(str);
26         }
27     }      

可變參數

在JDK1.5之後,如果我們定義一個方法需要接受多個參數,并且多個參數類型一緻,我們可以對其簡化成如下格式:

修飾符 傳回值類型 方法名(參數類型... 形參名){  }

其實這個書寫完全等價與

修飾符 傳回值類型 方法名(參數類型[] 形參名){  }

隻是後面這種定義,在調用時必須傳遞數組,而前者可以直接傳遞資料即可。

jdk1.5以後。出現了簡化操作。... 用在參數上,稱之為可變參數。

public static int add(int...arr){
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;

    }      

Collections集合工具類

java map接口,可變參數,Collections集合工具類

模拟鬥地主發牌練習

1 /*    //構造撲克牌
 2         HashMap<Integer,String> pooker =new HashMap<Integer,String>();
 3         //用于存撲克牌map中的key,洗牌的時候用
 4         ArrayList<Integer> pookenumber =new ArrayList<Integer>();
 5         //封裝一副撲克牌
 6         //定義花色數組
 7         String[] color={"♣","♥","♦","♠"};
 8         //定義數字數組
 9         String[] number ={"2","A","3","4","5","6","7","8","9","10","J","Q","k"};
10         int index =2;
11         for(String num:number){
12             for(String c:color){
13                 //将花色與數字結合存入map
14                 pooker.put(index, c+num);
15                 pookenumber.add(index);
16                 index++;
17             }    
18         }
19         pooker.put(0, "大王");
20         pookenumber.add(0);
21         pooker.put(1, "小王");
22         pookenumber.add(1);
23         Set<Integer> set =pooker.keySet();
24         for(Integer i:set){
25             System.out.println(i+"..."+pooker.get(i));
26         }
27         //洗牌
28         Collections.shuffle(pookenumber);
29         ArrayList<Integer> player1=new ArrayList<Integer>();
30         ArrayList<Integer> player2=new ArrayList<Integer>();
31         ArrayList<Integer> player3=new ArrayList<Integer>();
32         ArrayList<Integer> bottom=new ArrayList<Integer>();
33         for(int i =0;i<pookenumber.size();i++){
34             if(i<3){
35                 bottom.add(pookenumber.get(i));
36             }else if(i%3==0){
37                 player1.add(pookenumber.get(i));
38             }else if(i%3==1){
39                 player2.add(pookenumber.get(i));
40             }else if(i%3==2){
41                 player3.add(pookenumber.get(i));
42             }
43         }
44         //給玩家手裡的牌排序
45         Collections.sort(player1);
46         Collections.sort(player2);
47         Collections.sort(player3);
48         Collections.sort(bottom);
49         look("玩家一",player1,pooker);
50         look("玩家二",player2,pooker);
51         look("玩家三",player3,pooker);
52         look("底牌",bottom,pooker);*/
53               
1 /*public static void look(String name,ArrayList<Integer> player,
 2             HashMap<Integer,String>map){
 3         System.out.println(name+":");
 4         for(Integer num:player){
 5             System.out.print(map.get(num));
 6             
 7         }
 8         System.out.println();
 9     }*/
10           

轉載于:https://www.cnblogs.com/wangrongchen/p/9130761.html