HashMap常用的API

HashMap好用的API

isEmpty()和clear()

例子

package com.example.springbootdemo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;

/**
 * @Author yimeng
 * @Date 2024/4/26 9:27
 * @PackageName:com.example.springbootdemo
 * @ClassName: HashMapMethodTest
 * @Description: TODO
 * @Version 1.0
 */
@SpringBootTest
public class HashMapMethodTest {
    @Test
    void test() {
        HashMap<Integer, String> sites = new HashMap<>();
        // 检查该 HashMap 是否含有元素
        boolean result = sites.isEmpty();
        System.out.println("是否为空? " + result);// 输出:否为空? true

        // 往 HashMap 添加一些元素
        sites.put(1, "Google");
        sites.put(2, "Jiyik");
        sites.put(3, "Taobao");
        System.out.println("HashMap: " + sites);// 输出:HashMap: {1=Google, 2=Jiyik, 3=Taobao}

        result = sites.isEmpty();
        System.out.println("是否为空? " + result);// 输出:否为空? false
        sites.clear();// 相当于是把 HashMap 中的所有元素都移除了,size变为0
        System.out.println("清空 HashMap 后,是否为空? " + sites.isEmpty());// 输出:清空 HashMap 后,是否为空? true
    }
}

源码分析

clear()把HashMap中内容清空,并且长度变为0

在这里插入图片描述

isEmpty()判断size是否为0

在这里插入图片描述

putIfAbsent()

putIfAbsent() 方法会先判断指定的键(key)是否存在,不存在则将键/值对插入到 HashMap 中,存在,那么就不会进行任何操作。如果所指定的 key 已经在 HashMap 中存在,返回和这个 key 值对应的 value,如果所指定的 key 不在 HashMap 中存在,则返回 null。如果指定 key 之前已经和一个 null 值相关联了 ,则该方法也返回 null。

例子

package com.example.springbootdemo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;

/**
 * @Author yimeng
 * @Date 2024/4/26 9:27
 * @PackageName:com.example.springbootdemo
 * @ClassName: HashMapMethodTest
 * @Description: TODO
 * @Version 1.0
 */
@SpringBootTest
public class HashMapMethodTest {
    @Test
    void test() {
        // 创建一个 HashMap
        HashMap<Integer, String> sites = new HashMap<>();

        // 往 HashMap 添加一些元素
        sites.put(1, "Google");
        sites.put(2, "Jiyik");
        sites.put(3, "Taobao");

        System.out.println("sites HashMap: " + sites);// 输出:sites HashMap: {1=Google, 2=Jiyik, 3=Taobao}


        // 知识点1:HashMap 不存在该key。返回null。
        System.out.println(sites.putIfAbsent(4, "Weibo"));// 输出:null
        // 知识点2:HashMap 不存在该key。会进行插入。
        System.out.println("Insert Languages: " + sites);// 输出:Insert Languages: {1=Google, 2=Jiyik, 3=Taobao, 4=Weibo}

        // HashMap 中存在 Key。key 为 2 已经存在于 sites 中,所以不会执行插入操作。
        // 知识点3:存在key,hashmap中key对应的值,但是不会进行修改或者插入。
        System.out.println(sites.putIfAbsent(2, "Wiki"));// 输出:Jiyik
        System.out.println("Updated Languages: " + sites);// 输出:Updated Languages: {1=Google, 2=Jiyik, 3=Taobao, 4=Weibo}


        // 知识点4:hashMap中运行存在键为null的。
        sites.put(null, "baidu");
        System.out.println("Updated Languages: " + sites);// 输出:Updated Languages: {null=baidu, 1=Google, 2=Jiyik, 3=Taobao, 4=Weibo, 10=null}
        // 知识点5:值为null也行
        sites.put(10,null);
        System.out.println("Updated Languages: " + sites);
        // 知识点6:如果key存在,且值为null,那么输出也是null、所以,输出为null,不一定说明hashmap中一定不存在这个键。
        System.out.println(sites.putIfAbsent(10, "yimeng"));// 输出:null
        // 知识点7:如果key存在,且值为null,将会进行修改。
        System.out.println("Updated Languages: " + sites);// 输出:Updated Languages: {null=baidu, 1=Google, 2=Jiyik, 3=Taobao, 4=Weibo, 10=yimeng}
    }
}

getOrDefault()

getOrDefault() 方法获取指定 key 对应对 value,如果找不到 key ,则返回设置的默认值。

例子

package com.example.springbootdemo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;

