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 ...
随机推荐
- python 生成推导式
推导式comprehensions(又称解析式),是Python的一种独有特性.推导式是可以从一个数据序列构建另一个新的数据序列的结构体. 共有三种推导,在Python2和3中都有支持: 列表(lis ...
- WPF 模仿 UltraEdit 文件查看器系列 开篇和导读
WPF 模仿 UltraEdit 文件查看器系列 开篇和导读 运行环境:Win10 x64, NetFrameWork 4.8, 作者:乌龙哈里,日期:2019-05-10 学 .Net FrameW ...
- cucumber:extentreports集成报告
extentreports 测试报告 只支持java..Net 先在pom.xml文件中加入包引用 <!-- report--> <dependency> <groupI ...
- Tomcat debug 模式, Application一直reload,导致内存溢出
在server.xml配置文件中,将reloable改为false.
- sqlserver 调优(二)
良好的系统和数据库设计,优质的SQL编写,合适的数据表索引设计,甚至各种硬件因素:网络性能.服务器的性能.操作系统的性能,甚至网卡.交换机等.这篇文章主要讲到如何改善SQL语句,还将有另一篇讨论如何改 ...
- KdPrint/DbgPrint and UNICODE_STRING/ANSI_STRING
typedef struct _UNICODE_STRING { USHORT Length; USHORT MaximumLength; PWSTR Buffer; } UNICODE_STRING ...
- C++之STL(标准模板库)
STL:standard Template Lib 一.vector <1> 特点 <2> 常用函数: <3> 遍历方法: 1.for循环 2.迭代器 <4& ...
- Java-Class-C:org.springframework.http.MediaType
ylbtech-Java-Class-C:org.springframework.http.MediaType 1.返回顶部 1.1. /* * Copyright 2002-2018 the ori ...
- 杂项-Class:Class
ylbtech-杂项-Class:Class 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 作者:ylbtech出处:http://y ...
- LeetCode 746. Min Cost Climbing Stairs (使用最小花费爬楼梯)
题目标签:Dynamic Programming 题目给了我们一组 cost,让我们用最小的cost 走完楼梯,可以从index 0 或者 index 1 出发. 因为每次可以选择走一步,还是走两步, ...