HashMap Hashtable ArrayList HashSet
一、散列
1. HashMap
1) hashmap的数据结构
Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示:

当我们往hashmap中put元素的时候,先根据key的hash值得到这个元素在数组中的位置(即下标),然后就可以把这个元素放到对应的位置中了。如果这个元素所在的位子上已经存放有其他元素了,那么在同一个位子上的元素将以链表的形式存放,新加入的放在链头,最先加入的放在链尾。
2)使用
Map map = new HashMap();
map.put("Rajib Sarma","100");
map.put("Rajib Sarma","200");//The value "100" is replaced by "200".
map.put("Sazid Ahmed","200");
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
2. HashTable和HashMap区别
第一,继承不同。
public class Hashtable extends Dictionary implements Map
public class HashMap extends AbstractMap implements Map
第二
Hashtable 中的方法是同步的,而HashMap中的方法在缺省情况下是非同步的。在多线程并发的环境下,可以直接使用Hashtable,但是要使用HashMap的话就要自己增加同步处理了。
第三
Hashtable中,key和value都不允许出现null值。
在HashMap中,null可以作为键,这样的键只有一个;可以有一个或多个键所对应的值为null。当get()方法返回null值时,即可以表示 HashMap中没有该键,也可以表示该键所对应的值为null。因此,在HashMap中不能由get()方法来判断HashMap中是否存在某个键, 而应该用containsKey()方法来判断。
第四,两个遍历方式的内部实现上不同。
Hashtable、HashMap都使用了 Iterator。而由于历史原因,Hashtable还使用了Enumeration的方式 。
第五
哈希值的使用不同,Hashtable直接使用对象的hashCode。而HashMap重新计算hash值。
1)HashMap的hash方法:
final int hash(Object k) {
int h = hashSeed;
if (0 != h && k instanceof String) {
return sun.misc.Hashing.stringHash32((String) k);
}
h ^= k.hashCode();
// This function ensures that hashCodes that differ only by
// constant multiples at each bit position have a bounded
// number of collisions (approximately 8 at default load factor).
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
2)Hashtable的hash方法:
private int hash(Object k) {
// hashSeed will be zero if alternative hashing is disabled.
return hashSeed ^ k.hashCode();
}
第六
Hashtable和HashMap它们两个内部实现方式的数组的初始大小和扩容的方式。HashTable中hash数组默认大小是11,增加的方式是 old*2+1。
HashMap中hash数组的默认大小是16,而且一定是2的指数。
HashMap Hashtable ArrayList HashSet的更多相关文章
- HashMap, HashTable,HashSet,TreeMap 的时间复杂度
hashSet,hashtable,hashMap 都是基于散列函数, 时间复杂度 O(1) 但是如果太差的话是O(n) TreeSet==>O(log(n))==> 基于树的搜索,只需要 ...
- OAF_开发系列17_实现OAF数组应用Vector / Hashmap / Hashtable / Arraylist(案例)
20150506 Created By BaoXinjian
- Set集合[HashSet,TreeSet,LinkedHashSet],Map集合[HashMap,HashTable,TreeMap]
------------ Set ------------------- 有序: 根据添加元素顺序判定, 如果输出的结果和添加元素顺序是一样 无序: 根据添加元素顺序判定,如果输出的结果和添加元素的顺 ...
- 【转】HashMap、TreeMap、Hashtable、HashSet和ConcurrentHashMap区别
转自:http://blog.csdn.net/paincupid/article/details/47746341 一.HashMap和TreeMap区别 1.HashMap是基于散列表实现的,时间 ...
- 基于散列的集合 HashSet\HashMap\HashTable
HashSet\HashMap\HashTable 1 基于散列的集合 2 元素会根据hashcode散列,因此,集合中元素的顺序不一定与插入的顺序一致. 3 根据equals方法与hashCode方 ...
- Java基础系列 - JAVA集合ArrayList,Vector,HashMap,HashTable等使用
package com.test4; import java.util.*; /** * JAVA集合ArrayList,Vector,HashMap,HashTable等使用 */ public c ...
- HashMap、Hashtable 以及HashSet
关于多线程的问题大多会涉及到Collection框架,涉及到Collection框架就不得不谈HashSet和HashMap.HashMap和HashSet都是collection框架的一部分,它们让 ...
- HashMap,HashTable,TreeMap区别和用法
开始学HashTable,HashMap和TreeMap的时候比较晕,觉得作用差不多,但是到实际运用的时候又发现有许多差别的.需要大家注意,在实际开发中以需求而定. java为数据结构中的映射定义了一 ...
- Collections+Iterator 接口 | Map+HashMap+HashTable+TreeMap |
Collections+Iterator 接口 1. Collections 是一个操作 Set.List 和 Map 等集合的工具类 Collections 中提供了大量方法对集合元素进行排序.查询 ...
随机推荐
- 关于Socket编写简单聊天工具的总结(原创)
这段时间再看socket编程,虽然现在是刚刚接触,但是还是忍不住想写一篇总结,来激励自己努力学习,写的不好的地方,还请大家指教啊! 下面针对一个简单的发送消息和文件的程序说说吧. 首先是服务器需要 ...
- Transact-SQL 存储过程(c#调用执行)
1. Microsoft SQL Server Management Studio 中创建 存储过程 1.1 借助模板资源管理器中的Stored Procedure模板进行修改创建 1.2 直接新建查 ...
- C#调用IOS推送
C#调用IOS推送 使用的是 PushSharp 开源库 源码代码如下 点我
- CSS3中的变形处理(transform)属性
在CSS3中,可以利用transform功能来实现文字或图像的旋转.扭曲.缩放.位移.矩阵.原点这六种类型的变形处理,下面将详细讲解transform的使用. 变形--旋转 rotate() div. ...
- 字符串做异或使用union
#include <stdio.h> #include <sys/time.h> #include <string.h> union data { unsigned ...
- [摘] SQLPLUS Syntax
You use the SQLPLUS command at the operating system prompt to start command-line SQL*Plus: SQLPLUS [ ...
- ems lite 客户端远程连接mysql server
在本地用ems客户端远程连接虚拟机上的mysql server,弹出客户端没有权限访问mysql server.使用下面方法进行设置:mysql> select host,user,passwo ...
- Js 处理将时间转换 “年-月-日”
将时间 \/Date(1432828800000+0800)\/" 转换成:“年-月-日” //时间转换function ChangeDateFormat(val) { if (v ...
- html template
https://wrapbootstrap.com/tag/single-page http://themeforest.net/ https://wrapbootstrap.com/themes h ...
- MoonWarriors-lua——《雷电战机》游戏-Lua移植版
MoonWarriors是一个使用Cocos2d-Html5引擎开发的类似雷电战机的游戏Demo,源代码发布在Cocos2d-x官网的引擎示例当中.MoonWarriors-lua是为了学习Cocos ...