/**
 * @Author yimeng
 * @Date 2024/4/26 9:27
 * @PackageName:com.example.springbootdemo
 * @ClassName: HashMapMethodTest
 * @Description: TODO
 * @Version 1.0
 */
@SpringBootTest
public class HashMapMethodTest {
    @Test
    void test() {
        // 创建一个 HashMap
        HashMap<Integer, String> sites = new HashMap<>();

        // 往 HashMap 添加一些元素
        sites.put(1, "Google");
        sites.put(2, "Jiyik");
        sites.put(3, "Taobao");
        System.out.println("sites HashMap: " + sites);// 输出:sites HashMap: {1=Google, 2=Jiyik, 3=Taobao}

        // key 的映射存在于 HashMap 中
        // 没找到 - 如果 HashMap 中没有该 key,则返回默认值
        String value1 = sites.getOrDefault(1, "没找到");
        System.out.println("键为1的值是:" + value1);// 输出:键为1的值是:Google

        // key 的映射不存在于 HashMap 中
        // 没找到 - 如果 HashMap 中没有该 key,则返回默认值
        String value2 = sites.getOrDefault(4, "没找到");
        System.out.println("键为4的值是:" + value2);// 输出:键为4的值是:没找到
    }
}

entrySet()

获取键值对集合。返回值是一个Set<Map.Entry<K,V>>。

例子

package com.example.springbootdemo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;

/**
 * @Author yimeng
 * @Date 2024/4/26 9:27
 * @PackageName:com.example.springbootdemo
 * @ClassName: HashMapMethodTest
 * @Description: TODO
 * @Version 1.0
 */
@SpringBootTest
public class HashMapMethodTest {
    @Test
    void test() {
        // 创建一个 HashMap
        HashMap<Integer, String> sites = new HashMap<>();

        // 往 HashMap 添加一些元素
        sites.put(1, "Google");
        sites.put(2, "Jiyik");
        sites.put(3, "Taobao");
        System.out.println("sites HashMap: " + sites);// 输出:sites HashMap: {1=Google, 2=Jiyik, 3=Taobao}

        // 返回映射关系中 set view
        System.out.println("Set View: " + sites.entrySet());// 输出:Set View: [1=Google, 2=Jiyik, 3=Taobao]
    }
}

keySet()

返回映射中所有 key 组成的 Set 视图。返回的是Set

例子:

package com.example.springbootdemo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;

/**
 * @Author yimeng
 * @Date 2024/4/26 9:27
 * @PackageName:com.example.springbootdemo
 * @ClassName: HashMapMethodTest
 * @Description: TODO
 * @Version 1.0
 */
@SpringBootTest
public class HashMapMethodTest {
    @Test
    void test() {
        // 创建一个 HashMap
        HashMap<Integer, String> sites = new HashMap<>();

        // 往 HashMap 添加一些元素
        sites.put(1, "Google");
        sites.put(2, "Jiyik");
        sites.put(3, "Taobao");
        System.out.println("sites HashMap: " + sites);// 输出:sites HashMap: {1=Google, 2=Jiyik, 3=Taobao}

        // 返回所有 key 组成的 set 集合视图
        System.out.println("Keys: " + sites.keySet());// 输出:Keys: [1, 2, 3]
    }
}

values()

values() 方法返回映射中所有 value 组成的 Set 视图。返回的是Collection

例子

package com.example.springbootdemo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;

/**
 * @Author yimeng
 * @Date 2024/4/26 9:27
 * @PackageName:com.example.springbootdemo
 * @ClassName: HashMapMethodTest
 * @Description: TODO
 * @Version 1.0
 */
@SpringBootTest
public class HashMapMethodTest {
    @Test
    void test() {
        // 创建一个 HashMap
        HashMap<Integer, String> sites = new HashMap<>();

        // 往 HashMap 添加一些元素
        sites.put(1, "Google");
        sites.put(2, "Jiyik");
        sites.put(3, "Taobao");
        System.out.println("sites HashMap: " + sites);// 输出:sites HashMap: {1=Google, 2=Jiyik, 3=Taobao}

        // 访问 HashMap 中所有的 value
        System.out.print("Values: ");// 输出:Values: 

        // values() 返回所有 value 的一个视图
        // for-each 循环可以 从view中访问每一个value值
        for(String value: sites.values()) {
            // 输出每一个value
            System.out.print(value + ", ");// 输出:Google, 
        }
    }
}

merge()

merge() 方法会先判断指定的 key 是否存在。如果key不存在,则把键和第二个参数的值添加到 hashMap 中,并且返回第二个参数的值。如果 key 存在,则更新那个键值对的值为第三个参数计算后的值,并且返回 第三个参数计算后的值

