Java中HashMap的用法详解

package com.mbyte.easy.admin.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wqy
 * @Date: 2019-06-24 16:52
 * @Version 1.0
 */
public class A {

    public static void main(String[] args) {
        HashMap<String,String> map = new HashMap<>();
        /**
         * V put(K key, V value)  把指定的key和value插入map中。如果map中已存在该key,则原value值将被替换成指定值。返回与该key匹配的原value值。
         * 由于hashmap支持value为null值,所以put函数可能返回null,并且有两种情况:在put执行之前,map中不存在指定的key值,或该key的原value值为null。
         */
        map.put("age","11");
        map.put("school",null);
        map.put("sex","女");
        map.put("removetest","removetest");
        System.out.println(map.put("name","wqy"));
        System.out.println(map.put("name","wxd"));
        System.out.println("-----------------------");

        /**
         *  boolean containsKey(Object key)  如果map中包含key值为给定值的映射,则返回true,否则返回false。
         *  boolean containsValue(Object value)  如果map中包含value值为给定值的映射,则返回true,否则返回false。
         *  在上述两种方法中,如果containsKey方法返回为true,则说明map中有且只有一个映射符合条件,如果containsValue返回true,则说明有至少一个,
         *  因为key值不允许重复,而value值允许。
         */
        System.out.println(map.containsKey("name"));
        System.out.println(map.containsValue("wxd"));
        System.out.println(map.containsKey("haha"));
        System.out.println(map.containsValue("haha"));
        System.out.println("-----------------------");

        /**
         * V get(Object key)  返回与给定key值相匹配的value值,不存在该key时返回null
         */
        System.out.println(map.get("name"));
        System.out.println(map.get("haha"));
        System.out.println("-----------------------");

        /**
         * boolean isEmpty()  如果map为空(size==0)则返回true,否则返回false。
         * int size()  返回映射数量。
         */
        System.out.println(map.isEmpty());
        System.out.println(map.size());
        System.out.println("-----------------------");

        /**
         * V remove(Object key)  删除与给定key相关联的映射,返回其value值。
         * 同样存在两种返回null的情况:map中不存在指定的key值,或该key的原value值为null。
         */
        System.out.println(map.remove("removetest"));
        System.out.println(map.remove("school"));
        System.out.println(map.remove("lalal"));
        System.out.println("-----------------------");

        /**
         * boolean replace(K key, V oldValue, V newValue)  把指定的key-oldValue映射中oldValue值替换成newValue。这个操作用put(key, newValue)同样可以实现。
         * V replace(K key, V value)  只有当给定的key对应的value值不为null时,才将其替换成value。
         */
        map.put("key1","value1");
        map.put("key2","value2");
        map.put("key3",null);
        map.put("key4","456");
        map.put("key5","456");
        map.put("key6","456");

        System.out.println(map.replace("key1","value1","value11")+","+map.get("key1"));
        System.out.println(map.replace("key1","ggg","value11")+","+map.get("key1"));
        System.out.println(map.replace("key2","value22")+","+map.get("key2"));
        System.out.println(map.replace("key3","value33")+","+map.get("key3"));
        System.out.println("-----------------------");

        /**
         * 关于entrySet、keySet、values
         */
        for (String key : map.keySet()){
            System.out.print(key + " ");
        }
        System.out.println();

        for (String value : map.values()){
            System.out.print(value + " ");
        }
        System.out.println();

        for (Map.Entry<String,String> entry : map.entrySet()){
            System.out.print(entry.getKey()+":"+entry.getValue()+";");
        }
        System.out.println();
        System.out.println("-----------------------");

        /**
         * 不要在遍历的时候直接删除
         * 下述代码报错如下:
         * Exception in thread "main" java.util.ConcurrentModificationException
         * 	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1445)
         * 	at java.util.HashMap$EntryIterator.next(HashMap.java:1479)
         * 	at java.util.HashMap$EntryIterator.next(HashMap.java:1477)
         * 	at com.mbyte.easy.admin.controller.A.main(A.java:102)
         *
         * 	可以推测,由于我们在遍历HashMap的元素过程中删除了当前所在元素,下一个待访问的元素的指针也由此丢失了
         */
       // for (Map.Entry<String,String> entry : map.entrySet()){
         //       map.remove(entry.getKey());
        //}

        /**
         * 报错:Exception in thread "main" java.util.ConcurrentModificationException
         * 	at java.util.HashMap$HashIterator.nextNode(HashMap.java:1445)
         * 	at java.util.HashMap$EntryIterator.next(HashMap.java:1479)
         * 	at java.util.HashMap$EntryIterator.next(HashMap.java:1477)
         * 	at com.mbyte.easy.admin.controller.A.main(A.java:114)
         */
       // for (Map.Entry<String,String> entry : map.entrySet()){
            //  if(entry.getValue().equals("女")){
           //       map.remove(entry.getKey());
            //  }
      //  }


        /**
         * 正确的删除办法:
         * 先在遍历的时候将要删除的收集起来,退出遍历之后,在进行删除操作
         */
        List<String> keys = new ArrayList<>();

        for (Map.Entry<String,String> entry : map.entrySet()){
            if(entry.getValue().equals("456")){
                keys.add(entry.getKey());
            }
          }

        for(String key:keys){
            map.remove(key);
        }

        for (Map.Entry<String,String> entry : map.entrySet()){
            System.out.print(entry.getKey()+":"+entry.getValue()+";");
        }

        /**
         * hashMap不保证映射的顺序,特别是它不保证该顺序恒久不变
         * 解释:https://q.cnblogs.com/q/113314/
         */
    }
}


 

  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值