HashMap常用方法

HashMap API–oracle文档

map.put()
map.get()  : 只能 value = map.get(key);
map.isEmpty()
map.containsKey()
map.containsValue()
map.remove()  :删除这个key值下的value
map.values()  :显示所有的value值
map.size()    :元素个数
map.keySet()  :显示所有的key(keySet:表示key是不能重复的)
map.putAll()  :添加另一个同一类型的map下的所有值
map.remove()  :删除这个key和value
map.put()     :替换这个key的value (java8)
map.clear()   :清空Hashmap
map.clone()   :Hashmap的克隆
map.putIfAbsent()  :如果当前 Map 不存在键 key 或者该 key 关联的值为 null,那么就执行 put(key, value);否则,便不执行 put 操作:(java8新增方法)
map.compute()      :如果当前 Map 的value为xx时则值为xx否则为xx:(java8新增方法)compute 方法更适用于更新 key 关联的 value 时,新值依赖于旧值的情况

map.put()

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.get()

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
    }

map.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.containsKey()

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.containsValue()

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.remove() 删除这个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(删除的值)
    }

map.values() 显示所有的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);
        System.out.println(map.values()); // [1]
        
        map.put("DEMO2", 2);
        map.put("DEMO2", 2);
        map.put("DEMO2", 2);  // put方法会替换掉key值相同的value值
        System.out.println(map.values()); // [1, 2]
        
        map.put("DEMO2", 3);
        System.out.println(map.values()); // [1, 3]
    }

map.keySet() 显示所有的key(key不能重复)

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);
        map.put("DEMO2", 2);  // put方法会替换掉key值相同的value值
        map.put("DEMO2", 2);  // hashMap的key就是标识符,所以只能有一个
        System.out.println(map.keySet()); // [DEMO1, DEMO2]
    }

map.putAll() 添加另一个同一类型的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}
    }

map.remove() 删除这个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}
    }

map.put() 替换这个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}
    }

map.clear() 清空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);//{}
    }

map.clone() 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.putIfAbsent(): 如果当前 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.compute(): 如果当前 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.computeIfAbsent(key, mappingFunction);
map.computeIfPresent(key, remappingFunction);
map.forEach());
map.merge(key, value, remappingFunction);
map.getOrDefault(key, defaultValue);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值