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

英文网站建设 淮安国外网站网站app

英文网站建设 淮安,国外网站网站app,北京大学网站开发的需求分析,中国建筑集团有限公司官网首页hashMap常见面试题总览 为什么重写Equals还要重写HashCode方法#xff1f;HashMap如何避免内存泄漏问题#xff1f;HashMap1.7底层是如何实现的#xff1f;HashMapKey为null存放在什么位置#xff1f;HashMap如何解决Hash冲突问题#xff1f;HashMap底层采用单链表还是双…hashMap常见面试题总览 为什么重写Equals还要重写HashCode方法HashMap如何避免内存泄漏问题HashMap1.7底层是如何实现的HashMapKey为null存放在什么位置HashMap如何解决Hash冲突问题HashMap底层采用单链表还是双链表HashMap根据key查询的时间复杂度HashMap如何实现数组扩容问题HashMap底层是有序存放的吗LinkedHashMap 和 TreeMap底层如何实现有序的HashMap底层如何降低Hash冲突概率HashMap中hash函数是怎么实现的为什么不直接将key作为哈希值而是与高16位做异或运算HashMap如何存放1万条key效率最高HashMap高低位与取模运算有那些好处HashMap1.8如何避免多线程扩容死循环问题为什么加载因子是0.75而不是1为什么HashMap1.8需要引入红黑树为什么链表长度8需要转红黑树而不是6什么情况下需要从红黑树转换成链表存放HashMap底层如何降低Hash冲突概率如何在高并发的情况下使用HashMapConcurrentHashMap底层实现的原理 为什么重写equals还要重写hashCode方法 HashMap如何避免内存泄漏问题 回答 为什么重写Equals还要重写HashCode方法HashMap如何避免内存泄漏问题 Hashcode方法底层采用C语言编写根据对象内存地址转换成整数类型 public native int hashCode();基础1: 两个对象的hashCode值相等对象不一定相等** String a1a;Integer a297;//97 97System.out.println(a1.hashCode());System.out.println(a2.hashCode());引发hash碰撞问题所以还要重写EqualsString类型的Equals方法已经帮我们重写所以下面比较是不相等的 String a1a;Integer a297;//falseSystem.out.println(a1.equals(a2));String类型的Equals /*** Compares this string to the specified object. The result is {code* true} if and only if the argument is not {code null} and is a {code* String} object that represents the same sequence of characters as this* object.** param anObject* The object to compare this {code String} against** return {code true} if the given object represents a {code String}* equivalent to this string, {code false} otherwise** see #compareTo(String)* see #equalsIgnoreCase(String)*/public boolean equals(Object anObject) {if (this anObject) {return true;}if (anObject instanceof String) {String anotherString (String)anObject;int n value.length;if (n anotherString.value.length) {char v1[] value;char v2[] anotherString.value;int i 0;while (n-- ! 0) {if (v1[i] ! v2[i])return false;i;}return true;}}return false;}Integer类型的Equals /*** Compares this object to the specified object. The result is* {code true} if and only if the argument is not* {code null} and is an {code Integer} object that* contains the same {code int} value as this object.** param obj the object to compare with.* return {code true} if the objects are the same;* {code false} otherwise.*/public boolean equals(Object obj) {if (obj instanceof Integer) {return value ((Integer)obj).intValue();}return false;}基础2:两个对象equals相等hashCode值不一定相等 UserEntity userEntity new UserEntity(wei, 18);UserEntity userEntity1 new UserEntity(wei, 18);//false 内存地址肯定不相等System.out.println(userEntityuserEntity1);//false Object默认实现了equals方法本质还是比较内存地址System.out.println(userEntity.equals(userEntity1));Object类的equals public boolean equals(Object obj) {return (this obj);}结论重写equals必须重写hashCode方法。 阿里规范 1.只要重写equals必须重写hashCode方法【强制】 2.Set、Map底层用hashCode和equals进行判断相等 3.String已重写equals和hashCode所以可以用字符串作为Key【推荐】 不重写equals必须重写hashCode方法会导致内存无法回收内存泄漏问题同一个对象一直创建新的空间存放。 HashMap1.7底层是如何实现的 HashMapKey为null存放在什么位置 基础HashMap与HashTable之间的区别 HashMap底层如何实现 1.ArrayList集合实现不需要考虑hash碰撞时间复杂度为On 2. JDK1.7基于数组和链表(hkey.hashCode)^h16不发生冲突为O1发生冲突链表为OnhashCode相同对象不同 3. JDK1.8基于数组和链表和红黑树 源码解读 1.默认参数 /*** The default initial capacity - MUST be a power of two.*///hashmap默认初始大小16,用位移做运算static final int DEFAULT_INITIAL_CAPACITY 1 4; // aka 16/*** The maximum capacity, used if a higher value is implicitly specified* by either of the constructors with arguments.* MUST be a power of two 130.*///2^30次方static final int MAXIMUM_CAPACITY 1 30;/*** The load factor used when none specified in constructor.*///加载因子static final float DEFAULT_LOAD_FACTOR 0.75f;/*** The bin count threshold for using a tree rather than list for a* bin. Bins are converted to trees when adding an element to a* bin with at least this many nodes. The value must be greater* than 2 and should be at least 8 to mesh with assumptions in* tree removal about conversion back to plain bins upon* shrinkage.*///链表大于8转换成红黑树static final int TREEIFY_THRESHOLD 8;/*** The bin count threshold for untreeifying a (split) bin during a* resize operation. Should be less than TREEIFY_THRESHOLD, and at* most 6 to mesh with shrinkage detection under removal.*///链表小于6转换成红黑树static final int UNTREEIFY_THRESHOLD 6;/*** The smallest table capacity for which bins may be treeified.* (Otherwise the table is resized if too many nodes in a bin.)* Should be at least 4 * TREEIFY_THRESHOLD to avoid conflicts* between resizing and treeification thresholds.*///数组长度大于64且链表长度大于8才会转换成红黑树static final int MIN_TREEIFY_CAPACITY 64;2.内部静态类 static class NodeK,V implements Map.EntryK,V {final int hash;final K key;V value;NodeK,V next;Node(int hash, K key, V value, NodeK,V next) {this.hash hash;this.key key;this.value value;this.next next;}public final K getKey() { return key; }public final V getValue() { return value; }public final String toString() { return key value; }public final int hashCode() {return Objects.hashCode(key) ^ Objects.hashCode(value);}public final V setValue(V newValue) {V oldValue value;value newValue;return oldValue;}public final boolean equals(Object o) {if (o this)return true;if (o instanceof Map.Entry) {Map.Entry?,? e (Map.Entry?,?)o;if (Objects.equals(key, e.getKey()) Objects.equals(value, e.getValue()))return true;}return false;}}3.字段 /* ---------------- Fields -------------- *//*** The table, initialized on first use, and resized as* necessary. When allocated, length is always a power of two.* (We also tolerate length zero in some operations to allow* bootstrapping mechanics that are currently not needed.)*///数组 transient不能序列化transient NodeK,V[] table;/*** Holds cached entrySet(). Note that AbstractMap fields are used* for keySet() and values().*/transient SetMap.EntryK,V entrySet;/*** The number of key-value mappings contained in this map.*/transient int size;/*** The number of times this HashMap has been structurally modified* Structural modifications are those that change the number of mappings in* the HashMap or otherwise modify its internal structure (e.g.,* rehash). This field is used to make iterators on Collection-views of* the HashMap fail-fast. (See ConcurrentModificationException).*/// fail-fast机制transient int modCount;/*** The next size value at which to resize (capacity * load factor).** serial*/// (The javadoc description is true upon serialization.// Additionally, if the table array has not been allocated, this// field holds the initial array capacity, or zero signifying// DEFAULT_INITIAL_CAPACITY.)int threshold;/*** The load factor for the hash table.** serial*///加载因子如果不填写则采用默认加载因子final float loadFactor;4.forEach修改报错 public final void forEach(Consumer? super K action) {NodeK,V[] tab;if (action null)throw new NullPointerException();if (size 0 (tab table) ! null) {int mc modCount;for (int i 0; i tab.length; i) {for (NodeK,V e tab[i]; e ! null; e e.next)action.accept(e.key);}if (modCount ! mc)throw new ConcurrentModificationException();}}}5.put方法 public V put(K key, V value) {return putVal(hash(key), key, value, false, true);}static final int hash(Object key) {int h;// null值放在0号位置科学方法计算hash值减少冲突return (key null) ? 0 : (h key.hashCode()) ^ (h 16);}/*** Implements Map.put and related methods** param hash hash for key* param key the key* param value the value to put* param onlyIfAbsent if true, dont change existing value 当不存在时则不更改现有值* param evict if false, the table is in creation mode. 如果为false则退出表处于创建模式* return previous value, or null if none*/final V putVal(int hash, K key, V value, boolean onlyIfAbsent,boolean evict) {//数组链表 n 数组长度 i链表存放位置NodeK,V[] tab; NodeK,V p; int n, i;//数组不存在或者数组长度为0则调用扩容方法懒加载if ((tab table) null || (n tab.length) 0)n (tab resize()).length;//hash不冲突数组直接存放链表if ((p tab[i (n - 1) hash]) null)tab[i] newNode(hash, key, value, null);else {NodeK,V e; K k;//如果hash值相等且key值相等将p赋值给e等待做更新操作if (p.hash hash ((k p.key) key || (key ! null key.equals(k))))e p;//是红黑树else if (p instanceof TreeNode)e ((TreeNodeK,V)p).putTreeVal(this, tab, hash, key, value);else {for (int binCount 0; ; binCount) {// 遍历链表到末尾添加元素if ((e p.next) null) {p.next newNode(hash, key, value, null);//帮链表转换成红黑树数组容量64 链表长度大于8if (binCount TREEIFY_THRESHOLD - 1) // -1 for 1sttreeifyBin(tab, hash);break;}if (e.hash hash ((k e.key) key || (key ! null key.equals(k))))break;p e;}}// 存在则直接修改if (e ! null) { // existing mapping for keyV oldValue e.value;if (!onlyIfAbsent || oldValue null)e.value value;afterNodeAccess(e);return oldValue;}}modCount;if (size threshold)resize();afterNodeInsertion(evict);return null;}扩容方法 /*** Initializes or doubles table size. If null, allocates in* accord with initial capacity target held in field threshold.* Otherwise, because we are using power-of-two expansion, the* elements from each bin must either stay at same index, or move* with a power of two offset in the new table.** return the table*/final NodeK,V[] resize() {//旧的数组NodeK,V[] oldTab table;//获取旧数组的长度int oldCap (oldTab null) ? 0 : oldTab.length;// 旧数组的扩容阈值int oldThr threshold;// 新的数组长度和新的扩容阈值int newCap, newThr 0;// 如果旧的数组存在if (oldCap 0) {if (oldCap MAXIMUM_CAPACITY) {threshold Integer.MAX_VALUE;return oldTab;}else if ((newCap oldCap 1) MAXIMUM_CAPACITY oldCap DEFAULT_INITIAL_CAPACITY)newThr oldThr 1; // double threshold 两倍扩容}// 旧数组存在将旧的数组的长度赋值给新数组else if (oldThr 0) // initial capacity was placed in thresholdnewCap oldThr;//使用默认值进行初始化else { // zero initial threshold signifies using defaults//初始化数组容量数组默认初始大小16newCap DEFAULT_INITIAL_CAPACITY;// 初始化数组扩容阈值链表加载因子*数组默认初始大小12newThr (int)(DEFAULT_LOAD_FACTOR * DEFAULT_INITIAL_CAPACITY);}//如果新数组扩容阈值为0if (newThr 0) {//ft 新的数组长度*加载因子即计算阈值float ft (float)newCap * loadFactor; newThr (newCap MAXIMUM_CAPACITY ft (float)MAXIMUM_CAPACITY ?(int)ft : Integer.MAX_VALUE);}//将新数组扩容阈值赋值到全局变量threshold newThr;SuppressWarnings({rawtypes,unchecked})NodeK,V[] newTab (NodeK,V[])new Node[newCap];table newTab;if (oldTab ! null) {for (int j 0; j oldCap; j) {NodeK,V e;if ((e oldTab[j]) ! null) {oldTab[j] null;if (e.next null)newTab[e.hash (newCap - 1)] e;else if (e instanceof TreeNode)((TreeNodeK,V)e).split(this, newTab, j, oldCap);else { // preserve order//扩容是2的次方分高位低位进行移动操作插入新数组尾插法NodeK,V loHead null, loTail null;NodeK,V hiHead null, hiTail null;NodeK,V next;do {next e.next;if ((e.hash oldCap) 0) {if (loTail null)loHead e;elseloTail.next e;loTail e;}else {if (hiTail null)hiHead e;elsehiTail.next e;hiTail e;}} while ((e next) ! null);if (loTail ! null) {loTail.next null;newTab[j] loHead;}if (hiTail ! null) {hiTail.next null;newTab[j oldCap] hiHead;}}}}}return newTab;}
http://www.hkea.cn/news/14272038/

