HashMap—— values() remove方法 containsKey()方法 containsValue()方法
values()方法:看下面的实例,就是把所有的value值封装成一个connection型的数组
Map<Integer,Student> students=new HashMap<>();
students.put(1, new Student("111"));
students.put(2, new Student("111"));
Collection stuValues = students.values();
for (Object stuValue : stuValues) {
System.out.println(stuValue);
}
输出结果
com.xt.map.Student@52e922
com.xt.map.Student@25154f
remove()方法
final Node<K,V> removeNode(int hash, Object key, Object value,
boolean matchValue, boolean movable) {
Node<K,V>[] tab; Node<K,V> p; int n, index;
if ((tab = table) != null && (n = tab.length) > 0 &&
(p = tab[index = (n - 1) & hash]) != null) {
Node<K,V> node = null, e; K k; V v;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
node = p;
else if ((e = p.next) != null) {
if (p instanceof TreeNode)
node = ((TreeNode<K,V>)p).getTreeNode(hash, key);
else {
do {
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
node = e;
break;
}
p = e;
} while ((e = e.next) != null);
}
}
if (node != null && (!matchValue || (v = node.value) == value ||
(value != null && value.equals(v)))) {
if (node instanceof TreeNode)
((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
else if (node == p)
tab[index] = node.next;
else
p.next = node.next;
++modCount;
--size;
afterNodeRemoval(node);
return node;
}
}
return null;
}
看上面的红色代码,这个事底层代码,就是判定remove的是谁,,,怎么去判断的依据,下面就是依据
1:判定HashCode值是否相同 e.hash == hash
2:判定地址是否相同 k = e.key) == key
3:equals方法 key.equals(k))
if (e.hash == hash &&
((k = e.key) == key ||
(key != null && key.equals(k)))) {
如果为真就删除了这个对象
containsKey()方法
*/
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
我们发现红色的字体和remove方法道理相同,这里就不过多解释了
containsValues()方法
public boolean containsValue(Object value) {
Node<K,V>[] tab; V v;
if ((tab = table) != null && size > 0) {
for (int i = 0; i < tab.length; ++i) {
for (Node<K,V> e = tab[i]; e != null; e = e.next) {
if ((v = e.value) == value ||
(value != null && value.equals(v)))
return true;
}
}
}
return false;
}
(v = e.value) == value || (value != null && value.equals(v))
这个意思就是:如果value地址相同,或者本身的值相同,然后equals方法
如果是基本类型:例如2==2 直接就为TRUE
如果是引用类型:new Student(111)==new Student(222) 为FALSE 但是value.equals(v)为真 最后还是返回TRUE OK~
HashMap—— values() remove方法 containsKey()方法 containsValue()方法的更多相关文章
- java集合框架中contains(),containsKey()和containsValue()的用法:
List集合的contains()方法用于判断集合中包不包含某个元素,返回值是boolean. Map集合的containsKey()和containsValue()方法和上面的相同. 示例: pub ...
- Java 之HashMap.values()方法误用
1.出错 今天在测试代码的时候发现程序报错,看代码才知道是使用HashMap.values()方法的时候出错.因为项目中需要获取Map的值的集合然后进行遍历,所以就很自然的调用了HashMap.val ...
- 34-python基础-python3-列表删除元素-remove()方法-del语句-pop()方法
1-remove()方法 根据值删除元素. remove()方法传入一个列表中的值,它将从被调用的列表中删除. 如果该值在列表中出现多次,只有第一次出现的值会被删除. 如果要删除的值可能在列表中出现 ...
- 牛客网Java刷题知识点之字符流缓冲区、BufferedWriter、BufferedReader、BufferedReader-readLine方法原理、自定义MyBufferedReader-read方法、自定义MyBufferedReader-readLine方法
不多说,直接上干货! 把提高效率的动作,封装成一个对象.即把缓冲区封装成一个对象. 就是在一个类里封装一个数组,能对流锁操作数据进行缓存. 什么是字符流缓冲区? 善于使用字符流缓冲区,减轻负担,提高下 ...
- 静态工厂方法和实例工厂方法及普通的bean
容纳你的bean bean工厂:最简单的容器,提供了基础的依赖注入支持.创建各种类型的Bean. 应用上下文(ApplicationContext):建立在bean工厂基础之上,提供系统架构服务. ...
- Spring学习--静态工厂方法、实例工厂方法创建 Bean
通过调用静态工厂方法创建 bean: 调用静态工厂方法创建 bean 是将对象创建的过程封装到静态方法中 , 当客户端需要对象时 , 只需要简单地调用静态方法 , 而不需要关心创建对象的细节. 要声明 ...
- jquery-7 jquery中的文档处理方法有哪些(方法的参数表示功能增强)
jquery-7 jquery中的文档处理方法有哪些(方法的参数表示功能增强) 一.总结 一句话总结:多看参考文档,多看主干目录.一般的功能分两个方法来实现,一个对应标签,一个对应标签和事情,比如克隆 ...
- .NET 基础 一步步 一幕幕[面向对象之方法、方法的重载、方法的重写、方法的递归]
方法.方法的重载.方法的重写.方法的递归 方法: 将一堆代码进行重用的一种机制. 语法: [访问修饰符] 返回类型 <方法名>(参数列表){ 方法主体: } 返回值类型:如果不需要写返回值 ...
- querySelectorAll 方法相比 getElementsBy 系列方法区别
最近有人问到querySelectorAll 方法相比 getElementsBy 系列方法区别,一时没想起来说些什么,今天查下文档,总结一下它们的区别,以便自己理解. 1. W3C 标准queryS ...
随机推荐
- Leetcode题目101.对称二叉树(简单)
题目描述: 给定一个二叉树,检查它是否是镜像对称的. 例如,二叉树 [1,2,2,3,4,4,3] 是对称的. 1 / \ 2 2 / \ / \ 3 4 4 3 但是下面这个 [1,2,2,null ...
- Go语言中new和make的区别
Go语言中new跟make是内置函数,主要用来创建分配类型内存. new( ) new(T)创建一个没有任何数据的类型为T的实例,并返回该实例的指针: 源码解析 func new func new(T ...
- docker 用nginx 部署 node应用
1.查询镜像 # 1.查询镜像. docker search nginx 2.拉取指定的镜像 # 2.拉取指定的镜像 docker pull nginx 3.下载完成后终端查看 # 3.下载完成后终 ...
- UTC ISO 8601
如果时间在零时区,并恰好与协调世界时相同,那么(不加空格地)在时间最后加一个大写字母Z.Z是相对协调世界时时间0偏移的代号.如下午2点30分5秒表示为14:30:05Z或143005Z:只表示小时和分 ...
- 1.分布式配置中心 spring-cloud-config
pring Cloud 版本:2.1.0.RELEASE 一.server端 1.maven依赖 <dependency> <groupId>org.springframewo ...
- vue-判断设备是手机端还是pc端
经常在项目中会有支持 pc 与手机端需求.并且pc与手机端是两个不一样的页面.这时就要求判断设置,根据不同的设置跳转不同的路由. [代码演示] 在 router/index.js 中有两个页面. ex ...
- jar/war文件的解释
http://blog.csdn.net/tang_123_/article/details/6012202#comments
- hive基础指令
- Python-sympy科学计算与数据处理(数学表达式)
数学表达式 from sympy import * 1/2+1/3 S(1)/2+1/S(3) Out[4]: 5/6 Rational(5,10) Out[5]: 1/2 x,y = symbols ...
- CORS,jsonp解决跨域问题
同源和跨域 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能,如果缺少了同源策略,则浏览器的正常功能可能都会受到影响.可以说Web是构建在同源策略基础之上 ...