本人才疏学浅,手写HashMap以供学习,提供出来供大家参考
手写的hashMap
- map的接口内容了解和学习
- 接口增删改查实现
- 迭代器实现
- hash表实现,链表实现,二叉树未实现。
测试类
package cmap;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
public class Text
{
public static void main( String[] args )
{
Map<Integer,Object> map = new ChashMap<Integer,Object>();
map.put( null, "键值为null的内容的集合整理" );
map.put( 123, "456" );
map.put( 124, "789" );
map.put( 125, "147" );
map.put( 126, "258" );
System.out.println( map.get( null ) );
map.remove( null );
System.out.println( map.get( null ) );
System.out.println( map.get( 125 ) );
map.put( 125, "对125进行修改之后的125" );
System.out.println( map.get( 125 ) );
map.clear();
Map<Integer,Object> map1 = new HashMap<Integer,Object>();
map1.put( null, "预防出现问题的值" );
map1.put( 123, "一二三" );
map1.put( 127, "一二七" );
map1.put( 128, "一二八" );
map.putAll( map1 );
for(Entry< Integer, Object > i:map.entrySet()) {
System.out.println( i.toString() );
}
}
}
显示的内容结果
键值为null的内容的集合整理
null
147
对125进行修改之后的125
[0:0]-key:null,value:预防出现问题的值
[0:128]-key:128,value:一二八
[11:123]-key:123,value:一二三
[15:127]-key:127,value:一二七
手写的源码
package cmap;
import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class ChashMap< K, V > implements Map< K, V >,Serializable {
// 定义对象指向自己
ChashMap< K, V > That = this;
private static final long serialVersionUID = 4305955127605445131L;
/**
* 默认的初始化容量16
*/
private static final int DEFAULT_INITIAL_CAPATITY = 1<<4;
/**
* 最大的容量
*/
private static final int MAX_CAPATITY = 1<<32;
/**
* 定义当前map实际属性的多少
*/
private int size = 0;
/** table的容量 */
private int tableCapatity;
// 扩容部分内容未完成
/** 扩容系数 */
private float dilatationCoefficient = 0.75F;
/** 扩容数值 */
private int dilatationNum = 0;
/**
* 定义Key的set队列
*/
private Set< Entry< K, V > > keySet = null;
/**
* 定义value的内容
*/
private Collection<V> values = null;
/**
* 定义属性节点内容
*/
class Node<K,V> implements Map.Entry< K, V > {
// hash值
final int hash;
// 主键
final K key;
// 值
V value;
// 下个节点
Node<K,V> next;
public Node(int hash,K k,V v,Node<K,V> next){
this.hash = hash;
this.key = k;
this.value = v;
this.next = next;
}
@Override
public K getKey()
{
return key;
}
@Override
public V getValue()
{
return value;
}
@Override
public V setValue( V value )
{
V oldValue = this.value;
if(null != value) {
this.value = value;
}
return oldValue;
}
@Override
public boolean equals( Object obj )
{
// 此处应该添加引用相等或者值相等,未实现
return super.equals( obj );
}
@Override
public String toString()
{
return "["+(hash&(That.table.length-1))+":"+hash+"]-key:"+key+",value:"+value;
}
}
/**
* 定义hash节点的保存对象
*/
private Node<K,V>[] table = null;
public ChashMap() {
this.tableCapatity = DEFAULT_INITIAL_CAPATITY;
}
/**
* 初始化hashTable初始大小和扩容系数
* @param tableCapatity 容积大小
* @param dilatationCoefficient
*/
public ChashMap(int tableCapatity) {
if(tableCapatity<DEFAULT_INITIAL_CAPATITY) {
this.tableCapatity = DEFAULT_INITIAL_CAPATITY;
}
else {
this.tableCapatity = getLegal(tableCapatity);
}
}
/**
* 初始化hashTable初始大小和扩容系数
* @param tableCapatity 容积大小
* @param dilatationCoefficient
*/
public ChashMap(int tableCapatity,float dilatationCoefficient) {
if(tableCapatity<DEFAULT_INITIAL_CAPATITY) {
this.tableCapatity = DEFAULT_INITIAL_CAPATITY;
}
else {
this.tableCapatity = getLegal(tableCapatity);
}
this.dilatationCoefficient = dilatationCoefficient;
}
// 获取合法的table容量大小
int getLegal(int capatity){
// >>> 无符号右移
capatity |= capatity>>>1;
capatity |= capatity>>>2;
capatity |= capatity>>>4;
capatity |= capatity>>>8;
capatity |= capatity>>>16;
if(capatity>MAX_CAPATITY) {
capatity = MAX_CAPATITY;
}
return capatity;
}
@SuppressWarnings("unchecked")
void tableInit() {
if(null == table) {
table = (Node< K, V >[])new Node[tableCapatity];
}
}
final int hash(Object key) {
int hash;
// 源码中及其简练的写法,对hash右移16位之后与原hash进行异或
return key == null ? 0 : (hash=key.hashCode()) ^ (hash>>>16);
}
@Override
public V put( K key, V value )
{
// 初始化table
tableInit();
int t = table.length-1;
int index = hash(key)&t;
// 判断当前的内容有没有
if(table[index]!=null) {
/**
* 当前节点存在内容,则逐一对比key,存在则覆盖,不存在则下一个,已经到最后了,添加至最后
*/
for(Node<K,V> local = table[index];;) {
if(local.key == key) {
V oldValue = local.value;
// 可以认为是两者相等了
local.setValue( value );
return oldValue;
}
// key不相等,下一个节点是否为空
if(local.next == null) {
// 为空插入
local.next = new Node<K,V>(hash(key),key,value,null);
size++;
break;
}
// 循环下一个
local = local.next;
}
}
else {
// 当前节点为空,则存放内容
table[index] = new Node<K,V>(hash(key),key,value,null);
size++;
}
return null;
}
@Override
public V get( Object key )
{
Node<K,V> local = getNode(key);
return local==null ? null : local.value;
}
public Node<K,V> getNode(Object key){
if(isEmpty()) {
return null;
}
// 获取对应的内容数据
int hash = hash(key);
int index = hash&(table.length-1);
if(table[index]!=null) {
for(Node<K,V> local = table[index];;) {
if(local.key == key) {
return local;
}
// key不相等,下一个节点是否为空
if(local.next == null) {
return null;
}
// 循环下一个
local = local.next;
}
}
else
return null;
}
@Override
public int size()
{
return size;
}
@Override
public boolean isEmpty()
{
return size<=0;
}
/**
* 当前key的列表中,是否存在当前的key值
*/
@Override
public boolean containsKey( Object key )
{
return false;
}
/**
* 当前value的列表中,是否存在当前的value值
*/
@Override
public boolean containsValue( Object value )
{
return false;
}
@Override
public V remove( Object key )
{
if(isEmpty()) {
return null;
}
// 获取对应的内容数据
int hash = hash(key);
int index = hash&(table.length-1);
if(table[index]!=null) {
for(Node<K,V> local = table[index],prev = null;;) {
if(local.key == key) {
V old = local.value;
// 删除链表的相关内容
// 根节点
if(local == table[index]) {
table[index] = local.next;
local.next = null;
local = null;
size --;
return old;
}
// 根节点,prev肯定为空
else {
prev.next = local.next;
local.next = null;
local = null;
size --;
return old;
}
}
// key不相等,下一个节点是否为空
if(local.next == null) {
return null;
}
// 循环下一个
prev = local;
local = local.next;
}
}
else
return null;
}
@Override
public void putAll( Map< ? extends K, ? extends V > m )
{
if(!m.isEmpty()) {
for(Entry< ? extends K, ? extends V > entry:m.entrySet()) {
this.put( entry.getKey(), entry.getValue() );
}
}
}
@Override
public void clear()
{
if(!isEmpty()) {
int len = table.length;
for(int i=0;i<len;i++) {
if(table[i]!=null) {
for(Node<K,V> local = table[i],next = null;;) {
if(local.next != null) {
next = local.next;
}
local.next = null;
if(next == null) {
break;
}
local = next;
}
table[i] = null;
}
}
table = null;
size = 0;
}
}
@Override
public Set< K > keySet()
{
return null;
}
@Override
public Collection< V > values()
{
return null;
}
@Override
public Set< Entry< K, V > > entrySet()
{
if(null == this.keySet) {
this.keySet = new Cset();
}
return keySet;
}
class Citerator implements Iterator<Entry< K, V >>{
private int hashIndex = -1;
private Node<K,V> next = null,local = null;
public Citerator(){
hashIndex = -1;
getNext();
}
private Node<K,V> getNext(){
if(!That.isEmpty()) {
int len = table.length;
// 说明进行的是初始化
if(hashIndex < 0) {
hashIndex = 0;
}
if(next == null|| next.next == null) {
if(next!=null && next.next == null) {
hashIndex++;
}
for(int i=hashIndex;i<len;i++) {
if(table[i]!=null) {
next = table[i];
hashIndex = i;
return next;
}
}
// 运行到这里,证明没有下一个了
next = null;
}
else {
next = next.next;
return next;
}
}
return null;
}
@Override
public boolean hasNext()
{
return next!=null;
}
@Override
public Entry< K, V > next()
{
// 当前节点是什么不重要,获取下个节点,返回的节点就成为当前节点了。
local = next;
getNext();
return local;
}
@Override
public void remove()
{
That.remove(local.key);
}
}
class Cset implements Set<Entry< K, V >>{
@Override
public int size()
{
return That.size();
}
@Override
public boolean isEmpty()
{
return That.isEmpty();
}
// 包含
@SuppressWarnings("unchecked")
@Override
public boolean contains( Object o )
{
if(!(o instanceof Map.Entry)||null == o) {
return false;
}
Map.Entry<K,V> entry = That.getNode( ((Map.Entry<K,V>)o).getKey() );
return entry!=null&&entry.equals( o );
}
// 迭代器
@Override
public Iterator< Entry< K, V > > iterator()
{
return new Citerator();
}
@Override
public Object[] toArray()
{
Iterator<Entry< K, V >> iter = iterator();
Object[] obj = new Object[That.size];
for(int i=0;i<That.size;i++) {
obj[i] = iter.next();
}
return obj;
}
@Override
public < T > T[] toArray( T[] a )
{
return null;
}
@Override
public boolean add( Entry< K, V > e )
{
That.put(e.getKey(),e.getValue());
return true;
}
@Override
public boolean remove( Object o )
{
return That.remove(o)!=null;
}
@Override
public boolean containsAll( Collection< ? > c )
{
return false;
}
@Override
public boolean addAll( Collection< ? extends Entry< K, V > > c )
{
return false;
}
@Override
public boolean retainAll( Collection< ? > c )
{
return false;
}
@Override
public boolean removeAll( Collection< ? > c )
{
return false;
}
@Override
public void clear()
{
That.clear();
}
}
}