这一节次要是抛出一些面试题让大家测验一下学习成绩,也会小结一下汇合篇的知识点。 所以不会特地长。
练习-模仿面试
<div class=”output_wrapper” id=”output_wrapper_id” style=”width:fit-content;font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><h3 id=”hdddd” style=”width:fit-content;line-height: inherit; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; margin-bottom: 2em; margin-right: 5px; padding: 8px 15px; letter-spacing: 2px; background-image: linear-gradient(to right bottom, rgb(43,48,70), rgb(43,48,70)); background-color: rgb(63, 81, 181); color: rgb(255, 255, 255); border-left: 10px solid rgb(255,204,0); border-radius: 5px; text-shadow: rgb(102, 102, 102) 1px 1px 1px; box-shadow: rgb(102, 102, 102) 1px 1px 2px;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>练习-模仿面试</span></h3></div>
先给大家讲一个简略的面试场景
快手Java面试一、二面:
(一面个别会问一些各种根底,比方汇合、并发、锁、JVM、MySql、Redis,IO模型,网络模型等基本原理和常识。二面也会掺杂一些根底,还会有些我的项目相干、框架的原理,中间件的原理,一些架构和思维,三面也是多方面的,然而根底会少,个别是一些底层原理,零碎设计或者架构等)
面试官:你好
候选人:你好
大家寒暄一下……
面试官:在你的简历下面看了你的履历……
之后你总结一面和二面,问了一些ArrayList如下问题:
\1) 新建一个ArrayList会分配内存吗?
\2) ArrayList和LinkedList的区别?
\3) ArrayList扩容机制?
\4) ArrayList的一些API,一一剖析他们的工夫复杂度?
\5) Hashmap底层数据结构,什么时候转化为红黑树,put操作的流程。
\6) Hashmap线程平安吗?线程平安的形式有哪些?
再给大家讲几个比拟猛的面试题
阿里面试 HashMap连环炮
\1) hash值计算的算法是什么?就是key.hashCode()吗?
\2) 默认状况下,put第一个元素时候容量大小是多少?扩容阈值又是多少?
\3) hash寻址如何进行的?
\4) hash值如果计算的雷同该怎么解决抵触?
\5) HashMap扩容后怎么进行rehash的?
\6) 指定大小的HashMap,扩容阈值算法是什么?
\7) Hashmap死循环问题解释一下?
美团面试题(深刻)
HashMap的JDK1.8和JDK1.7的实现不同之处?(大家能够看下JDK1.7的源码找下不同)
字节跳动面试题(果然有算法)
\1) 算法:翻转一下单向链表—>单向链表每k个元素翻转一次(升级版)
\2) 介绍HashMap ,与TreeMap区别?
\3) 用HashMap实现一个有过期性能的缓存,怎么实现?
https://blog.csdn.net/zhouyan…
ArrayList小结
<div class=”output_wrapper” id=”output_wrapper_id” style=”width:fit-content;font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><h3 id=”hdddd” style=”width:fit-content;line-height: inherit; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; margin-bottom: 2em; margin-right: 5px; padding: 8px 15px; letter-spacing: 2px; background-image: linear-gradient(to right bottom, rgb(43,48,70), rgb(43,48,70)); background-color: rgb(63, 81, 181); color: rgb(255, 255, 255); border-left: 10px solid rgb(255,204,0); border-radius: 5px; text-shadow: rgb(102, 102, 102) 1px 1px 1px; box-shadow: rgb(102, 102, 102) 1px 1px 2px;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>ArrayList小结</span></h3></div>
1、 ArrayList的基本原理底层是一个Object数组。长处次要是随机拜访快,毛病是扩容和插入和删除元素,会进行元素拷贝性能较差,不是线程平安的。
2、 创立ArrayList时,不指定大小, ArrayList第一次增加元素,会指定默认的容量大小为10。
3、 ArrayList的扩容机制次要两点,一个是计算扩容大小=原值+原值右移1(等价1.5倍),一个是底层通过System.arraycopy来拷贝元素和创立新数组,来最终实现空间的扩容。
4、 外围办法大多是通过System.arraycopy进行拷贝元素做到的,removeIf办法中
5、 遍历通过外部类Itr遍历时向后拜访,ListItr遍历时向前、向后均可拜访,因为不是线程平安,通过modcout的检测,实现了fail-fast机制。
6、 Vector应用了synchronized关键字的ArrayList,线程平安,扩容是原来的2倍。Stack继承了Vector,应用数组模拟了栈的操作而已。
LinkedList小结
<div class=”output_wrapper” id=”output_wrapper_id” style=”width:fit-content;font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><h3 id=”hdddd” style=”width:fit-content;line-height: inherit; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; margin-bottom: 2em; margin-right: 5px; padding: 8px 15px; letter-spacing: 2px; background-image: linear-gradient(to right bottom, rgb(43,48,70), rgb(43,48,70)); background-color: rgb(63, 81, 181); color: rgb(255, 255, 255); border-left: 10px solid rgb(255,204,0); border-radius: 5px; text-shadow: rgb(102, 102, 102) 1px 1px 1px; box-shadow: rgb(102, 102, 102) 1px 1px 2px;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>LinkedList小结</span></h3></div>
1、 LinkedList的基本原理,底层是一个双向链表。长处次要是插入和删除元素性能好,确定是随机拜访性能差,不是线程平安的。链表元素具备prev和next、item组成,外部存在first和last两个头尾指针。
2、 在LinkedList的尾、头增加或者删除元素,应用l辅助指针记录原last元素或first指针地位。
3、 定位元素,采纳二分法思维,依据size>>1进行了二分,通过for循环定位元素,工夫复杂度O(n)
4、 两头增加或者删除元素时候,先定位元素,之后应用两个前后辅助指针,进行元素的插入或者删除。
HashMap小结
<div class=”output_wrapper” id=”output_wrapper_id” style=”width:fit-content;font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><h3 id=”hdddd” style=”width:fit-content;line-height: inherit; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; margin-bottom: 2em; margin-right: 5px; padding: 8px 15px; letter-spacing: 2px; background-image: linear-gradient(to right bottom, rgb(43,48,70), rgb(43,48,70)); background-color: rgb(63, 81, 181); color: rgb(255, 255, 255); border-left: 10px solid rgb(255,204,0); border-radius: 5px; text-shadow: rgb(102, 102, 102) 1px 1px 1px; box-shadow: rgb(102, 102, 102) 1px 1px 2px;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>HashMap小结</span></h3></div>
1、 HashMap底层数据结构,JDK1.7数组+链表、JDK1.8数组+链表+红黑树。长处拜访工夫复杂度O(1),很快,毛病不是线程平安的。
2、 hash值计算的算法是什么?就是key.hashCode()吗?不是,基于hashCode进行计算的,高16位和低16位进行了异或操作。让高16位尽可能计算,缩小hash抵触的概率。具体:hash=hashCode^hashCode>>>16。个别称之为扰动解决。
3、 默认状况下,put第一个元素时候容量大小是多少?扩容阈值又是多少?
put第一个元素的时候默认容量时16,扩容阈值是0.75*16=12。
4、 hash寻址如何进行的?
index = (n-1) & hash等价于 index =hash % n。不过&与操作性能更高。
5、 hash值如果计算的雷同该怎么解决抵触?
3种状况,第一种hash值雷同,key值也雷同,进行笼罩操作。第二种hash值雷同,key值不同,链接为单向链表构造,第二种hash值雷同,key值不同,单向链表长度达到8,会将链表变为红黑树。
6、 HashMap扩容后怎么进行rehash的?
对应hash抵触产生的3中状况,扩容rehash也有3种状况。
状况1:如果数组地位只有一个值:应用新的容量进行rehash,即e.hash & (newCap – 1)
状况2:如果数组地位有链表,依据 e.hash & oldCap == 0进行判断,后果为0的应用原地位,否则应用index + oldCap地位,放入元素造成新链表,这里不会和状况1新的容量进行rehash与运算了,index + oldCap这样更省性能。
状况3:如果数组地位有红黑树,依据split办法,同样依据 e.hash & oldCap == 0进行树节点个数统计,如果个数小于6,将树的后果复原为一般Node,否则应用index + oldCap,调整红黑树地位,这里不会和新的容量进行rehash与运算了,index + oldCap这样更省性能。
7、 指定大小的HashMap,扩容阈值算法是什么?
因为指定容量大小时,会计算扩容阈值,会将传入的容量大小调整为最靠近2的次幂大小,设置为扩容阈值,构造函数时候,只是计算了扩容阈值,没有初始化数组大小。在增加元素时候数组大小等于了扩容阈值。
为什么这么做呢?一句话,为了进步hash寻址和扩容计算的的效率。因为无论扩容计算还是寻址计算,都是二进制的位运算,效率很快。另外之前你还记得取余(%)操作中如果除数是2的幂次方则等同于与其除数减一的与(&)操作。即 hash%size = hash & (size-1)。这个前提条件是除数是2的幂次方。
8、 HashMap死循环问题
在JDK1.8引入红黑树之前,JDK1.7因为只有单向链表解决hash抵触,除了遍历性能可能会慢,还有几率在多线程同时扩容,rehash的时候产生死循环问题,造成cpu100%。
造成死循环的外围脉络有如下几步:
1、首先原地位得有hash抵触,比方链表元素有4个。
2、之后须要有2个线程,同时增加元素,均满足扩容条件,进行扩容
3、一个线程刚刚进行了rehash操作,之后另一个线程开始rehash操作,会造成环向链表
4、get操作时候产生有限死循环,cpu可能达到100%
HashMap的兄弟姐妹们小结
<div class=”output_wrapper” id=”output_wrapper_id” style=”width:fit-content;font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><h3 id=”hdddd” style=”width:fit-content;line-height: inherit; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; margin-bottom: 2em; margin-right: 5px; padding: 8px 15px; letter-spacing: 2px; background-image: linear-gradient(to right bottom, rgb(43,48,70), rgb(43,48,70)); background-color: rgb(63, 81, 181); color: rgb(255, 255, 255); border-left: 10px solid rgb(255,204,0); border-radius: 5px; text-shadow: rgb(102, 102, 102) 1px 1px 1px; box-shadow: rgb(102, 102, 102) 1px 1px 2px;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>HashMap的兄弟姐妹们小结</span></h3></div>
1、 LinkedHashMap 继承与HashMap,底层构造基于HashMap,减少了2个before和after指针,造成双向链表,能够保护插入有序或者拜访有序,拜访有序可用来做LRUMap或者实用须要程序遍历的场景,然而它依然不是线程平安的。
2、 TreeMap 能够自定义Key值的排序规定,底层数据结构是红黑树。利用场景很灵便,能够用来申请参数排序或者最近一次心跳工夫、续约工夫排序,来实现心跳或续约过期机制等。
3、 HashTable和HashMap外围区别就是应用synchronized保障线程平安,这个和Vector+ArrayList很像
4、 HashSet应用了HashMap,只不过add办法时候的value都是new Object()而已,联合map的个性,同一个key值只能存在一个,map在put的时候,会hash寻址到数组的同一个地位去,而后笼罩原来的值,所以Set是去重的。默认是无序的。
5、 LinkedHashSet继承了HashSet,此时HashSet通过应用LinkedHashMap,是能够进行拜访有序的保障
6、 TreeSet也同理,默认是依据key值的compare办法来排序的,能够自定义Comparator,底层应用了TreeMap,add元素时,同样是空的Object,同样去重,然而TreeSet拜访是能够有序。
7、 LinkedHashSet/TreeSet/HashTable/HashSe的原理都极其简略,都是基于之前的HashMap、LinkedHashMap、TreeMap而已。
浏览源码的思维和办法总结
<div class=”output_wrapper” id=”output_wrapper_id” style=”width:fit-content;font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><h3 id=”hdddd” style=”width:fit-content;line-height: inherit; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; margin-bottom: 2em; margin-right: 5px; padding: 8px 15px; letter-spacing: 2px; background-image: linear-gradient(to right bottom, rgb(43,48,70), rgb(43,48,70)); background-color: rgb(63, 81, 181); color: rgb(255, 255, 255); border-left: 10px solid rgb(255,204,0); border-radius: 5px; text-shadow: rgb(102, 102, 102) 1px 1px 1px; box-shadow: rgb(102, 102, 102) 1px 1px 2px;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>浏览源码的思维和办法总结</span></h3></div>
1、 抓大放小的、对先脉络后细节、连蒙带猜的思维
2、 举例子、画图的办法
观点心态成长总结
<div class=”output_wrapper” id=”output_wrapper_id” style=”width:fit-content;font-size: 16px; color: rgb(62, 62, 62); line-height: 1.6; word-spacing: 0px; letter-spacing: 0px; font-family: ‘Helvetica Neue’, Helvetica, ‘Hiragino Sans GB’, ‘Microsoft YaHei’, Arial, sans-serif;”><h3 id=”hdddd” style=”width:fit-content;line-height: inherit; margin: 1.5em 0px; font-weight: bold; font-size: 1.3em; margin-bottom: 2em; margin-right: 5px; padding: 8px 15px; letter-spacing: 2px; background-image: linear-gradient(to right bottom, rgb(43,48,70), rgb(43,48,70)); background-color: rgb(63, 81, 181); color: rgb(255, 255, 255); border-left: 10px solid rgb(255,204,0); border-radius: 5px; text-shadow: rgb(102, 102, 102) 1px 1px 1px; box-shadow: rgb(102, 102, 102) 1px 1px 2px;”><span style=”font-size: inherit; color: inherit; line-height: inherit; margin: 0px; padding: 0px;”>观点心态成长总结</span></h3></div>
5个更重要
Ø 成长比胜利更重要
Ø 扭转本人比扭转他人更重要
Ø 楷模比说服力更重要
Ø 借力比致力更重要
Ø 置信比晓得更重要
3个保持秘诀
Ø 保持的三个秘诀之一视觉化
Ø 保持的三个秘诀之一指标化
Ø 保持的三个秘诀之一个性化
本文由博客一文多发平台 OpenWrite 公布!
发表回复