HashMap复杂方法总结


主要总结下一些非常规的方法,有些用不太到,但没准哪天就用上了就来看看吧。

1.putAll

批量添加元素,传入map集合,无返回值。

public void putAll(Map<? extends K, ? extends V> m);

  HashMap<Integer, Integer> map1 = new HashMap<>();
	  HashMap<Integer, Integer> map2 = new HashMap<>();
	  map1.put(1, 1);
	  map1.put(2, 1);
	  map1.put(3, 1);
	
	  map2.put(1, 10);
	  map2.put(4, 10);
	
	  map1.putAll(map2);
	  //打印map1
	  System.out.println(map1);
  	 //{1=10, 2=1, 3=1, 4=10}

从结果来看一切以后面的为准,这个方法主要是做合并,但相同会覆盖,需要累加的可不能用这个方法。

2.putIfAbsent

添加元素,传键值对,有返回值。
public V putIfAbsent(K key, V value)

        HashMap<Integer, Integer> map1 = new HashMap<>();
        map1.put(1, 10);
        Integer value1 = map1.putIfAbsent(1, 100);
        Integer value2 = map1.putIfAbsent(2, 200);
        System.out.println(map1);
        System.out.println(value1);
        System.out.println(value2);
        //输出
        //{1=10, 2=200}
        //10
        //null

从结果可知,添加分两种情况
1.如果当前k存在,则不进行添加,并且会返回原有的v值。
2.如果当前k不存在,则进行添加,但会返回null值。
总结:为了保证对应的k不为空才添加值,有点保险操作的感觉,可以通过返回值是否为null来判断是否添加成功。

3.replace

default方法有重载情况,分情况进行讨论。

default V replace(K key, V value);
default boolean replace(K key, V oldValue, V newValue);

1.两种参数的情况

        HashMap<Integer, Integer> map1 = new HashMap<>();
        map1.put(1, 10);
        Integer replace1 = map1.replace(1, 200);
        Integer replace2 = map1.replace(2, 200);
        System.out.println(map1);
        System.out.println(replace1);
        System.out.println(replace2);
        //{1=200}
        //10
        //null

将对应的kv替换,返回替换前的v值 如果替换的k不存在 则返回null

2.三种参数的情况
其中第一个参数为k,第二个参数为旧值,第三个参数为新值

        HashMap<Integer, Integer> map1 = new HashMap<>();
        map1.put(1, 10);
        boolean flag1 = map1.replace(1, 10, 20);
        boolean flag2 = map1.replace(1, 5, 20);
        System.out.println(map1);
        System.out.println(flag1);
        System.out.println(flag2);
        //{1=20}
        //true
        //false

从中可以看出,如果替换成功,则返回true,否则返回fasle。

4.replaceAll

传入lambda表达式,一般就传kv两个值,无返回值。

public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function)

        HashMap<Integer, Integer> map1 = new HashMap<>();
        map1.put(1, 10);
        map1.put(2, 20);
        map1.put(3, 30);

        map1.replaceAll((k, v) -> k * 2);
        System.out.println(map1);
        map1.replaceAll((k, v) -> v * 2);
        System.out.println(map1);
        //{1=2, 2=4, 3=6}
        //{1=4, 2=8, 3=12}
        

从结果可以看出,最终改变的是v的值,相当于遍历map,将v值按某个规则一起改变。

5.getOrDefault

传入k值获取v值,返回v或者替换值。
public V getOrDefault(Object key, V defaultValue)

        HashMap<Integer, Integer> map1 = new HashMap<>();
        map1.put(1, 10);
        map1.put(2, 20);
        map1.put(3, 30);

        Integer v1 = map1.getOrDefault(1, 0);
        Integer v2 = map1.getOrDefault(4, 0);
        System.out.println(v1);
        System.out.println(v2);
        //10
        //0

由结果可看出,如果传入的k有对应的v,则获取,如果没有,我们可以填入v值来获取一个默认值。

6.merge

合并方法,个人觉得很有用,传入三个参数,前两个是kv,后面的是两个参数的lambda,具体看下面。
public V merge(K key, V value,BiFunction<? super V, ? super V, ? extends V> remappingFunction);

        HashMap<Integer, Integer> map1 = new HashMap<>();
        map1.put(1, 10);
        map1.put(2, 20);
        map1.put(3, 30);
		//目的是两个相同的k值相加,组合成新的
        map1.merge(1, 20, (v1, v2) -> v1 + v2);//后面的lambda可以改成方法引用
        map1.merge(10, 20, Integer::sum);//后面的lambda可以改成方法引用
        System.out.println(map1);
        //{1=30, 2=20, 3=30, 10=20}

其实用起来很简单,可以将两个map相同k的v合并起来,最终返回合并过后的新value,如果k不存在,则直接加入map。

7.compute

public V compute(K key,BiFunction<? super K, ? super V, ? extends V> remappingFunction);
第一个参数传入当前map中的k,后面是映射函数,一般传kv值进行重新计算。

        HashMap<Integer, Integer> map1 = new HashMap<>();
        map1.put(1, 10);
        map1.put(2, 20);
        map1.put(3, 30);

        Integer compute = map1.compute(2, (k, v) -> k * v);
        System.out.println(map1);
        System.out.println(compute);
        //{1=10, 2=40, 3=30}
        //40

首先我们通过传入的k获取到kv两个值,之后就可以进行计算了。

8.computeIfAbsent

如果这个k不存在 则放入新的v 否则不进行替换

        HashMap<Integer, Integer> map1 = new HashMap<>();
        map1.put(1, 10);
        map1.put(2, 20);
        map1.put(3, 30);

        //如果这个k不存在 则放入新的v 否则不进行替换
        Integer value1 = map1.computeIfAbsent(1, k -> k * 2);
        Integer value2 = map1.computeIfAbsent(4, k -> k * 2);
        System.out.println(map1);
        System.out.println(value1);
        System.out.println(value2);
        //{1=10, 2=20, 3=30, 4=8}
        //10 可以看到返回了有的v
        //8 可以看到返回了新的v

关键就是针对不存在的kv进行增加。

9. computeIfPresent

传入map的k,和kv参数的lambda,返回计算过后的v值
public V computeIfPresent(K key,BiFunction<? super K, ? super V, ? extends V> remappingFunction)

        HashMap<Integer, Integer> map1 = new HashMap<>();
        map1.put(1, 10);
        map1.put(2, 20);
        map1.put(3, 30);


        Integer value1 = map1.computeIfPresent(2, (k, v) -> k * v);
        Integer value2 = map1.computeIfPresent(4, (k, v) -> k * v);
        Integer value3 = map1.computeIfPresent(4, (k, v) -> 40);
        System.out.println(map1);
        System.out.println(value1);
        System.out.println(value2);
        System.out.println(value3);
        //{1=10, 2=40, 3=30}
        //40
        //null
        //null

从结果可以看出,如果不存在的k,则不会进行新增,如果存在,才会进行计算。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值