编程英语文章解读(3)

Anatomy(剖析) of the Java Collections Framework

1、Talk about the difference between List, Set and Map?

stores存储
duplicate重复
maintains维护

2、What is the difference between Arraylist and LinkedList?

guarantee保证 线程都不安全
underlying底层的
strage存储
performing 执行(注意区别性能performance)
as before JDK1.6 circular linked list(jdk1.6以前为循环链表), JDK1.7 canceled(1.取消了循环) and the circular doubly linked list bidirectional attention(注意) distinction (区别)circular linked list, the below described. 注意区别双向链表和双向循环链表,以下有讲。
ArrayList:
It ArrayListuses array storage, so the time complexity(复杂度) of inserting and deleting elements is affected by the position of the element. 是以数组存储的,因此增加删除元素子影响当前元素本身。
添加元素到指定位置时,the (ni) element in the set and the (ni) element after the i element must perform the backward / forward operation.集合中的ni元素和ni后面的元素将进行后移操作。
LinkList:
默认位置插入元素时间复杂度还是1.
If you are iinserting and deleting elements at a specified position (add(int index, E element), the time complexity is approximately(近似) o(n))because Move to the specified position and insert.插入指定位置为o(n)

LinkedList do not support efficient random element access, and ArrayListsupport. LinkedList不支持快速随机访问,ArrayList支持。
Memory space occupation内存空间分配

3、ArrayList和Vector

在不需要保证线程安全时用ArrayList最好,多线程需要线程安全时用Vector

4、Talk about the expansion mechanism of ArrayList 。(ArrayList的扩展机制)

这里没什么翻译的内容,我的另一篇博客有详讲:

5、Difference between HashMap and Hashtable

directly:立刻,马上
That is to say:也就是说
hash conflicts:Hash冲突

1、HashMap的K/V可以为空,一个键还可以对应多个空值,HashTable不可以。
2、HashMap线程不安全,HashTable线程安全(如果你想线程安全,使用:ConcurrentHashMap)
3、效率上:HashMap>HashTable,因为线程安全问题,加之(in addition)HashTable基本上已经被淘汰了(eliminated)
4、The size of the initial capacity is different from the capacity of each expansion。初始容量的大小与每次扩展容量不同。
A、如果你没有规定初始容量,hashtable的初始容量是11,每次扩展容量2n+1倍。而HashMap初始容量为16,扩展为原来的2倍。
B、你给了初始容量,HashTable会使用你给定的初始容量,而HashMap会扩展为原来的2的幂(tableSizeFor()方法保证),HashMap always uses the power of 2 as the size of the hash table. We will explain why it is a power of 2.HashMap总是用2的幂次大小的容量。
5、底层数据结构:HashMap在jdk1.8后解决哈希冲突上发生了很大变化,当链表长度超过阈值时,链表转换为红黑树(converted ),HashTable就没有。

6、Difference between HashMap and HashSet

HashSet是HashMap的底层实现,源码中除了clone(), writeObject(), readObject()等本身必须实现的方法外,其他的方法都是直接调用的HashMap。
比较如下:

HashMap HashSet
Implemented the Map interface Implementing the Set interface
Store key-value pairs Store objects only 只存储对象
Call to put()add elements to the map Call the add()method to add elements to the Set
HashMap uses keys to calculate Hashcode HashSet uses member objects to calculate(计算) hashcode values. The hashcode may be the same for two objects, so the equals () method is used to determine the equality of objects。 HashSet因为两个对象可能会产生相同hashcode,所以使用equals方法区分两个对象。

7、How HashSet checks for duplicates(重复)

在添加对象HashSet时,HashSet首先计算对象的hashcodevalue,以确定在何处添加对象,并将其与其他添加对象的hashcode值进行比较。如果没有匹配的hashcode,则HashSet假定对象不会重复出现。但是,如果找到具有相同hashcode值的对象,则调用equals()方法来检查具有相同hashcode的对象是否确实相同。如果二者相同,则HashSet将不会使连接操作成功。
equals()和hashCode()的规定:
1、两个对象相等,则其hashcode必相等。反之有相同hashcode则对象不一定相等。
2、重写equals必须重写hashCode。
3、The default behavior of hashCode () is to generate unique values ​​for objects on the heap.hashcode的默认行为是为堆上的对象生成唯一的值。如果不重写,则两个对象怎么也不会相同(即使是指向同一个数据)。

= =和equals():
= =比较的是内存地址,equals比较的是内容。

8、The underlying implementation of HashMap

jdk1.8之前:
JDK1.8 before HashMap the bottom is an array and linked lists used together is chain hash .之前HashMap底层是数组和链表组合使用,也就是链表散列。
HashMap uses the hashCode of the key to get the hash value after processing the perturbation function。HasMap使用key的hashCode经过扰动函数处理的值作为hash值。然后通过(n-1)&hash计算当前元素的存储位置(n为数组长度)。
如果当前位置存在元素的话,就判断该元素与要存入的元素的 hash 值以及 key 是否相同,如果相同的话,直接覆盖,不相同就通过拉链法(zip method)解决冲突。
perturbation function扰动函数:就是HashMap的hash方法。
拉链法:就是合并(combining )链表和数组,换句话说,就是合并链表数组,数组中的每一项都是一个链表, If a hash conflict is encountered, the conflicting value can be added to the linked list.如果hash冲突发生,冲突的值会被添加到链表中。

jdk1.8后:
当链表长度大于阈值后,链表将转成红黑树,减少搜索时间。

红黑树:JDK1.8之后的TreeMap、TreeSet和HashMap都使用了红色和黑色的树。红黑树是为了解决二叉搜索树的缺陷,因为二叉搜索树在某些情况下会退化成线性结构。

发布了44 篇原创文章 · 获赞 3 · 访问量 1364

猜你喜欢

转载自blog.csdn.net/weixin_43329639/article/details/103884903