Hashtable与HashMap区别(2)
提到hashtable,先要澄清两个问题hashCode与equals().Hashtable有容量和加载因子,容量相当于桶,因子相当于桶里的对象.而hashCode我们可以把它理解为桶的序号,所以HashCode相同的,即它们在同一个桶里,这两上对象就在同一个桶里,这时候如果他们还equals()的话,那么这两个对象就是一样的.而如果他们的hashCode不一样,即他们不在同一个桶里,那么这两个对象肯定是不一样的.
所以我们在用hashtable进行存储对象时要重写他们的hashCode与equals(),否则会出现很多重复的对象.
hashtable与hashMap最大的区别是,hashtable是方法同步的,而后者是异步的.
先来看看下面的例子吧:
- public class Ball {
- private int size;
- private String name;
- public Ball(int size, String name) {
- this.size = size;
- this.name = name;
- }
- public void setSize(int size) {
- this.size = size;
- }
- public int getSize() {
- return size;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public static void main(String[] args) {
- Hashtable<Ball, String> hash = new Hashtable<Ball, String>();
- , "one"), "1");
- , "two"), "2");
- , "one"), "1");
- System.out.println(hash.size());
- }
- }
可能有的人会认为打印出出来的结果当然是2,有一个是重复的
但结果是3.
因为默认它们都是用Object对象来实现比较的,所以它们的hashCode当然是不一样的
我们可以加以下代码检查是否是这样
- @Override
- public int hashCode() {
- System.out.println(super.hashCode());
- return super.hashCode();
- }
那么,如何才能去掉重复的对象的
这里我们必须重写hashCode,如下,我们可以把size相同的对象放入同一个桶里,再比较他们的name
把hashCode方法改为
- @Override
- public int hashCode() {
- System.out.println(size);
- return size;
- }
- 写equals();
- <pre class="java" name="code"> @Override
- public boolean equals(Object obj) {
- if (obj instanceof Ball) {
- Ball ball = (Ball) obj;
- return name.equals(ball.getName());
- }
- return false;
- }</pre>
再试试,结果就是为2.
hashtable再每添加一个对象的都会先判断他们的hashCode是否一样,如果一样再判断他们是否equals(),如果返回为true的话,那么这个对象将不添加
我们也可以看一下源代码实现
- /**
- * Maps the specified <code>key</code> to the specified
- * <code>value</code> in this hashtable. Neither the key nor the
- * value can be <code>null</code>. <p>
- *
- * The value can be retrieved by calling the <code>get</code> method
- * with a key that is equal to the original key.
- *
- * @param key the hashtable key
- * @param value the value
- * @return the previous value of the specified key in this hashtable,
- * or <code>null</code> if it did not have one
- * @exception NullPointerException if the key or value is
- * <code>null</code>
- * @see Object#equals(Object)
- * @see #get(Object)
- */
- public synchronized V put(K key, V value) {
- // Make sure the value is not null
- if (value == null) {
- throw new NullPointerException();
- }
- // Makes sure the key is not already in the hashtable.
- Entry tab[] = table;
- int hash = key.hashCode();
- int index = (hash & 0x7FFFFFFF) % tab.length;
- for (Entry<K,V> e = tab[index] ; e != null ; e = e.next) {
- if ((e.hash == hash) && e.key.equals(key)) {
- V old = e.value;
- e.value = value;
- return old;//这里如果比较结果相同时就返回原来的值
- }
- }
- modCount++;
- if (count >= threshold) {
- // Rehash the table if the threshold is exceeded
- rehash();
- tab = table;
- index = (hash & 0x7FFFFFFF) % tab.length;
- }
- // Creates the new entry.
- Entry<K,V> e = tab[index];
- tab[index] = new Entry<K,V>(hash, key, value, e);
- count++;
- return null;
- }
hashMap允许键与值为空.其他的和hashtable是一样的.
具体可再参考API...
Hashtable与HashMap区别(2)的更多相关文章
- Java中List,ArrayList、Vector,map,HashTable,HashMap区别用法
Java中List,ArrayList.Vector,map,HashTable,HashMap区别用法 标签: vectorhashmaplistjavaiteratorinteger ArrayL ...
- Hashtable和HashMap区别
Hashtable和HashMap区别 相同点: 实现原理,功能相同,可以互用 主要区别: a.hashtable继承Directionary类,HashMap实现Map接口 b.Hashtable线 ...
- 【Java】HashTable和HashMap区别
①继承不同 public class Hashtable extends Dictionary implements Map public class HashMap extends Abstract ...
- HashTable 和 HashMap 区别
hashMap去掉了HashTable 的contains方法,但是加上了containsValue()和containsKey()方法. hashTable同步的,而HashMap是非同步的,效率上 ...
- HashTable和HashMap的区别详解(转)
一.HashMap简介 HashMap是基于哈希表实现的,每一个元素是一个key-value对,其内部通过单链表解决冲突问题,容量不足(超过了阀值)时,同样会自动增长. HashMap是非线程安全的, ...
- HashTable和HashMap的区别详解
一.HashMap简介 HashMap是基于哈希表实现的,每一个元素是一个key-value对,其内部通过单链表解决冲突问题,容量不足(超过了阀值)时,同样会自动增长. HashMap是非线程安全的, ...
- HashTable、HashMap、HashSet
1. HashMap 1) hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashmap中put元素的时候,先根据key的hash ...
- Hashtable、HashMap
JDK1.6 API public class Hashtable<K,V>extends Dictionary<K,V>implements Map<K,V>, ...
- Hashtable和HashMap类的区别
Hashtable和HashMap类有三个重要的不同之处.第一个不同主要是历史原因.Hashtable是基于陈旧的Dictionary类的,HashMap是Java 1.2引进的Map接口的一个实现. ...
随机推荐
- PHP学习之开发工具
刚接触PHP,必然需要一套完整的开发工具.每个语言都有各种各样的编辑工具.采用了相对来说比较了解的Eclipse来作为开发工具. 1.要是用Eclipse需要安装JDK或JRE(Eclipse本身就是 ...
- Discuz x2.5 单页制作的教程
首先,单页包括该单页的php文件和该单页的模板(.htm)文件,比如:host.php.host.htm 单页的php文件内容如下: <?php require './source/class/ ...
- 利用IDE编写C语言程序的一点注意事项
前言:我是喜欢编程的一只菜鸟,在自学过程中,对遇到的一些问题和困惑,有时虽有一点体会感悟,但时间一长就会淡忘,很不利于知识的积累.因此,想通过博客园这个平台,一来记录自己的学习体会,二来便于向众多高手 ...
- The connection to adb is down
有一周时间没有打开ADT了,最近想为我的APP增加下些新的功能,但是在编译的时候出现了一个奇怪问题 [2013-10-18 14:43:50 - zzbus] Android Launch![2013 ...
- 2016 系统设计第一期 (档案一)MVC form数据提交
前几天我发现 MVC 虽然解决了webform的问题但是用起来真的很麻烦,不知道是我刚接触的原因还是为什么,感觉有很多的局限性,对于form的提交一个form只能绑定一个action,代码如下: @u ...
- Contest2037 - CSU Monthly 2013 Oct (problem F :ZZY and his little friends)
http://acm.csu.edu.cn/OnlineJudge/problem.php?cid=2037&pid=5 [题解]: 没想通这题暴力可以过.... [code]: #inclu ...
- Rust入门篇 (1)
Rust入门篇 声明: 本文是在参考 The Rust Programming Language 和 Rust官方教程 中文版 写的. 个人学习用 再PS. 目录这东东果然是必须的... 找个时间生成 ...
- 分享SCI写作经验和一些工具
- OD之常用命令
一:od断点注释保存的问题,由于od只有在正常退出的情况下才会保存分析代码时留下的注释,而很多时候为了在退出od时不让目标程序退出使用了剥离进程,这样就会导致这次操作所有的注释都没有保存,第二次重新载 ...
- 入侵HP打印机的文件系统
计算机入侵可听多了,然而打印机入侵相信大家可很少听过吧.如今,很大一部分的打印机已经网络化了,能入侵和控制打印机不仅能用来进行DDOS,而内部的文件系统更是绝好的秘密文件收藏空间.现在就来谈谈如何入侵 ...