Java学习与复习笔记--Day12

HashMap存储自定义类型键值:

/*
* HashMap存储自定义类型键值
* Map集合保证key是唯一的:
* 作为key的元素,必须重写hashCode方法和equals方法,以保证key唯一
* */
public class Demo01HashMapSavePerson {
    public static void main(String[] args) {
        //show01();
        show02();
    }
    /*
    * HashMap存储自定义类型键值
    * key:Person类型
    * Person类就必须重写hashCode反讽和equals方法以保证key唯一。
    * value:String类型
    * 可以重复*/
    private static void show02() {
        //创建HashMap集合
        HashMap<Person,String> map=new HashMap<>();
        //往集合中添加元素
        map.put(new Person("女王",18),"英国");
        map.put(new Person("秦始皇",18),"秦朝");
        map.put(new Person("普京",30),"俄罗斯");
        map.put(new Person("女王",18),"西班牙");
        //使用entrySet和增强for循环遍历Map集合
        Set<Map.Entry<Person, String>> set = map.entrySet();
        for (Map.Entry<Person, String> entry : set) {
            Person key = entry.getKey();
            String value = entry.getValue();
            System.out.println(key+"--->"+value);
        }
    }

    /*
    * HashMap存储自定义类型键值
    * key:String类型
    *      String类:重写hashCode方法和equals方法,以保证key唯一
    * value:Person类型
    * value可以重复(同名,同年龄的人视为同一个)
    * */
    private static void show01() {
        //创建HashMap集合
        HashMap<String,Person> map=new HashMap<>();
        //往集合中添加元素
        map.put("北京",new Person("张三",18));
        map.put("上海",new Person("李四",19));
        map.put("广州",new Person("王五",20));
        map.put("北京",new Person("李三",18));
        //使用KeySet和增强for循环遍历Map集合
        Set<String> set = map.keySet();
        for (String key : set) {
            Person value = map.get(key);
            System.out.println(key+"-->"+value);
        }
    }
}

LinkedHashMap集合:

/*
* java.util.LinkedHashMap<K,V> extends HashMap<K,V>
* Map接口的哈希表和链表实现,具有可预知的迭代顺序
* 底层原理:
* 哈希表+链表(记录元素的顺序)
* */
public class Demo01LinkedHashMap {
    public static void main(String[] args) {
        HashMap<String,String> map=new HashMap<>();
        map.put("a","a");
        map.put("c","c");
        map.put("b","b");
        map.put("a","d");
        System.out.println(map);//key不允许重复,无序
        LinkedHashMap<String,String> linked=new LinkedHashMap<>();
        linked.put("a","a");
        linked.put("c","c");
        linked.put("b","b");
        linked.put("a","d");
        System.out.println(linked);//key不允许重复,有序
    }
}

HashTable集合:

/*
* java.util.HashTable<K,V>集合 implements Map<K,V>接口
* HashTable接口:底层也是一个哈希表,是一个线程安全的集合,是单线程集合,速度慢
* HashMap接口:底层是一个哈希表,是一个线程不安全的集合,是多线程的集合,速度快
* HashMap集合(之前学的所有集合):可以存储null值,null键
* HashTable集合,不能存储null值,null键
* HashTable和Vector集合一样,在JDK1.2版本后被更先进的集合(HashMap,ArrayList)所取代
* HashTable的子类Properties依然活跃在历史舞台
* Properties集合是唯一一个和IO流相结合的集合
* */
public class Demo02HashTable {
    public static void main(String[] args) {
        HashMap<String,String> map=new HashMap<>();
        map.put(null,"a");
        map.put("b",null);
        map.put(null,null);
        System.out.println(map);//{null=null, b=null}
        Hashtable<String,String> table=new Hashtable<>();
        //table.put(null,"a");//NullPointerException
        //table.put("b",null);//NullPointerException
        //table.put(null,null);//NullPointerException
    }
}

计算一个字符串中每个字符出现的次数:

/*
* 练习,计算一个字符串中每个字符出现的次数
* 分析:
* 1。使用Scanner获取用户输入的字符串
* 2.创建Map集合,key是字符串中的字符,value是字符的个数
* 3.遍历字符串,获取每一个字符
* 4.使用获取到的字符,去Map集合判断,key是否存在
* key存在,通过字符(key)获取value(字符个数)
* value++
* put(key,value)把新的value存储到Map集合中
* key不存在:
* put(key,1)
* 5.遍历Map集合,输出结果*/
public class Demo03MapTest {
    public static void main(String[] args) {
        //使用Scanner获取用户输入的字符串
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入一个字符串:");
        String str = sc.next();
        //创建Map集合,key是字符串中的字符,value是字符的个数
        HashMap<Character,Integer> map=new HashMap<>();
        //遍历字符串,获取每一个字符
        for(char c:str.toCharArray()){
            //key存在,通过字符(key)获取value(字符个数)
            if(map.containsKey(c)){
                Integer value = map.get(c);
                value++;
                //put(key,value)把新的value存储到Map集合中
                map.put(c,value);
            }else{
                //key不存在
                map.put(c,1);
            }
        }
        for(Character key:map.keySet()){
            Integer value = map.get(key);
            System.out.println(key+"="+value);
        }
    }
}

JDK9对集合添加的优化:of方法