例子

package com.example.springbootdemo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;

/**
 * @Author yimeng
 * @Date 2024/4/26 9:27
 * @PackageName:com.example.springbootdemo
 * @ClassName: HashMapMethodTest
 * @Description: TODO
 * @Version 1.0
 */
@SpringBootTest
public class HashMapMethodTest {
    @Test
    void test() {
        //创建一个HashMap
        HashMap<String, Integer> prices = new HashMap<>();

        // 往 HashMap 插入映射
        prices.put("Shoes", 200);
        prices.put("Bag", 300);
        prices.put("Pant", 150);
        System.out.println("HashMap: " + prices);// 输出:HashMap: {Pant=150, Bag=300, Shoes=200}

        // 知识点1:如果键不存在,则将键和值(第二个参数)对插入到 HashMap 中
        int returnedValue = prices.merge("Shirt", 100, (oldValue, newValue) -> oldValue + newValue);
        System.out.println("Price of Shirt: " + returnedValue);// 输出:Price of Shirt: 100

        // 输出插入后的 HashMap
        System.out.println("Insert HashMap: " + prices);// 输出:Insert HashMap: {Pant=150, Shirt=100, Bag=300, Shoes=200}


        // 知识点2:如果键存在,则会把对应的值更新值为第三个参数的执行结果,并把第三个参数的执行结果当作merge方法的返回值
        //合并 key为 Shoes的映射
        Integer returnedValue1 = prices.merge("Shoes", 123, (oldValue, newValue) -> oldValue + newValue);
        System.out.println("Shoes: " + returnedValue1);// 输出:Shoes: 323
        // 输出更新后的 HashMap
        System.out.println("Updated HashMap: " + prices);// 输出:Updated HashMap: {Pant=150, Shirt=100, Bag=300, Shoes=323}
    }
}

compute()

对 hashMap 中指定 key 的值进行重新计算。如果 key 对应的 value 不存在,则返回该 null,如果存在,则返回通过 remappingFunction 重新计算后的值。

例子

package com.example.springbootdemo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;

/**
 * @Author yimeng
 * @Date 2024/4/26 9:27
 * @PackageName:com.example.springbootdemo
 * @ClassName: HashMapMethodTest
 * @Description: TODO
 * @Version 1.0
 */
@SpringBootTest
public class HashMapMethodTest {
    @Test
    void test() {
        //创建一个 HashMap
        HashMap<String, Integer> prices = new HashMap<>();

        // 往HashMap中添加映射项
        prices.put("Shoes", 200);
        prices.put("Bag", 300);
        prices.put("Pant", 150);
        System.out.println("HashMap: " + prices);// 输出:HashMap: {Pant=150, Bag=300, Shoes=200}

        // 重新计算鞋子打了10%折扣后的值
        // 知识点1:如果key存在,那么就重新计算HashMap中对应的value值(更新),并返回计算后的值作为compute的返回值
        int newPrice = prices.compute("Shoes", (key, value) -> value - value * 10/100);
        System.out.println("Discounted Price of Shoes: " + newPrice);// 输出:Discounted Price of Shoes: 180

        // 输出更新后的HashMap
        System.out.println("Updated HashMap: " + prices);// 输出:Updated HashMap: {Pant=150, Bag=300, Shoes=180}

        // 知识点2:如果key不存在。可能会报错,看你使用。比如,newPrice2,不会报错。但是newPrice4会报错。不报错的话就相当于是插入。报错的话,就停止了。
        int newPrice2 = prices.compute("apple", (key, value) -> 100);
        System.out.println("Discounted Price of Apple: " + newPrice2);// 输出:Discounted Price of Apple: 100

        // 输出更新后的HashMap
        System.out.println("Updated HashMap: " + prices);// 输出:Updated HashMap: {apple=100, Pant=150, Bag=300, Shoes=180}


        // 知识点3:上面插入了,所以这里就相当于是更新了。同知识点1.
        int newPrice3 = prices.compute("apple", (key, value) -> value - value * 10/100);
        System.out.println("Discounted Price of Apple: " + newPrice3);// 输出:Discounted Price of Apple: 90

        // 输出更新后的HashMap
        System.out.println("Updated HashMap: " + prices);// 输出:Updated HashMap: {apple=90, Pant=150, Bag=300, Shoes=180}

        // 知识点4:第二个参数中获取键和值都是可以的。
        int newPrice666 = prices.compute("apple", (key, value) -> {
            System.out.println(key+value);// 输出:apple90
            return 999;
        });
        System.out.println("Discounted Price of Apple: " + newPrice666);// 输出:Discounted Price of Apple: 999
        
        // 输出更新后的HashMap
        System.out.println("Updated HashMap: " + prices);// 输出:Updated HashMap: {apple=999, Pant=150, Bag=300, Shoes=180}

        // 知识点5:可能会报错,看你重新计算中的语句是否允许value为null也能执行成功。
        int newPrice4 = prices.compute("banana", (key, value) -> value - value * 10/100);
        System.out.println("Discounted Price of Banana: " + newPrice4);// 输出:java.lang.NullPointerException

        // 输出更新后的HashMap
        System.out.println("Updated HashMap: " + prices);// 输出:没执行到!!!!
    }
}

