HashMap(常用)方法个人理解
Hashmap的存值:
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));//
}
Hashmap的取值:
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"));//
}
Hashmap的判断为空:
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
7 }
Hashmap判断是否含有key:
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
}
Hashmap判断是否含有value:
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
}
Hashmap删除这个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(删除的值)
}
Hashmap显示所有的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);
map.put("DEMO2", 2);
System.out.println(map.values());//[1, 2]
}
Hashmap的元素个数:
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*int*///显示map里的值得数量
System.out.println(map.size());//
map.put("DEMO1", 1);
System.out.println(map.size());//
map.put("DEMO2", 2);
System.out.println(map.size());//
}
Hashmap删除这个key值下的value:
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);
System.out.println(map.keySet());//[DEMO1, DEMO2]
}
Hashmap显示所有的key和value:
public static void main(String[] args) {
HashMap<String, Integer> map=new HashMap<>();
/*SET<map<String,Integer>>*///显示所有的key和value
System.out.println(map.entrySet());//[]
map.put("DEMO1", 1);
System.out.println(map.entrySet());//[DEMO1=1]
map.put("DEMO2", 2);
System.out.println(map.entrySet());//[DEMO1=1, DEMO2=2]
}
Hashmap添加另一个同一类型的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}
}
Hashmap删除这个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}
}
Hashmap替换这个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));//
System.out.println(map);//{DEMO1=1, DEMO2=1}
}
清空这个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();//
System.out.println(map);//{}
}
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
不存在键 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,DEMO3=12222}
}
如果当前 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
的value为xx时则值为xx否则为xx:(java8新增方法)
如果当前 Map
的value为xx时则值为xx否则为xx:(java8新增方法)
如果当前 Map
的value为xx时则值为xx否则为xx:(java8新增方法)
如果当前 Map
的value为xx时则值为xx否则为xx:(java8新增方法)
如果当前 Map
的value为xx时则值为xx否则为xx:(java8新增方法)
java8新增方法
/**/map.computeIfAbsent(key, mappingFunction);
/**/map.computeIfPresent(key, remappingFunction);
/**/map.forEach());
/**/map.merge(key, value, remappingFunction);
/**/map.getOrDefault(key, defaultValue);
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} 此处应该是 {DEMO1=1, DEMO2=2, DEMO3=12222}
}
HashMap(常用)方法个人理解的更多相关文章
- HashMap resize方法的理解(一)
对于oldTable中存储的为15.7.4.5.8.1,长度为8的一个数组中,存储位置如下 0 1 2 3 4 5 6 7 8 1 4 5 15 7 当扩容到一倍后,对于新的位置的选择通过e.hash ...
- Catalina.createDigester方法详细理解
这个方法主要设置(这个方法很重要,贵在理解,虽然还没学过设计模式..) 1.遇到<server>标签时创建StandardServer实例 设置StandardServer类内部的相关 ...
- 遍历HashMap常用的的三种方式
遍历HashMap常用的的三种方式 HashMap是我们使用非常多的集合之一,下面就来介绍几种常用的HashMap的遍历方式. 1.首先定义一个新的HashMap,并往里面添加一些数据. HashMa ...
- 转载:JAVA中关于set()和get()方法的理解及使用
对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 为了让JAVA初学者能更好的理解这两个方法的使用和意义,今天笔者 ...
- [转]Android View.onMeasure方法的理解
转自:http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html Android View.onMeasure方法的理解 View在屏幕上显示出来要先经过 ...
- 五种I/O 模式,select、epoll方法的理解,BIO、NIO、AIO理解 相关文章
一.io方式 Linux网络编程 五种I/O 模式及select.epoll方法的理解 web优化必须了解的原理之I/o的五种模型和web的三种工作模式 五种I/O 模式——阻塞(默认IO模式),非阻 ...
- initWithFrame方法的理解
initWithFrame方法的理解 有时候,知道initWithFrame方法如何用,但是么有弄明白initWithFrame方法到底是什么? 那就通过查资料弄明白. 1. initWi ...
- java中set和get方法的理解
对于JAVA初学者来说,set和get这两个方法似乎已经很熟悉了,这两个方法是JAVA变成中的基本用法,也是出现频率相当高的两个方法. 为了让JAVA初学者能更好的理解这两个方法的使用和意义,今天笔者 ...
- Java 之HashMap.values()方法误用
1.出错 今天在测试代码的时候发现程序报错,看代码才知道是使用HashMap.values()方法的时候出错.因为项目中需要获取Map的值的集合然后进行遍历,所以就很自然的调用了HashMap.val ...
随机推荐
- 经典换根dp——hdu2196
给定一棵边权树,求距离每个点最远的点,输出这个距离 #include<bits/stdc++.h> using namespace std; #define N 10005 ]; int ...
- nagios监控实用教程
nagios监控实用教程 Nagios作为开源网络监视工具,它不但可以有效的监控内存.流量.数据库使用情况.它还可以Windows.Linux主机状态.本专题收录了有关Nagios监控相关文章,供大家 ...
- shell脚本实现GoDaddy中IPv6的动态域名解析(DDNS)_可在路由中运行
首先谈一下个人的需求,家里路由拿到了运营商的外网ipv6地址,于是想着将路由的各种服务通过DDNS放到外网上来,这才有下面的动态域名解析折腾.废话不多说,进入正题. 1.首先在godaddy中注册域名 ...
- Openstack Nova 源码分析 — Create instances (nova-conductor阶段)
目录 目录 前言 Instance Flavor Instance Status Virt Driver Resource Tracker nova-conductor Create Instance ...
- Delphi中的Sender:TObject对象解析转载
https://blog.csdn.net/jl_tiny/article/details/24376661 Delphi中的Sender:TObject对象解析 procedure TForm1.B ...
- vs使用出现的一些常见错误(持续更新)
vs2010编译出错时怎么会执行上一次的结果_百度知道https://zhidao.baidu.com/question/193018332.html
- 漏洞验证系列--MongoDB未授权访问
本系列文章旨在对于有一定网络安全基础的人员,在日常工作中扫描出来的各种漏洞,如何进行验证,以区分该漏洞是否存在或是扫描器误报.请勿应用非法途径. 本漏洞是由于MongoDB未设置访问权限,用户可以直接 ...
- ASP.NET Core Web应用在发布时选择是否对视图进行编译
原文:ASP.NET Core Web应用在发布时选择是否对视图进行编译 在我们发布ASP.NET Core Web应用程序时,选择以文件形式发布,发布方法选择文件系统 默认情况下,会把Views的视 ...
- EXE 和 SYS 信息交互
操了,分发函数少发一个,让我白调了两个多小时.
- vue echarts 给饼图中间添加文字 ,并且添加多个样式
最近根据设计要求写了一个统计图,以下是设计要求,要求中间文字分别是总数和汉字,样式分别不同 好吧具体的解决方案如下 方案一 series: [ { type:'pie', radius: ['50%' ...