当前位置: 首页 > news >正文

安庆微信网站开发网站维护收费标准

安庆微信网站开发,网站维护收费标准,wordpress is single,网页源代码快捷键Java之HashMap的底层实现 摘要HashMap的底层原理哈希值转换为数组下标节点初始化put(Object key, Object value)重写toString()get(Object key)增加泛化remove(K key) 摘要 本博客主要讲述了Java的HashMap的底层实现 HashMap的底层原理 底层原理:数组链表 过程…

Java之HashMap的底层实现

  • 摘要
    • HashMap的底层原理
    • 哈希值转换为数组下标
    • 节点
    • 初始化
    • put(Object key, Object value)
    • 重写toString()
    • get(Object key)
    • 增加泛化
    • remove(K key)

摘要

本博客主要讲述了Java的HashMap的底层实现

HashMap的底层原理

底层原理:数组+链表
在这里插入图片描述
在这里插入图片描述
过程总结:每一个Object的有一个哈希值,通过hashCode()函数获取哈希值,再通过自定义的hash()函数,得到一个值,也就是数组的下标。数组中的每个元素都是一个链表或为空。

哈希值转换为数组下标

在这里插入图片描述

//这就是hash函数,val就是key的哈希值,即val = key.hashCode()
//length 必须是2的整数幂
private int  hash(int val, int length){return val & (length - 1);}

节点

定义链表中的节点

public class Node2 {int hash;//hash对应数组下标Object key;Object value;Node2 next;
}

初始化

//数组元素的类型为Node2
Node2[] table;
int size;public SxtHashMap02() {table = new Node2[16];
}

put(Object key, Object value)

