关于java:JDK成长记9集合篇的总结和练习

2次阅读

共计 9411 个字符,预计需要花费 24 分钟才能阅读完成。

这一节次要是抛出一些面试题让大家测验一下学习成绩,也会小结一下汇合篇的知识点。所以不会特地长。

练习 - 模仿面试

<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 公布!

正文完
 0