/*
* JDK9的新特性:
* List接口,Set接口,Map接口:里面增加了一个静态方法of,可以一次性添加多个元素
* static <E> list<E> of(E...elements)
* 使用前提:
* 当集合中存储的元素的个数已经确定了,不再改变时使用。
* 注意:
* 1.of方法只适用于List接口、Map接口、Set接口,不适用于接口的实现类
* 2.of方法的返回值是一个不能改变的集合,集合不能再使用add,put方法添加元素,会抛出异常
* 3.Set接口和Map接口在调用of方法时,不能有重复的元素,否则会抛出异常。
* */
public class Demo01JDK9 {
    public static void main(String[] args) {
        List<String> list = List.of("a", "b", "a", "c", "d");
        System.out.println(list);//[a, b, a, c, d]
        //list.add("k");//UnsupportedOperationException:不支持操作异常
        //Set<String> set = Set.of("a", "b", "a", "c", "d");
        //System.out.println(set);//IllegalArgumentException:非法参数异常,有重复的元素
        Set<String> set = Set.of("a", "b", "c", "d");
        System.out.println(set);//[a, b, c, d]
        //set.add("k");UnsupportedOperationException:不支持操作异常
        //Map<String, Integer> map = Map.of("张三", 18, "李四", 19, "王五", 20);
        Map<String, Integer> map = Map.of("张三", 18, "李四", 19, "王五", 20,"张三",19);
        System.out.println(map);//{张三=18, 王五=20, 李四=19}       IllegalArgumentException:非法参数异常,有重复的元素
        map.put("赵四",20);//UnsupportedOperationException:不支持操作异常
    }
}

Debug追踪:

/*
* Debug调试程序:
* 可以让代码逐行执行,查看代码执行过程中,出现的bug
* 使用方式:
* 1.在行号的右边,鼠标左键单击,添加断点(每个方法的第一行,哪里有bug添加到哪里)
* 2.右键,选择Debug执行程序
* 程序就会停留在添加的第一个断点处
* 执行程序:
* f8:逐行执行程序
* f7:进入到方法中
* shift+f8:跳出方法
* f9:跳到下一个断点,如果没有下一个断点,那么就停止程序
* ctrl+f2:退出debug模式,停止程序
* Console:切换到控制台。
* */
public class Demo01Debug {
    public static void main(String[] args) {
        /*int a=10;
        int b=20;
        int sum=a+b;
        System.out.println(sum);*/
        /*for (int i = 0; i <3 ; i++) {
            System.out.println(i);
        }*/
        print();
    }

    private static void print() {
        System.out.println("Hello world!");
        System.out.println("Hello world!");
        System.out.println("Hello world!");
        System.out.println("Hello world!");
    }
}

模拟斗地主洗牌发牌:

/*
* 斗地主综合案例:
* 1.准备牌
* 2.洗牌
* 3.发牌
* 4.排序
* 5.看牌*/
public class Doudizhu {
    public static void main(String[] args) {
        //准备牌
        //创建一个Map集合,存储牌的索引和组装好的牌
        HashMap<Integer,String> poker=new HashMap<>();
        //创建一个List集合,存储牌的索引
        ArrayList<Integer> pokerIndex=new ArrayList<>();
        //定义两个集合,存储花色和牌的序号
        List<String> colors = List.of("♠", "♥", "♣", "♦");
        List<String> numbers = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
        //把大王小王存储到集合中
        //定义牌的索引
        int index=0;
        poker.put(index,"大王");
        pokerIndex.add(index);
        index++;
        poker.put(index,"小王");
        pokerIndex.add(index);
        index++;
        //循环嵌套遍历两个集合,组装52张牌,存储到集合中
        for (String number : numbers) {
            for (String color : colors) {
                poker.put(index,color+number);
                pokerIndex.add(index);
                index++;
            }
        }
        //System.out.println(poker);
        //System.out.println(pokerIndex);
        /*
        * 洗牌:
        * 使用Collections中的方法shuffle(List)*/
        Collections.shuffle(pokerIndex);
        //System.out.println(pokerIndex);
        /*
        * 发牌*/
        //定义四个集合,存储玩家牌的索引,和底牌的索引
        ArrayList<Integer> play01=new ArrayList<>();
        ArrayList<Integer> play02=new ArrayList<>();
        ArrayList<Integer> play03=new ArrayList<>();
        ArrayList<Integer> diPai=new ArrayList<>();
        //遍历存储牌索引的List集合,获取每一个牌的索引
        for (int i = 0; i <pokerIndex.size() ; i++) {
            Integer in = pokerIndex.get(i);
            //先判断底牌
            if(i>=51){
                //给底牌添加牌的索引
                diPai.add(in);
            }else if(i%3==0){
                play01.add(in);
            }else if(i%3==1){
                play02.add(in);
            }else if(i%3==2){
                play03.add(in);
            }
        }
        /*
        * 排序
        * 使用Collections中的方法sort(list)
        * 默认是升序排序
        * */
        Collections.sort(play01);
        Collections.sort(play02);
        Collections.sort(play03);
        Collections.sort(diPai);
        /*看牌
        * 调用看牌的方法
        * */
        lookPoker("刘德华",poker,play01);
        lookPoker("周润发",poker,play02);
        lookPoker("周星驰",poker,play03);
        lookPoker("底牌",poker,diPai);
    }
    /*
     * 看牌
     * 定义一个看牌的方法提高看牌的复用性
     * 参数:
     * String name:玩家名称
     * HashMap<Integer,String> poker:存储牌的poker集合
     * ArrayList<Integer> list:存储玩家和底牌的List集合
     * 查表法:
     * 遍历玩家或者底牌结果,获取牌的索引
     * 使用牌的索引,去Map集合中,找到对应的牌
     * */
    public static void lookPoker(String name,HashMap<Integer,String> poker,ArrayList<Integer> list){
        //输出玩家名称,不换行
        System.out.println(name+":");
        for (Integer key : list) {
            //使用牌的索引,去Map集合中,找到对应的牌
            String value = poker.get(key);
            System.out.println(value+"");
        }
        System.out.println();//打印完每一个玩家的牌,再换行
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值