computeIfAbsent()

对 hashMap 中指定 key 的值进行重新计算,如果不存在这个 key,则添加到 hashMap 中。

例子

package com.example.springbootdemo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;

/**
 * @Author yimeng
 * @Date 2024/4/26 9:27
 * @PackageName:com.example.springbootdemo
 * @ClassName: HashMapMethodTest
 * @Description: TODO
 * @Version 1.0
 */
@SpringBootTest
public class HashMapMethodTest {
    @Test
    void test() {
        // 创建一个 HashMap
        HashMap<String, Integer> prices = new HashMap<>();

        // 往HashMap中添加映射项
        prices.put("Shoes", 200);
        prices.put("Bag", 300);
        prices.put("Pant", 150);
        System.out.println("HashMap: " + prices);//输出:HashMap: {Pant=150, Bag=300, Shoes=200}

        // 知识点1:没有就插入计算后的值。并且computeIfAbsent方法返回计算后的值。
        // 计算 Shirt 的值
        int shirtPrice = prices.computeIfAbsent("Shirt", key -> 280);
        System.out.println("Price of Shirt: " + shirtPrice);//输出:Price of Shirt: 280

        // 输出更新后的HashMap
        System.out.println("Updated HashMap: " + prices);//输出:Updated HashMap: {Pant=150, Shirt=280, Bag=300, Shoes=200}

        // 知识点2:有不会执行重新计算。并且computeIfAbsent方法返回原来集合中的值。
        // Shoes中的映射关系已经存在
        // Shoes并没有计算新值
        int shoePrice = prices.computeIfAbsent("Shoes", (key) -> 666);
        System.out.println("Price of Shoes: " + shoePrice);//输出:Price of Shoes: 200

        // 输出更新后的 HashMap
        System.out.println("Updated HashMap: " + prices);//输出:Updated HashMap: {Pant=150, Shirt=280, Bag=300, Shoes=200}
    }
}

computeIfPresent()

对 hashMap 中指定 key 的值进行重新计算,前提是该 key 存在于 hashMap 中。

例子

package com.example.springbootdemo;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;

/**
 * @Author yimeng
 * @Date 2024/4/26 9:27
 * @PackageName:com.example.springbootdemo
 * @ClassName: HashMapMethodTest
 * @Description: TODO
 * @Version 1.0
 */
@SpringBootTest
public class HashMapMethodTest {
    @Test
    void test() {
        // 创建一个 HashMap
        HashMap<String, Integer> prices = new HashMap<>();

        // 往HashMap中添加映射关系
        prices.put("Shoes", 200);
        prices.put("Bag", 300);
        prices.put("Pant", 150);
        System.out.println("HashMap: " + prices);// 输出:HashMap: {Pant=150, Bag=300, Shoes=200}

        // 知识点1:如果存在。就重新计算。
        // 重新计算鞋加上10%的增值税后的价值
        int shoesPrice = prices.computeIfPresent("Shoes", (key, value) -> value + value * 10/100);
        System.out.println("Price of Shoes after VAT: " + shoesPrice);// 输出:Price of Shoes after VAT: 220

        // 输出更新后的HashMap
        System.out.println("Updated HashMap: " + prices);// 输出:Updated HashMap: {Pant=150, Bag=300, Shoes=220}

        // 知识点2:如果不存在。会出现异常。没有用value也会出现异常。和上面的compute()不一样。
        int shirtPrice = prices.computeIfPresent("Shirt", (key, value) -> 66);
        System.out.println("Price of Shirt after VAT: " + shirtPrice);// 输出:java.lang.NullPointerException

        // 输出更新后的HashMap
        System.out.println("Updated HashMap: " + prices);// 输出:没有执行
    }
}
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值