• HashMap常用方法-java


    转载:https://blog.csdn.net/AlbenXie/article/details/87950482

    Hashmap的存值:
    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
    }
    Hashmap的取值:
    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
    }
    Hashmap的判断为空:

    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
    }
    Hashmap判断是否含有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
    }
    Hashmap判断是否含有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
    }
    Hashmap删除这个key值下的value:

    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(删除的值)
    }
    Hashmap显示所有的value值:

    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]
    }
    Hashmap的元素个数:

    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
    }
     

    Hashmap增加这个key值下的value:(其实是增加键值对)

    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]
    }
     

    Hashmap显示所有的key和value:

    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]
    }
     

    Hashmap添加另一个同一类型的map下的所有制:

    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}
    }
     

    Hashmap删除这个key和value:

    public static void main(String[] args) {
    HashMap<String, Integer> map=new HashMap<>();
    /*boolean*///删除这个键值对
    map.put("DEMO1", 1);
    map.put("DEMO2", 2);
    System.out.println(map);//{DEMO1=1, DEMO2=2}
    System.out.println(map.remove("DEMO2", 1));//false
    System.out.println(map.remove("DEMO2", 2));//true
    System.out.println(map);//{DEMO1=1}
    }
     

    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}
    }
    清空这个hashmap:

    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);//{}
    }
    Hashmap的克隆:

    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}
    }
     如果当前 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}
    }
     如果当前 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}
    }
    如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

    如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

    如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

    如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

    如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)

    java8新增方法

    方法详解
    /**/map.computeIfAbsent(key, mappingFunction);
    /**/map.computeIfPresent(key, remappingFunction);
    /**/map.forEach());
    /**/map.merge(key, value, remappingFunction);
    /**/map.getOrDefault(key, defaultValue);
    ————————————————
    版权声明:本文为CSDN博主「AlbenXie」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/AlbenXie/article/details/87950482

  • 相关阅读:
    C语言基础知识-程序流程结构
    C语言基础知识-运算符与表达式
    Cloudera Certified Associate Administrator案例之Configure篇
    Python入门篇-文件操作
    gif软件(ShareX)
    BareTail(日志查看工具)
    [UGUI]游戏中的Tips贴图标边缘显示(贴边)
    Lua中的#
    ugui SetParent在安卓上一个诡异bug
    .svn文件夹特别大
  • 原文地址:https://www.cnblogs.com/lzh1043060917/p/12799801.html
Copyright © 2020-2023  润新知