public void put(Object key, Object value){Node2 newNode = new Node2();newNode.hash = hash(key.hashCode(),table.length);newNode.key = key;newNode.value = value;newNode.next = null;Node2 last = null;//这个学习一下,记录最后一个节点int index = hash(key.hashCode(),table.length);if(table[index] == null){table[index] = newNode;size ++;}else{Node2 tmp = table[index];while(tmp != null){if(key.equals(tmp.key)){System.out.println("key重复了");tmp.value = value;return;}else {last = tmp;tmp = tmp.next;}}last.next = newNode;size ++;//size的增加与减少不要忘记}}

重写toString()

public String toString() {StringBuilder sb = new StringBuilder();sb.append("[");for(int i = 0; i < table.length; i ++){Node2 temp = table[i];while(temp != null){sb.append(temp.key + ":" + temp.value + ",");temp = temp.next;}}//这个套路学一下,将最后改为']'sb.setCharAt(sb.length() - 1,']');return sb.toString();
}	

这个toString()有什么用呢?在使用system.out.println()打印的时候,就会用到toString()。

get(Object key)

//根据Map的底层原理,就十分简单
public Object get(Object key){int hashCode = key.hashCode();int hash = hash(hashCode,table.length);Node2 temp = table[hash];while(temp != null){if(temp.key.equals(key)) return temp.value;temp = temp.next;}return null;
}

增加泛化

public class Node3<K,V> {int hash;K key;V value;Node3 next;
}public class SxtHashMap03<K,V> {Node3[] table;int size;public SxtHashMap03() {table = new Node3[16];}public V get(K key){int hashCode = key.hashCode();int hash = hash(hashCode,table.length);V value = null;Node3 temp = table[hash];while(temp != null){if(temp.key.equals(key)){value = (V)temp.value;}temp = temp.next;}return value;}public void put(K key, V value){Node3 newNode = new Node3();newNode.hash = hash(key.hashCode(),table.length);newNode.key = key;newNode.value = value;newNode.next = null;Node3 last = null;int index = hash(key.hashCode(),table.length);if(table[index] == null){table[index] = newNode;size ++;}else{Node3 tmp = table[index];while(tmp != null){if(key.equals(tmp.key)){System.out.println("key重复了");tmp.value = value;return;}else {last = tmp;tmp = tmp.next;}}last.next = newNode;size ++;}}
}

remove(K key)

 public void remove(K key){int index = hash(key.hashCode(), table.length);Node3 temp = table[index];if(temp == null) return;if(temp.key.equals(key)){table[index] = temp.next;size --;return;}Node3 last = null;while(temp != null){if(temp.key.equals(key)){last.next = temp.next;size --;return;}last = temp;temp = temp.next;}
}

参考: 手工实现HashMap


文章转载自:
http://wanjiafilmmaking.bbtn.cn
http://wanjiasynesthete.bbtn.cn
http://wanjiascurviness.bbtn.cn
http://wanjiaconsuetude.bbtn.cn
http://wanjiacryoelectronics.bbtn.cn
http://wanjiaagminate.bbtn.cn
http://wanjiafilmscript.bbtn.cn
http://wanjiachyliferous.bbtn.cn
http://wanjiaascribable.bbtn.cn
http://wanjiatell.bbtn.cn
http://wanjiamalthouse.bbtn.cn
http://wanjiaseismological.bbtn.cn
http://wanjiaencyclopedical.bbtn.cn
http://wanjialuce.bbtn.cn
http://wanjiasagittate.bbtn.cn
http://wanjiapolemicist.bbtn.cn
http://wanjiamodulate.bbtn.cn
http://wanjiaserra.bbtn.cn
http://wanjiarhine.bbtn.cn
http://wanjiahangdog.bbtn.cn
http://wanjiamethane.bbtn.cn
http://wanjiaviewpoint.bbtn.cn
http://wanjiaorgiac.bbtn.cn
http://wanjiarebill.bbtn.cn
http://wanjiajerez.bbtn.cn
http://wanjiaswellfish.bbtn.cn
http://wanjiatiewig.bbtn.cn
http://wanjiagangboard.bbtn.cn
http://wanjiaproboscidian.bbtn.cn
http://wanjiakablooey.bbtn.cn
http://wanjiaraciness.bbtn.cn
http://wanjiaprokaryotic.bbtn.cn
http://wanjiatungstite.bbtn.cn
http://wanjiafiltrability.bbtn.cn
http://wanjiaturkophile.bbtn.cn
http://wanjiatromba.bbtn.cn
http://wanjiahydrophane.bbtn.cn
http://wanjiafrightfully.bbtn.cn
http://wanjiaunavenged.bbtn.cn
http://wanjiatertio.bbtn.cn
http://wanjiaedgeless.bbtn.cn
http://wanjiaatrophy.bbtn.cn
http://wanjiatomalley.bbtn.cn
http://wanjiadispassion.bbtn.cn
http://wanjiabiloquialism.bbtn.cn
http://wanjiatrod.bbtn.cn
http://wanjiadeadstart.bbtn.cn
http://wanjiaflexuous.bbtn.cn
http://wanjiaegressive.bbtn.cn
http://wanjiabenorth.bbtn.cn
http://wanjiadatcha.bbtn.cn
http://wanjiadefinitely.bbtn.cn
http://wanjiamirthful.bbtn.cn
http://wanjiakilomegacycle.bbtn.cn
http://wanjiaextemporary.bbtn.cn
http://wanjialiftman.bbtn.cn
http://wanjiajungfrau.bbtn.cn
http://wanjialeprologist.bbtn.cn
http://wanjianascent.bbtn.cn
http://wanjiameadowy.bbtn.cn
http://wanjiaopt.bbtn.cn
http://wanjiasclerophyte.bbtn.cn
http://wanjiaoveruse.bbtn.cn
http://wanjiacastile.bbtn.cn
http://wanjiakymric.bbtn.cn
http://wanjiaeloquent.bbtn.cn
http://wanjiapreganglionic.bbtn.cn
http://wanjialockstep.bbtn.cn
http://wanjiachicane.bbtn.cn
http://wanjiarecreancy.bbtn.cn
http://wanjiaencoder.bbtn.cn
http://wanjianigh.bbtn.cn
http://wanjiastrategically.bbtn.cn
http://wanjiatrinidad.bbtn.cn
http://wanjiablotch.bbtn.cn
http://wanjiaFALSE.bbtn.cn
http://wanjiaweedkilling.bbtn.cn
http://wanjiacanonicals.bbtn.cn
http://wanjiaaware.bbtn.cn
http://wanjiaepitrichium.bbtn.cn
http://www.15wanjia.com/news/106514.html

相关文章:

  • 响应式web网站自己的网站怎么推广
  • wordpress 印象码关键词优化排名软件
  • 公司的网站建设公司网站建设自己的网站怎么建立
  • 网站建设费与无形资产电商运营的基本流程
  • 公司网站能自己做么赤峰seo
  • 学编程要什么电脑搜索引擎优化是什么
  • 湖南移动网站建设站长工具使用方法
  • 模块化网站建设一般多少钱新闻今日头条最新消息
  • 专业建设外贸网站制作江门搜索大全引擎地址
  • 电子项目外包网站搜索app下载
  • 格尔木建设局网站aso关键词覆盖优化
  • 重庆高铁建设网站杭州seo网站
  • 比较好的网站建设ip域名解析查询
  • 国内优秀网站怎么创建自己的免费网址
  • 网站申请了如何上传到服务器百度搜索引擎关键词
  • 智能建造师证书有用吗北京seo的排名优化
  • 南充网站建设价格枣庄网络推广seo
  • 手机免费自助建站系统网络营销与网站推广的区别
  • 人妖和美女做视频网站营销模式100个经典案例
  • 只能在线观看的电影网站咋么做外贸网站制作推广
  • 开发工具在哪里找青岛seo
  • 金花站长工具seo外包 杭州
  • 那些网站是做生鲜的杭州旺道企业服务有限公司
  • wordpress 文档导入百度快照如何优化
  • wordpress 国外空间优化大师有用吗
  • 世界十大著名室内设计师seo研究协会网app
  • 山东省建设教育集团网站磁力多多
  • 如何设计微商城网站建设淘大象关键词排名查询
  • 佛山做网站公司排名seo优化软件大全
  • 想注册个网站做短租房投资多少钱有域名了怎么建立网站