当前位置: 首页 > news >正文

Day26 HashMap

Day26 HashMap

文章目录

  • Day26 HashMap
    • 一、应用场景
    • 二、特点
    • 三、基本用法
    • 四、面试题

一、应用场景

1、概念: HashMap是Java集合框架中的一种实现类,用于存储键值对。

2、好处: HashMap是一个常用的集合类,适用于需要快速查找和插入键值对的场景。通过合理使用HashMap,可以高效地管理和操作键值对数据 。

二、特点

1、HashMap基于哈希表实现,提供了快速的查找和插入操作。

2、允许使用null作为键和值。

3、不保证元素的顺序,即不保证遍历顺序与插入顺序相同。

4、允许键和值都可以重复(键重复会覆盖旧值)。

理解:无序 + key去重

注意:1.HashMap的key不允许重复,Key是唯一的
2.HashMap的value允许重复

代码理解:

	public static void main(String[] args) {//Key是唯一的,(小杨)去重HashMap<String, Integer> map = new HashMap<>();		map.put("小张", 27);map.put("小宽", 23);map.put("小白", 23);map.put("小杨", 28);map.put("小杨", 29);map.put("小杨", 30);Set<Entry<String,Integer>> entrySet = map.entrySet();for (Entry<String, Integer> entry : entrySet) {System.out.println(entry);}}

三、基本用法

  1. 导入HashMap类

    import java.util.HashMap;
    
  2. 创建HashMap对象

    HashMap<String, Integer> map = new HashMap<>();
    
  3. 添加键值对

    map.put("apple", 10);
    map.put("banana", 5);
    map.put("orange", 8);
    
  4. 获取值

    int quantity = map.get("apple"); // 返回键"apple"对应的值
    
  5. 遍历HashMap

    for (String key : map.keySet()) {int value = map.get(key);System.out.println(key + ": " + value);
    }
    
  6. 检查键是否存在

    boolean containsKey = map.containsKey("apple"); // 检查是否包含键"apple"
    
  7. 删除键值对

    map.remove("banana"); // 删除键"banana"对应的键值对
    
  8. 获取HashMap大小

    int size = map.size(); // 返回HashMap中键值对的数量
    

代码理解:

public class Test01 {/*** 知识点:HashMap的使用*/public static void main(String[] args) {HashMap<String, Integer> map = new HashMap<>();//添加元素map.put("小希", 27);map.put("小空", 23);map.put("小丽", 28);map.put("小光", 36);map.put("小田", 32);map.put("小阳", 28);map.put("小波", 28);//将newMap中所有的元素添加到map集合中HashMap<String, Integer> newMap = new HashMap<>();newMap.put("aaa", 10);newMap.put("bbb", 20);newMap.put("ccc", 30);newMap.put("ddd", 40);map.putAll(newMap);//如果key存在就获取value值,如果不存在就添加Integer putIfAbsent = map.putIfAbsent("小希111", 28);System.out.println("putIfAbsent:" + putIfAbsent);//通过Key获取到对应的ValueInteger integer1 = map.get("小丽");System.out.println("通过Key获取对应的value:" + integer1);//28//通过Key获取对应的value,如果key不存在则返回默认值Integer integer2 = map.getOrDefault("小希111", 888);System.out.println("通过Key获取对应的value:" + integer2);//888//清空集合中的元素//map.clear();System.out.println("判断集合中是否有指定的key:" + map.containsKey("小希"));//trueSystem.out.println("判断集合中是否有指定的value:" + map.containsValue(27));//trueSystem.out.println("判断集合中是否没有元素:" + map.isEmpty());//false//通过key删除映射关系(key+value)map.remove("aaa");//通过key+value删除映射关系(key+value)map.remove("bbb", 20);//通过key替换valuemap.replace("小希", 30);//通过key+value替换valuemap.replace("小空", 23, 25);//获取映射关系的个数(映射关系内包含了key和value)int size = map.size();System.out.println("获取映射关系的个数:" + size);//10//获取map中所有的valueCollection<Integer> values = map.values();System.out.println(Arrays.toString(values.toArray()));//将集合转换为数组,再将数组转换为字符串System.out.println("-----------------------");//遍历 -- keySet()//思路:获取map集合中所有的key放在一个Set集合中,遍历Set集合获取出key,再通过key获取到Map集合中对应的valueSet<String> keySet = map.keySet();for (String key : keySet) {Integer value = map.get(key);System.out.println(key + " -- " + value);}System.out.println("-----------------------");//遍历 -- entrySet()//思路:获取map集合中所有的映射关系对象放在一个Set集合中,遍历Set集合获取出映射关系对象(Key+Value)Set<Entry<String,Integer>> entrySet = map.entrySet();for (Entry<String, Integer> entry : entrySet) {String key = entry.getKey();Integer value = entry.getValue();System.out.println(key + " -- " + value);}}
}

四、面试题

需求:针对于HashMap的value排序

public static void main(String[] args) {HashMap<String, Integer> map = new HashMap<>();		map.put("小希", 27);map.put("小空", 23);map.put("小丽", 28);map.put("小光", 36);map.put("小田", 32);map.put("小阳", 28);map.put("小波", 28);//将map的映射关系对象取出,返回Set集合Set<Entry<String,Integer>> entrySet = map.entrySet();//将Set集合转换为ArrayList集合ArrayList<Entry<String,Integer>> list = new ArrayList<>(entrySet);//利用ArrayList的sort方法去排序list.sort(new Comparator<Entry<String,Integer>>() {@Overridepublic int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {Integer v1 = o1.getValue();Integer v2 = o2.getValue();return Integer.compare(v1, v2);}});//遍历ArrayListfor (Entry<String, Integer> entry : list) {System.out.println(entry);}}
http://www.lryc.cn/news/325124.html

相关文章:

  • 某蓝队面试经验
  • 【Linux】 centos7安装卸载SQL server(2017、2019)
  • 面试算法-110-课程表
  • 注册前后端php的检测
  • Redis:什么是redis?①
  • 【课程】MyBatisPlus视频教程
  • 如何使用人工智能和ChatGPT来优化营销转化率
  • Ubuntu 22.04上构建libvirt源码错误解决
  • 游戏客户端面经
  • AS,idea,maven,gradle
  • ElasTool v3.0 程序:材料弹性和机械性能的高效计算和可视化工具包
  • Redis入门级详解(一)
  • java算法题每日多道六
  • C# 特性(Attribute)
  • Redis 教程系列之Redis 配置(三)
  • Java实验03
  • 安卓studio连接手机之后,一两秒之后就自动断开了。问题解决。
  • 数字科技优化金融供给,内外协同激活新质生产力
  • 「Linux系列」Shell 输入/输出重定向
  • java实现word转pdf
  • [flask] flask的基本介绍、flask快速搭建项目并运行
  • 设计编程网站集:生活部分:饮食+农业,植物(暂记)
  • 搜索二维矩阵
  • 【LeetCode周赛】第 390 场周赛
  • leetcode 343.整数拆分
  • 部署Zabbix Agents添加使能监测服务器_Linux平台_Yum源/Archive多模式
  • 吴恩达2022机器学习专项课程(一) 第一周课程实验:模型表示(Lab_03)
  • 流畅的 Python 第二版(GPT 重译)(十)
  • 【自然语言处理七-经典论文-attention is all you need】
  • 【嵌入式】STM32和I2C通信