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

【Java】Map集合中常用方法

Map集合的常用方法
方法名称作用
V put(Key k,V value)添加元素
V remove(K key, V value)根据键值删除对应的值
void clear()清除所有键值元素
boolean containsKey(Object key)判断集合中是否包含指定的键
boolean containsValue(Object value)判断集合中是否包含指定的值
boolean isEmpty()判断集合是否为空
size()返回集合中存放元素的个数

示例代码

package com.collection.Demo09;

import java.util.HashMap;
import java.util.Map;

public class Test01 {
    public static void main(String[] args) {
        Map<String, String> hashMap = new HashMap<>();
        System.out.println("========put========");
        hashMap.put("mayikt001", "小明");
        hashMap.put("mayikt002", "xiaojun");
        hashMap.put("mayikt003", "xiaoli");
        hashMap.put("mayikt003", "小王"); //键是不允许重复的,这里并不会报错,而是修改K="003"的值为V="mayikt"
        System.out.println(hashMap);//{mayikt002=xiaojun, mayikt001=小明, mayikt003=小王}
        //注意:上面遍历的顺序并不是put插入的顺序——∴元素存取是散列无序的
        System.out.println("========remove========");
        hashMap.remove("mayikt001"); //返回String类型
//        hashMap.remove("mayikt002","xiaojun");//只有键值对 都 存在才可以删除,返回Boolean类型
        hashMap.remove("xiaojun"); //没有这个键 ,执行并不报错
        System.out.println(hashMap);
        System.out.println("========clean========");
//        hashMap.clear();//清空hashMap集合中所有键值对
        System.out.println(hashMap);//{}
        System.out.println("========containsKey========");
        //判断在 hashMap集合中 是否存在 键值=mayikt002, 返回true/false
        System.out.println(hashMap.containsKey("mayikt002"));//true
        System.out.println("========containsValue========");
        System.out.println(hashMap.containsValue("xiaowang"));//false
        System.out.println(hashMap.containsValue("小王"));//true
        System.out.println(hashMap.containsValue("小明"));//false
        System.out.println("========isEmpty========");
        System.out.println(hashMap.isEmpty());//false
        HashMap<String, String> hashMap1 = new HashMap<>();
        System.out.println(hashMap1.size());//0 hashMap1中元素的个数
        HashMap<String, String> hashMap2 = null;
        System.out.println(hashMap1.isEmpty());//true
//        System.out.println(hashMap2.isEmpty());//报错.NullPointerException
    }
}

下一篇文章:


http://www.kler.cn/news/108663.html

相关文章:

  • 【2023年NCST C语言新生培训】| 五次培训总结 | C到C++内容补充 | 排位赛详细题解 |《万字长文》
  • Python之函数-传实参的两种方式
  • UNUNX安全的交易所
  • 大数据与人工智能的未来已来
  • 手写IOC
  • 不做学习的奴隶,更要注重生活
  • IntelliJ IDEA 常用快捷键-个人查阅
  • modelsim仿真报错:vlog-2388 ‘scl‘ already declared in this scope
  • Python求n位的自幂数
  • Maven第一章:Maven安装、验证、使用
  • 基于 ARM+FPGA+AD平台的多类型同步信号采集仪开发及试验验证(二)板卡总体设计
  • html2pdf
  • Python实现双目标定、畸变矫正、立体矫正
  • 方太描画未来厨房的模样
  • CV计算机视觉每日开源代码Paper with code速览-2023.10.27
  • 2023上半年系统集成项目管理工程师下午真题
  • 【驱动开发】基于GPIO子系统编写LED驱动,编写应用程序进行测试设置定时器,5秒钟打印一次hello world
  • 嵌入式 Tomcat 调校
  • ABBYY FineReader PDF15免费版图片文件识别软件
  • Dockerfile镜像实战
  • ZYNQ连载02-开发环境
  • Pritunl搭建OpenVPN服务器详细流程,快速实现公网远程连接!
  • 【C++的OpenCV】第十四课-OpenCV基础强化(三):Mat元素的访问之data和step属性
  • 正点原子嵌入式linux驱动开发——外置RTC芯片PCF8563
  • TELUS Ventures(泰勒斯)
  • 项目管理概论:什么是项目生命周期、项目阶段都包含哪些、项目管理过程都有什么以及三者的关系
  • Vue实现首页导航和左侧菜单,介绍mock.js并实现登录注册间的跳转,实现左侧栏折叠效果,优化Main.vue组件,使用mock.js生成随机响应数据
  • 队列(8.6)
  • 多媒体应用设计师 第17章 多媒体应用场景的技术应用和实现示例
  • 适用于物联网的UI设计工具都有哪些?