四、Map

一、Map集合

1.1 Map介绍

java.util.Map<k,v>集合

特点:

  1. Map集合是一个双列集合,一个元素包含两个值(一个key,一个value)
  2. Map集合中的元素,key和value的数据类型可以是相同,也可以不同
  3. Map集合中的元素,key是不允许重复的,value是可以重复的
  4. Map集合中的元素,key和value是一一对应的

1.2 Map常用子类

java.util.HashMap<k,v>集合 implements Map<k,v>接口

特点:

  1. HashMap集合底层是哈希表:查询速度特别快

    JDK1.8之前:数组+单向链表

    JDK1.8之后:数组+单向链表/红黑树(链表长度超过8):提高查询速度

  2. HashMap集合是一个无序集合,存储元素和取出元素的顺序有可能不一样

java.util.LinkedHashMap<k,v>集合 extends HashMap<k,v>集合

特点:

  1. LinkedHashMap集合底层是哈希表+链表(保证迭代顺序)
  2. LinkedHashMap集合是一个有序的集合,存储元素和取出元素的顺序是一致的

1.3 Map常用方法

public V put (K key,V value);

把指定的值添加入Map中

public V remove(Object key);

把指定键所对应的键值对删除

public V get(Object key)

根据指定的键,在集合中获取对应的值

public boolean containsKey(Object key)

判断集合中是否包含指定的键

1.4 遍历Map方法

  1. 使用Map集合中的方法keySet(),把Map中所有的key取出来,存储到一个Set集合中
  2. 遍历Set集合,获取Map集合中的每一个key(迭代器/增强for)
  3. 通过Map集合中的方法get(key),通过key找到value
Map<String,String> map =new HashMap<>();
map.put("key1", "hello");
map.put("key2", "world");
map.put("key3", "!");
/*Set<String> strings = map.keySet();
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()){
            System.out.println(map.get(iterator.next()));
        }*/
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
    System.out.println(entry.getValue());
}

1.5 HashMap存储自定义类型键值

HashMap存储自定义类型键值

key:Person类型

​ Person类必须重写hashCode和equals方法,保证key唯一

value:String类型

​ 可以重复

HashMap存储自定义类型键值

key:String类型

​ String类重写了hashCode和equals方法,可以保证key唯一

value:Person类型

​ 可以重复

1.6 Hashtable

Hashtable:底层也是一个哈希表,是一个线程安全的集合,是单线程集合,速度慢

HashMap:底层是一个哈希表,是一个线程不安全的集合,是多线程集合,速度快

Hashtable:不能存储null值,null键

HashMap:可以存储null值,null键

Hashtable和Vector集合一样,在jdk1.2被HashMap和ArrayList取代了

Hashtable的子类Properties依然使用,唯一和IO流相结合的集合

二、Debug追踪

可以让代码逐行进行,查看代码执行的过程,调试程序中出现的bug

执行程序:

​ f8:逐行执行程序

​ f7:进入到方法中

​ shift+f8:跳出方法

​ f9:跳到下一个断电

​ ctrl+f2:退出debug

​ console:切换到控制台

三、斗地主

  1. 准备牌

    • 特殊牌:大王、小王
    • 52张牌
  2. 洗牌

  3. 发牌

  4. 排序

  5. 看牌

public static void main(String[] args) {
       //1.准备牌
        List<String> colors = Arrays.asList("♠","♥","♣","♦");
        List<String> numbers = Arrays.asList("2","A","K","Q","J","10","9","8","7","6","5","4","3");
        Map<Integer,String> poker =new HashMap<>();
        List<Integer> pokerIndex =new ArrayList<>();
        int index = 0 ;
        poker.put(index,"大王");
        pokerIndex.add(index);
        index++;
        poker.put(index,"小王");
        pokerIndex.add(index);
        index++;
        for (String number : numbers) {
            for (String color : colors) {
                poker.put(index,color+number);
                pokerIndex.add(index);
                index++;
            }
        }
        System.out.println(poker);
        //2.洗牌
        Collections.shuffle(pokerIndex);
        System.out.println(pokerIndex);
        //3.发牌
        ArrayList<Integer> play01 = new ArrayList<>();
        ArrayList<Integer> play02 = new ArrayList<>();
        ArrayList<Integer> play03 = new ArrayList<>();
        ArrayList<Integer> dipai = new ArrayList<>();
        for (int i = 0; i < pokerIndex.size(); i++) {
            if (i>=51){
                dipai.add(pokerIndex.get(i));
            }else if (i%3==0){
                play01.add(pokerIndex.get(i));
            }else if(i%3==1){
                play02.add(pokerIndex.get(i));
            }else if(i%3==2){
                play03.add(pokerIndex.get(i));
            }
        }
        //4.排序
        Collections.sort(play01);
        Collections.sort(play02);
        Collections.sort(play03);
        Collections.sort(dipai);
        //5.看牌
       lookPoker("play01",poker,play01);
       lookPoker("play02",poker,play02);
       lookPoker("play03",poker,play03);
       lookPoker("dipai",poker,dipai);

    }
    public static void lookPoker(String name,Map<Integer,String> map,List<Integer> index){
        System.out.print(name+": ");
        for (Integer integer : index) {
            System.out.print(map.get(integer)+" ");
        }
        System.out.println();
    }
posted @ 2021-09-09 14:45  盐汽水mua  阅读(52)  评论(0)    收藏  举报