HashMap的put方法返回值问题
API文档中的描述:
先看一个例子
Map<Character, Integer> map = new HashMap<Character, Integer>();
System.out.println(map.put('a', 0)); // null
System.out.println(map.put('a', 1)); //
System.out.println(map.put('a', 2)); //
System.out.println(map.put('b', 1)); // null
System.out.println(map.put('b', 2)); //
System.out.println(map.get('a')); //
可以看出:put方法的返回值为null或value;
调用put方法时,如果已经存在一个相同的key, 则返回的是前一个key对应的value,同时该key的新value覆盖旧value;
如果是新的一个key,则返回的是null;
通过hashmap的源码可以看出:
map中一个映射不能包含重复的键。每个键最多只能映射一个值。即相同的key在Map中只会有一个与之关联的value存在。
put()方法实现:首先使用hash(key)得到key的hashcode(),hashmap根据获得的hashcode找到要插入的位置所在的链,在这个链里面放的都是hashcode相同的Entry键值对,
在找到这个链之后,会通过equals()方法判断是否已经存在要插入的键值对,而这个equals比较的就是key。
添加对应的key-value这样的键值对node时,如果原本已经存在相同的key,则直接改变对应的value,并返回旧的value;如果不存在相同的key,则插入,在插入链表时,如果链表长度为临界长度TREEIFY_THRESHOLD,再插入任何元素就要变成红黑树。
public V put(K key, V value) {
return putVal(hash(key), key, value, false, true);//put方法调用putVal方法
} /**
* Implements Map.put and related methods
*
* @param hash hash for key
* @param key the key
* @param value the value to put
* @param onlyIfAbsent if true, don't change existing value
* @param evict if false, the table is in creation mode.
* @return previous value, or null if none
*/
final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
boolean evict) {
Node<K,V>[] tab; Node<K,V> p; int n, i;
if ((tab = table) == null || (n = tab.length) == 0)
n = (tab = resize()).length;
if ((p = tab[i = (n - 1) & hash]) == null)
tab[i] = newNode(hash, key, value, null);
else {
Node<K,V> e; K k;
if (p.hash == hash &&
((k = p.key) == key || (key != null && key.equals(k))))
e = p;
else if (p instanceof TreeNode)
e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
else {
for (int binCount = 0; ; ++binCount) {
if ((e = p.next) == null) {
p.next = newNode(hash, key, value, null);
if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
treeifyBin(tab, hash);
break;
}
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
break;
p = e;
}
}
if (e != null) { // existing mapping for key //key已经存在
V oldValue = e.value; //将已经存在的结点e的value值赋给oldValue
if (!onlyIfAbsent || oldValue == null)//当onlyIfAbsent为false或者oldValue为null时,进行覆盖操作
e.value = value;//覆盖原结点的value值,用新值替换旧值
afterNodeAccess(e);
return oldValue;//返回的是被覆盖的oldValue
}
}
++modCount;
if (++size > threshold)
resize();
afterNodeInsertion(evict);
return null;
}
在hashset中的add方法调用的就是hashmap的put方法,判断put方法的返回值是否等于空,来保证hashset的值不重复。
private transient HashMap<E,Object> map; /**
* Adds the specified element to this set if it is not already present.
* More formally, adds the specified element <tt>e</tt> to this set if
* this set contains no element <tt>e2</tt> such that
* <tt>(e==null ? e2==null : e.equals(e2))</tt>.
* If this set already contains the element, the call leaves the set
* unchanged and returns <tt>false</tt>.
*
* @param e element to be added to this set
* @return <tt>true</tt> if this set did not already contain the specified
* element
*/
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
HashMap的put方法返回值问题的更多相关文章
- Spring MVC--------处理方法返回值的可选类型
对于Spring MVC处理方法支持支持一系列的返回方式: (1)ModelAndView (2)Model (3)ModelMap (4)Map (5)View (6)String (7)Void ...
- C# 方法返回值的个数
方法返回值类型总的来说分为值类型,引用类型,Void 有些方法显示的标出返回值 public int Add(int a,int b) { return a+b; } 有些方法隐式的返回返回值,我们可 ...
- 使用Result代替ResultSet作为方法返回值
在开发过程中,我们不能将ResultSet对象作为方法的返回值,因为Connection连接一旦关闭,在此连接上的会话和在会话上的结果集也将会自动关闭,而Result对象则不会发生这种现象,所以在查询 ...
- AJAX JQuery 调用后台方法返回值(不刷新页面)
AJAX JQuery 调用后台方法返回值(不刷新页面) (1)无参数返回值(本人亲试返回结果不是预期结果) javascript方法: $(function () { //无 ...
- SpringMVC第五篇【方法返回值、数据回显、idea下配置虚拟目录、文件上传】
Controller方法返回值 Controller方法的返回值其实就几种类型,我们来总结一下-. void String ModelAndView redirect重定向 forward转发 数据回 ...
- 关于java字节流的read()方法返回值为int的思考
我们都知道java中io操作分为字节流和字符流,对于字节流,顾名思义是按字节的方式读取数据,所以我们常用字节流来读取二进制流(如图片,音乐 等文件).问题是为什么字节流中定义的read()方法返回值为 ...
- java 反射获取方法返回值类型
//ProceedingJoinPoint pjp //获取方法返回值类型 Object[] args = pjp.getArgs(); Class<?>[] paramsCls = ne ...
- js 获取getElementsTagName()方法返回值的内容
<div id="news-top" class="section"> <h3>Some title</h3> <di ...
- eclipse自动生成变量名声明(按方法返回值为本地变量赋值)
eclipse自动生成变量名声明(按方法返回值为本地变量赋值) ctrl+2+L 这个快捷键可自动补全代码,极大提升编码效率! 注:ctrl和2同时按完以后释放,再快速按L.不能同时按! 比如写这句代 ...
随机推荐
- UWP 五星评价(不跳转到龟速商店)
之前写过一篇文章 UWP 五星好评 代码如下 var pfn = Package.Current.Id.FamilyName; await Launcher.LaunchUriAsync(new ...
- 为什么 Action/ViewController/ProperttyEditor不可见或不可用?
英文版:https://documentation.devexpress.com/eXpressAppFramework/112818/Concepts/Extend-Functionality/De ...
- Direct2D处理几何图形之间的碰撞检测(上)
转载请注明出处:http://www.cnblogs.com/Ray1024 一.概述 Direct2D中支持以下几种类型的几何图形: a.简单几何图形(Simple Geometry):矩形.圆角矩 ...
- 41F继电器座的解剖与妙用
摘要:如果继电器不是焊在电路板上使用,就需要有个插座,这样方便接线,否则继电器的管脚是没法固定导线的.实际项目中使用了HF41F的继电器(宏发),在选择继电器座的时候,有一点感想,分享给大家.继电器是 ...
- VMware Workstation and Device/Credential Guard are not compatible
VMware Workstation and Device/Credential Guard are not compatible. VMware Workstation can be run aft ...
- linux运维升级路线
运维工程师是从一个呆逼进化为苦逼再成长为牛逼的过程,前提在于你要能忍能干能拼,还要具有敏锐的嗅觉感知前方潮流变化.如:今年大数据,人工智能比较火……(相对表示就是 Python 比较火) 之前写过运维 ...
- umount命令详解
基础命令学习目录首页 umount 用来卸载设备 -a:卸除/etc/mtab中记录的所有文件系统: -h:显示帮助: -n:卸除 ...
- Daily Scrum (2015/11/4)
因为距离部署的时间临近,而之前我们的进度偏慢.这天晚上我们大多数成员几乎所有时间都用在了这个项目上,成果还算令人满意. 成员 今日任务 时间 明日任务 符美潇 1.修复了一个BUG,此BUG会导致所爬 ...
- 课堂练习 psp表
项目计划总结表: 日期 编程 完善程序 测试程序 参考资料 日总结 3.20 18:00---19:30 1.5 3.21 9:30----10:00 10:00---10:30 ...
- 使用成员资格管理用户Membership
ASP.NET成员资格使您可以验证和管理Web应用程序的用户信息.它提供验证用户凭据,创建和修改成员资格用户以及管理用户设置(如密码和电子邮件地址)的功能. ASP.NET成员资格主要用于ASP.NE ...