• HashMap TreeMap LinkedHashMap


    HashMap 键是Student值 是String 的案例
      键是Student, 如果不重写,则键肯定不一致, 所以需要重写键对象的hashcode() equals()

    LinkedHashMap 底层是链表,怎么存怎么取

    TreeMap
      键如果是自定义对象,则该对象必须有比较的功能

    public class demon7_hashmapHashMap {
        /*
         * 嵌套HashMap
         *  键是学生 值是学生归属地
         *  有多个班级  键是班级   值是班级号
         */
        public static void main(String[] args) {
            HashMap<Student, String> hm1 = new HashMap<>();
            hm1.put(new Student("张三",23), "北京");
            hm1.put(new Student("李四",24), "北京");
            hm1.put(new Student("王五",25), "上海");
            hm1.put(new Student("赵柳",26), "广州");
            //System.out.println(hm1);
            
            HashMap<Student, String> hm2 = new HashMap<>();
            hm2.put(new Student("唐僧",1023), "北京");
            hm2.put(new Student("悟空",1024), "北京");
            hm2.put(new Student("八戒",1025), "上海");
            hm2.put(new Student("沙僧",926), "广州");
            //System.out.println(hm2);
            
            HashMap<HashMap<Student, String>, Integer> grade = new HashMap<>();
            grade.put(hm1, 88);
            grade.put(hm2, 89);
            
            for (HashMap<Student, String> hm : grade.keySet()) {
                Integer value = grade.get(hm);
                for (Student s : hm.keySet()) {
                    String addr = hm.get(s);
                    
                    System.out.println(s + "..." + addr + "..." + value);
                }
            }
        }
    
    }
    public class test1 {
        /*
         * 需求:统计字符串中每个字符出现的次数
         */
        public static void main(String[] args) {
            String str = "aaaabbbcccccccccc";
            char[] arr = str.toCharArray();                        
            HashMap<Character,Integer> hm = new HashMap<>();
            for (char c : arr) {
                /*if (!hm.containsKey(c)){
                    hm.put(c, 1);
                }else {
                    hm.put(c, hm.get(c)+1);
                }*/
                hm.put(c, !hm.containsKey(c)? 1 : hm.get(c)+1 );
            }
            for (Character key : hm.keySet()) {
                System.out.println(key + " = " + hm.get(key));
            }
            System.out.println(hm.get('a'));
        }
    
    }
    public class test2_dizhu2 {
    
        public static void main(String[] args) {
            String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
            String[] color = {"红","黑","梅","花"};
            
            HashMap<Integer, String> hm = new HashMap<>();   //存贮索引和牌
            
            ArrayList<Integer> list = new ArrayList<>();  // 存索引
            int index= 0 ;
            for (String s1 : color) {
                for (String s2 : num) {
                    hm.put(index, s1.concat(s2));
                    list.add(index);
                    index++;
                }
            }
            hm.put(index, "小王");
            list.add(index);
            index++;
            hm.put(index, "大王");
            list.add(index);
            Collections.shuffle(list);     //随机打乱顺序,相当于洗牌
            
            TreeSet<Integer> gaojin = new TreeSet<>();
            TreeSet<Integer> longwu = new TreeSet<>();
            TreeSet<Integer> me = new TreeSet<>();
            TreeSet<Integer> dipai= new TreeSet<>();
            for (int i = 0; i < list.size(); i++) {    //发牌  存 牌的索引
                if (i>=list.size()-3) {
                    dipai.add(list.get(i));
                } else if (i%3==0) {
                    gaojin.add(list.get(i));
                }else if (i%3==1) {
                    longwu.add(list.get(i));
                }else {
                    me.add(list.get(i));
                }
            }    
            lookPoker(hm, gaojin, "高进");
            lookPoker(hm, longwu, "龙武");
            lookPoker(hm, me, "xxx");
            lookPoker(hm, dipai, "底牌");
        }
        //看牌的参数列表:  hashMap
        public static void lookPoker(HashMap<Integer, String> hm, TreeSet<Integer> ts, String name) {
            System.out.print(name + "的牌是:");
            for (Integer i : ts) { //  ts代表双列集合的键的集合
                System.out.print(hm.get(i)+ " ");
            }
            System.out.println();
        }
    
    }
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.HashMap;
    import java.util.TreeSet;
    
    public class test3 {
    
        /**
         * * A:案例演示
         * 模拟斗地主洗牌和发牌并对牌进行排序的代码实现
         * 
         *  分析:
         * 1,买一副扑克,其实就是自己创建一个集合对象,将扑克牌存储进去
         * 2,洗牌
         * 3,发牌
         * 4,看牌
         */
        public static void main(String[] args) {
            //1,买一副扑克,其实就是自己创建一个集合对象,将扑克牌存储进去
            String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
            String[] color = {"红桃","黑桃","方片","梅花"};
            HashMap<Integer, String> hm = new HashMap<>();                    //存储索引和扑克牌
            ArrayList<Integer> list = new ArrayList<>();                    //存储索引
            int index = 0;
            
            //拼接扑克牌并索引和扑克牌存储在hm中
            for(String s1 : num) {                                            //获取数字
                for(String s2 : color) {                                    //获取颜色
                    hm.put(index, s2.concat(s1));
                    list.add(index);                                        //将索引0到51添加到list集合中
                    index++;
                }
            }
            //将小王添加到双列集合中
            hm.put(index, "小王");
            list.add(index);                                                //将52索引添加到集合中
            index++;
            hm.put(index, "大王");
            list.add(index);                                                //将53索引添加到集合中
            
            //2,洗牌
            Collections.shuffle(list);
            //3,发牌
            TreeSet<Integer> gaojin = new TreeSet<>();
            TreeSet<Integer> longwu = new TreeSet<>();
            TreeSet<Integer> me = new TreeSet<>();
            TreeSet<Integer> dipai = new TreeSet<>();
            
            for(int i = 0; i < list.size(); i++) {
                if(i >= list.size() - 3) {
                    dipai.add(list.get(i));                            //将三张底牌存储在底牌集合中
                }else if(i % 3 == 0) {
                    gaojin.add(list.get(i));
                }else if(i % 3 == 1) {
                    longwu.add(list.get(i));
                }else {
                    me.add(list.get(i));
                }
            }
            
            //看牌
            lookPoker(hm, gaojin, "高进");
            lookPoker(hm, longwu, "龙五");
            lookPoker(hm, me, "xxx");
            lookPoker(hm, dipai, "底牌");
        }
        /*
         * 看牌
         * 1,返回值类型void
         * 2,参数列表HashMap,TreeSet,String name
         */
        public static void lookPoker(HashMap<Integer, String> hm,TreeSet<Integer> ts ,String name) {
            System.out.print(name + "的牌是:");
            for(Integer i : ts) {                        //i代表双列集合中的每一个键
                System.out.print(hm.get(i) + " ");
            }
            System.out.println();
        }
    }
    竹杖芒鞋轻胜马,一蓑烟雨任平生。 回首向来萧瑟处,也无风雨也无晴。
  • 相关阅读:
    Linux内存分析
    mysql 分表
    安装YCM
    c/c++ 之静态库
    ubuntu20 宽带连接
    数据对齐
    计算机中浮点数的表示
    整数的表示
    信息的储存
    SparseTable ST表
  • 原文地址:https://www.cnblogs.com/yaobiluo/p/11306352.html
Copyright © 2020-2023  润新知