1,链表法。
public class HashMap<K,V> { public static class Node<K,V> { K key; V value; Node next; public Node(K key, V value, Node<K, V> next) { this.key = key; this.value = value; this.next = next; } } private Node<K,V> tab = new Node<K,V>[16]; private ReentrantLock[] locks = new ReentrantLock[16]; public void put(K key, V value) { if(key == null || value == null) { throw new NullPointerException("illegal arguments"); } int index = indexOf(key); final locker = locks[index]; locker.lock(); try{ Node<K, V> node = tab[index]; if(node == null) { tab[index] = new Node<K,V>(key, value, null); } else { while(node != null && node.key != key && !node.key.equals(key)) { node = node.next; } if(node == null) { tab[index] = new Node<K,V>(key, value, tab[index]); } else { node.value = value; } } } finally { locker.unlock(); } } public V get(K key) { if(key == null) { throw new NullPointerException("illegal arguments"); } int index = indexOf(key); final locker = locks[index]; locker.lock(); try{ Node<K, V> node = tab[index]; while(node != null) { if(node.key == key || node.key.equals(key)) { return node.value; } else { node = node.next; } } return null; } finally { locker.unlock(); } } private int indexOf(K key) { if(key == null) return 0; int hash = key.hashCode(); // return hash%tab.length; return hash&(tab.length-1); // length of tab is 2^n; } }
2,线性探测法。
public class HashTable { Item[] hashArray; int arraySize;//定义数组长度 public HashTable(int size){//构造器,初始化 arraySize = size; hashArray = new Item[arraySize]; } //哈希函数 public int hash(int key){ return key % arraySize; } //插入,这里假设是数组未满,即不能插入大于arraySize的数据数 public void insert(Item item){ int key = item.getKey(); int hashCode = hash(key); //若已存在同样的数据,则向下进一位,直到找到空的位置 //为了简单,也可要求不准有重复数据 while(hashArray[hashCode] != null){ ++hashCode; hashCode %= arraySize; } hashArray[hashCode] = item; } //删除 public Item delete(int key){ int hashCode = hash(key); while(hashArray[hashCode] != null){ if(hashArray[hashCode].getKey() == key){ Item temp = hashArray[hashCode]; hashArray[hashCode] = null; return temp; } ++hashCode; hashCode %= arraySize; } return null; } //查找 public Item find(int key){ int hashCode = hash(key); while(hashArray[hashCode] != null){ if(hashArray[hashCode].getKey() == key) return hashArray[hashCode]; ++hashCode; hashCode %= arraySize; } return null; } } //定义哈希表中存放的数据类型,可以为任意的类型 class Item{ int idata; public Item(int idata){ this.idata = idata; } public int getKey(){ return idata; } }
相关推荐
liballoc 中的 hashmap 默认使用 SipHash,它并没有我们想要的那么快。在编译器中,我们并不真正担心 DOS 尝试,因此我们使用快速非加密哈希。 这与 Firefox 使用的算法相同——它是一种不基于任何广为人知的算法的...
HashMap存放.doc
HashMap是一个散列桶(数组和链表),它存储的内容是键值对(key-value)映射HashMap采用了数组和链表的数据结构,能在查询和修改方便继承了数组的线性查找和链表的寻址修改HashMap是非synchronized,所以HashMap很快...
HashMap为什么是线程不安全的?如何解决HashMap的线程不安全问题?
JNI处理hashmap,string等对象的操作,别处绝对没有的
HashMap之resize()方法源码解读,分两部分概述扩容方法涉及到的处理:创建新数组,将旧数组元素转移到新数组上
hashmap相关的面试题
HashMap数据结构,HashMap的构造方法,HashMap的put,HashMap的get
hashMap和hashTable的区别,大家可以下载学习学习。
HashMap介绍和使用
java hashmap 扩容因子为什么是0.75,官方给出的解释
要求:从键盘输入5本书的名称、单价、购买数量,将这些信息存入一个HashMap,然后将该HashMap作为参数调用方法getSum(HashMap books),该方法用于计算书的总价并返回。【说明:键盘输入可以使用Scanner类】
记得刚毕业那会准备面试,看过不少面试题,里面有个说出HashMap和HashTable不同的题目,我那会面试的时候也遇到不少次这个问题,还隐约记得当时的回答是这样的: HashTable是比较旧的版本;HashTable是线程安全的,...
1、此HashMap类采用java jdk中HashMap的实现方式。2、相比网站上发布过的hashtable之类的源码:。此HashMap寻址方法是拉链法.比开放寻址法对连续内存要求更低。哈希函数用的是java中String.hashCode()算法(经实际验证...
ibatis 用HashMap解决Ibatis未知列名和列数的查询结果的resultClass映射
java中HashMap,LinkedHashMap,TreeMap,HashTable的区别
这个文档“ HashMap详解(通俗易懂)”很好的阐述了hashmap的底层数据结构示意,希望对学习java的人有帮助
本文档主要讲述的是java中HashMap详解;HashMap和HashSet是Java Collection Framework的两个重要成员,其中HashMap是Map接口的常用实现类,HashSet是Set接口的常用实现类。虽然HashMap和HashSet实现的接口规范不同,...
HashMap类.rar
Javascript实现和操作HashMap,压缩包里面有hashmap定义和操作的例子