第七天 Map

Map

特点

  • 接口:键不能重复
  • HashMap:键存取的顺序不一样
  • LinkedHashMap:键存取的顺序一样
  • Properties:一般和IO流一起使用,使用频率贼高

方法

  • put(K,V) 键不存在,则存进去,键存在修改以前的值
  • get(K) 通过键,获取值
  • containsKey(K) 判断map中是否存在这个值
  • remove(K),通过键删除值

遍历

  • keySet():返回键的一个集合

  • entrySet():返回键值映射关系的集合

    //key是String类型,value也是String类型
    HashMap<String, String> map = new HashMap<>();

    //添加
    map.put(“白富美”,“嬲”);//niao
    map.put(“高富帅”,“嫐”);//nao
    map.put(“屌丝”,“挊”);//nong

    Set sets = map.keySet();
    for(String set:sets){
    String value = set;
    System.out.println(value)
    }

    Set<Map.Entry<String,String>> sets = map.entrySet();
    for(Map.Entry<String,String> set :sets){
    System.out.println(set.getKey+"–"+set.getValue);
    }

注意点:

  • 自定义对象作为键必须重写equals()和hashcode()

LinkedHashMap:

  • 是链表维护的一个HashMap,存取顺序一样

Hashtable和HashMap的区别

  • Hashtable :不能出现null键和值,所有的方法synchronized修饰了,所以是线程安全的,效率低
  • HashMap:可以出现null值和键,方法没有被synchronized,所以线程不安全,效率高

Properties

  • 一般情况键 的位置存属性名,值的位置存属性值
  • 键值一般都是使用字符串类型

统计字符串中每个字符出现的个数

    public static void main(String[] args) {
            System.out.println("请输入:");
            Scanner sc = new Scanner(System.in);
            String str = sc.nextLine();
            long start = System.currentTimeMillis();
            HashMap<Character, Integer> map = new HashMap<>();
            char[] chars = str.toCharArray();
            for (int i = 0; i < chars.length; i++) {
                char ch = chars[i];
                if(map.containsKey(ch)){
                    map.put(ch,map.get(ch)+1);
                }else{
                    map.put(ch,1);
                }
            }
            System.out.println("结果是:");
            Set<Map.Entry<Character,Integer>> sets = map.entrySet();
            for (Map.Entry<Character, Integer> set : sets) {
                System.out.println(set.getKey()+"---出现的次数是:"+set.getValue());
            }
            System.out.println(System.currentTimeMillis()-start);
        }

of方法

- 通过of方法创建的集合不能修改,新特性,没什么卵用

    public static void main(String[] args) {
            List<Integer> list = List.of(1, 2, 3);
            Set<Integer> set = Set.of(1, 2, 3, 4);
            Map<Integer, String> map = Map.of(1, "a", 2, "b");
            System.out.println(list);
            System.out.println(set);
            System.out.println(map);
        }

HashMap实现的斗地主案例

    public class DouDiZhu {
        public static void main(String[] args) {
            //1.先建HashMap和ArrayList,准备牌
            HashMap<Integer, String> poker = new HashMap<>();
            ArrayList<Integer> pokerIndex = new ArrayList<>();
            int index = 0;
            poker.put(0, "大王");
            pokerIndex.add(index);
            index++;
            poker.put(index, "小王");
            pokerIndex.add(index);
            index++;
    
    
            List<String> colors = List.of("♥", "♠", "♣", "♦");
            List<String> nums = List.of("2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3");
    
            for (String num : nums) {
                for (String color : colors) {
                    poker.put(index, color + num);
                    pokerIndex.add(index);
                    index++;
                }
            }
            System.out.println(pokerIndex);
            System.out.println(poker);
    
    //        2.洗牌发牌
            Collections.shuffle(pokerIndex);
            ArrayList<Integer> player01 = new ArrayList<>();
            ArrayList<Integer> player02 = new ArrayList<>();
            ArrayList<Integer> player03 = 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) {
                        player01.add(pokerIndex.get(i));
                    } else if (i % 3 == 1) {
                        player02.add(pokerIndex.get(i));
                    } else if (i % 3 == 2) {
                        player03.add(pokerIndex.get(i));
                    }
                }
            }
            look("发哥",poker,player01);
            look("星爷",poker,player02);
            look("华仔",poker,player03);
            look("底牌",poker,diPai);
    
        }
    	//3.看牌的功能
        public static void look(String name,HashMap<Integer, String> poker,ArrayList<Integer> pokerIndex ){
            System.out.print(name+"的牌是:");
            Collections.sort(pokerIndex);
            for (Integer index : pokerIndex) {
                System.out.print(poker.get(index)+" ");
            }
            System.out.println();
        }
    }

今日总结

  • set 集合 ,动词是设置的意思
  • put的返回值 put(k,v) k不重复,返回null
  • k重复,返回替换的对象,旧的值
  • Collection和Map都重写了tostring()
  • Remove(K),k存在返回v , k不存在返回null
  • Remove() 返回integer 的时候,不建议用int接收,不存在的时候,空指针异常
  • Get和remove注意事项一样
  • Map.Entry<K,V> 内部类
  • Set<Map.Entry<Integer,String>> sets = map.entrySet(); 泛型的嵌套
  • 外部类不可以用private和protected修饰
  • List.of(e1,e2)不能操作 增删
  • Set.of(e1,e2)不能相同的值
  • 通过of创建的集合不能修改
  • 哈希表 hashset
  • public static void sort(T[] arr) 泛型的继承
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值