• Java数据结构系列(2)——HashMap的常见方法


    1、存值:map.put(K key, V value)

    public static void main(String[] args) {
        ///*Integer*/map.put("1", 1);//向map中添加值(返回这个key以前的值,如果没有返回null)
        HashMap<String, Integer> map=new HashMap<>();
        System.out.println(map.put("1", 1));//null
        System.out.println(map.put("1", 2));//1
    }

      向map集合中添加Key为key,Value为value的元素,当添加成功时返回null,否则返回value。

    就是说Map集合中的Key是不能重复的,这就类似于Set集合中元素不能重复,但是Map集合中的Value是可以重复。

    2、取值:map.get(key)

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            map.put("DEMO", 1);
            /*Value的类型*///得到map中key相对应的value的值
            System.out.println(map.get("1"));//null
            System.out.println(map.get("DEMO"));//1
    }

    3、判空:boolean isEmpty()

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*boolean*///判断map是否为空
            System.out.println(map.isEmpty());//true
            map.put("DEMO", 1);
            System.out.println(map.isEmpty());//false
    }

      检出map集合中是否有元素,如果没有则返回true,如果有元素则返回false

    4、判断是否有key:boolean containsKey(Object key)

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*boolean*///判断map中是否存在这个key
            System.out.println(map.containsKey("DEMO"));//false
            map.put("DEMO", 1);
            System.out.println(map.containsKey("DEMO"));//true
    }

      检出map集合中有没有包含Key为key的元素,如果有则返回true,否则返回false。

    5、判断是否有value:boolean containsValue(Object value)

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*boolean*///判断map中是否存在这个value
            System.out.println(map.containsValue(1));//false
            map.put("DEMO", 1);
            System.out.println(map.containsValue(1));//true
    }

      检出map集合中有没有包含Value为value的元素,如果有则返回true,否则返回false。

    6、删除键值对:V remove(Object key)

      删除Key为key值的元素

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*Integer*///删除key值下的value
            System.out.println(map.remove("1"));//null
            map.put("DEMO", 2);
            System.out.println(map.remove("DEMO"));//2(删除的值)
    }

    7、显示所有value:map.values()

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*Collection<Integer>*///显示所有的value值
            System.out.println(map.values());//[]
            map.put("DEMO1", 1);
            map.put("DEMO2", 2);
            System.out.println(map.values());//[1, 2]
    }

    8、得到map的大小:size()

      返回map集合中元素个数

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*int*///显示map里的值得数量
            System.out.println(map.size());//0
            map.put("DEMO1", 1);
            System.out.println(map.size());//1
            map.put("DEMO2", 2);
            System.out.println(map.size());//2
    }

    9、返回所有值:map.keySet()

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*SET<String>*///显示map所有的key
            System.out.println(map.keySet());//[]
            map.put("DEMO1", 1);
            System.out.println(map.keySet());//[DEMO1]
            map.put("DEMO2", 2);
            System.out.println(map.keySet());//[DEMO1, DEMO2]
    }
    
    //遍历方式
    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*SET<String>*///遍历map所有的key
            map.put("DEMO1", 1);
            map.put("DEMO2", 2);
            Set set = map.keySet();
            Iterator iter = set.iterator();
            while (iter.hasNext()){
                      String key = (String) iter.next();
                      System.out.println(key);
            }
    }    

    10、显示所有的key和value:map.entrySet()

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*SET<map<String,Integer>>*///显示所有的key和value
            System.out.println(map.entrySet());//[]
            map.put("DEMO1", 1);
            System.out.println(map.entrySet());//[DEMO1=1]
            map.put("DEMO2", 2);
            System.out.println(map.entrySet());//[DEMO1=1, DEMO2=2]
    }

      遍历

    public static void main(String[] args){
            HashMap<String, Integer> map=new HashMap<>();
            /*SET<map<String,Integer>>*///显示所有的key和value
            System.out.println(map.entrySet());//[]
            map.put("DEMO1", 1);
            System.out.println(map.entrySet());//[DEMO1=1]
            map.put("DEMO2", 2);
            System.out.println(map.entrySet());//[DEMO1=1, DEMO2=2]
    
            Iterator i = map.entrySet().iterator();
    //        while (i.hasNext()) {
    //            Object obj = i.next();
    //            String key = obj.toString();
    //            System.out.println(key);   // DEMO1=1    //DEMO2=2
    //        }
    
            while (i.hasNext()) {
                Map.Entry entry = (java.util.Map.Entry)i.next();
                System.out.println(entry.getKey());      //DEMO1   DEMO2
                System.out.println(entry.getValue());    //1   2
            }  
        }

    11、Hashmap添加另一个同一类型的map下的所有制:map.putAll()

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            HashMap<String, Integer> map1=new HashMap<>();
            /*void*///将同一类型的map添加到另一个map中
            map1.put("DEMO1", 1);
            map.put("DEMO2", 2);
            System.out.println(map);//{DEMO2=2}
            map.putAll(map1);
            System.out.println(map);//{DEMO1=1, DEMO2=2}
        }

    12、Hashmap替换这个key的value:(java8)

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*value*///判断map中是否存在这个key
            map.put("DEMO1", 1);
            map.put("DEMO2", 2);
            System.out.println(map);//{DEMO1=1, DEMO2=2}
            System.out.println(map.replace("DEMO2", 1));//2
            System.out.println(map);//{DEMO1=1, DEMO2=1}
        }

    13、清空这个hashmap:void clear()

      把map集合中所有的键值删除

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*void*///清空map
            map.put("DEMO1", 1);
            map.put("DEMO2", 2);
            System.out.println(map);//{DEMO1=1, DEMO2=2}
            map.clear();//2
            System.out.println(map);//{}
        }

    14、Hashmap的克隆:map.clone()

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*object*///克隆这个map
            map.put("DEMO1", 1);
            map.put("DEMO2", 2);
            System.out.println(map.clone());//{DEMO1=1, DEMO2=2}
            Object clone = map.clone();
            System.out.println(clone);//{DEMO1=1, DEMO2=2}
        }

    15、插值

      如果当前 Map 不存在键 key 或者该 key 关联的值为 null,那么就执行 put(key, value);否则,便不执行 put 操作:(java8新增方法)

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*boolean*///判断map中是否存在这个key
            map.put("DEMO1", 1);
            map.put("DEMO2", 2);
            System.out.println(map);//{DEMO1=1, DEMO2=2}
            System.out.println(map.putIfAbsent("DEMO1", 12222));//1
            System.out.println(map.putIfAbsent("DEMO3", 12222));//null
            System.out.println(map);//{DEMO1=1, DEMO2=2,DEMO3=12222}
    }

    16、更新value

       如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)compute 方法更适用于更新 key 关联的 value 时,新值依赖于旧值的情况。

    public static void main(String[] args) {
            HashMap<String, Integer> map=new HashMap<>();
            /*boolean*///当这个value为null时为1,否则为3
            map.put("DEMO1", 1);
            map.put("DEMO2", 2);
            System.out.println(map);//{DEMO1=1, DEMO2=2}
            map.compute("DEMO2", (k,v)->v==null?1:3);
            System.out.println(map);//{DEMO1=1, DEMO2=3}
        }

    17、int hashCode()

      返回map集合的哈希码值

    HashMap<String, Integer> map=new HashMap<>();
    map.put("DEMO1", 1);
    map.put("DEMO2", 2);
    System.out.println(map.hashCode());   //129863004
  • 相关阅读:
    9IDEA常见的快捷键
    8Java类与对象
    5Java运算符
    10Java面向对象中级
    3Java基础
    4Java基础变量
    6Java控制结构
    7Java数组
    11Java面向对象高级
    缓存事件过期监听机制
  • 原文地址:https://www.cnblogs.com/SupremeBoy/p/12380570.html
Copyright © 2020-2023  润新知