相关文章:

  • 网站建设基本步骤包括哪些flash怎么做网页
  • 建网页和网站的区别wordpress 快速安装
  • 谁做的四虎网站是多少钱邢台集团网站建设费用
  • 合肥做装修哪个网站好天津市津南区教育网站建设招标
  • 品牌网站建设收费情况深圳深圳建设网站
  • 派点网站建设重庆电力建设公司网站
  • 山西住房建设部网站西部数码网站管理助手 301
  • 做公司网站哪家 上海wordpress修改社交
  • 网站建设开票开什么内容小程序怎么开通
  • 怎么自己开发网站企业如何申请网站
  • 网站功能模块是什么百度广告管家
  • 购物网站建设与开发wordpress做淘宝的交流插件
  • 温州好的网站推广郑州出租车网
  • 做it的网站有哪些wordpress优酷插件
  • 忻州网站建设公司建设银行网站上预览电子回单
  • 建设应用型网站的意义网络维护电话
  • 做区位图的网站大理公司网站建设
  • 做网店好还是网站网站优化建设山东
  • 美团网站除佣金表格怎么做网上电影网站怎么做的
  • 钟楼区建设局网站做本地婚恋网站
  • 常州网站建设价格网站优化实习报告
  • 京东网站建设的要求电子商务营销写作实务
  • 百度快照和做网站有关系吗东莞推广系统
  • 郑州做旅游网站的公司有免费的wordpress
  • 宣城地宝网站开发安庆信德建设咨询有限公司网站
  • 白杨seo课程苏州网站排名优化
  • 手表网站排名前十织梦网站安装视频
  • 简单网站的制作开发app应用公司排名
  • 如何把网站做的和别人一样wordpress 占比
  • 江苏五星建设集团有限公司网站企业外部网站建设