What are the common collections?
A: The Map
interface and the Collection
interface is the parent interface of all collections framework:
Collection
Subinterface interface comprises:List
an interface andSet
an interface;List
Interface implementation classArrayList
are:LinkedList
, ,Stack
andVector
the like;Set
Interface implementation classHashSet
are:TreeSet
, ,LinkedHashSet
and the like;
Map
Interface implementation classHashMap
are:TreeMap
,Hashtable
, ,ConcurrentHashMap
andProperties
the like;
HashMap
And Hashtable
the difference?
-
HashMap
Failing to consider synchronization is thread-unsafe;Hashtable
usingsynchronized
keywords is thread-safe; -
HashMap
AllowK/V
arenull
; the latterK/V
are not allowed tonull
; -
HashMap
Inherit fromAbstractMap
class; andHashtable
inherit fromDictionary
class;
HashMap
Why not directly use the hashCode()
processed hash value as table
the subscript?
Answer: The hashCode()
method returns an int integer type, which has a range of -(2^31)~(2^31 - 1)
about 4 billion mapping spaces. The HashMap
capacity is in the range 16
(initialization default) ~2^30
, the HashMap is usually less than the maximum value taken, and the device is also difficult to provide so much storage space, by causing hashCode()
the calculated hash value of the array size range may not be Within, and thus cannot match the storage location.
Interviewer: How to solve it?
Answer: HashMap
I have implemented my own hash()
method. Through two disturbances, it makes its own hash value high or low to perform XOR operation by itself, reducing the probability of hash collision and making the data distribution more even.
When the length of the array is guaranteed to be a power of 2, use hash()
the value after the operation and operation ( &
) ( 数组长度 - 1
) to obtain the array subscript for storage:
- First, it is more efficient than taking the rest operation;
- The second reason is that
h&(length-1)
it is only equivalent to when the length of the array is a power of twoh%length
; - Three to solve the "hash value does not match the size of the array" problem.
Interviewer: Why is the length of the array guaranteed to be a power of 2?
Answer: Only when the length of the array is a power of 2, h&(length - 1)
it is equivalent to h%length
, that is key
, the positioning achieved . The power of 2 can also reduce the number of conflicts and improve HashMap
query efficiency.
If length
a power of two is length - 1
converted to binary it must be 11111……
in the form, wherein h
the binary operation efficiency is very fast, and the space is not wasted; if length
power is not 2, for example length
is 15, length - 1
14, corresponding to the binary system 1110
, that h
the operation, the last one are 0
, and 0001
, 0011
, 0101
, 1001
, 1011
, 0111
, 1101
this position will never be stored several elements, and considerable wasted space, or worse, this is the case, the position of the array can be used than arrays The length is much smaller, which means that the probability of collision is further increased and the efficiency of the query is slowed down! This will cause a waste of space.
Interviewer: Why are there two disturbances?
Answer: This is to increase the randomness of the low order of the hash value, so that the distribution is more uniform, thereby improving the randomness &
uniformity of the corresponding array storage index position , and ultimately reducing the Hash
conflict. Two times is enough. The purpose of the operation.
HashMap
How is it different in JDK 1.7
and JDK 1.8
?
different | JDK 1.7 | JDK 1.8 |
---|---|---|
Storage structure | Array + linked list | Array + linked list + red black tree |
Initialization method | Separate function:inflateTable() |
Integrated directly into the expansion function resize() in |
Hash value calculation method | Disturbance processing = 9 disturbances = 4 bit operations + 5 XOR operations | Disturbance processing = 2 disturbances = 1 bit operation + 1 XOR operation |
Rules for storing data | When there is no conflict, store the array; when conflict, store the linked list | When there is no conflict, store the array; conflict & linked list length <8: store single linked list; conflict & linked list length> 8: tree and store red-black tree |
Insert data | Head interpolation method (first talk about the original position of the data moved to the last one, and then insert the data to the position) | Tail insertion method (direct insertion into the tail of the linked list / red-black tree) |
Calculation method of storage location after capacity expansion | All calculations are performed according to the original method (ie hashCode ->> perturbation function->> (h&length-1) ) |
Calculate according to the law after capacity expansion (that is, the location after capacity expansion = original location or original location + old capacity) |
Why is the HashMap
middle String
and Integer
such packaging suitable as K
?
Answer: The characteristics of packaging String
, Integer
such as, can guarantee Hash
the unchangeable value and calculation accuracy of the value, and can effectively reduce Hash
the probability of collision.
- They are all
final
types, that is, immutability and guaranteed immutability,key
and there will be nohash
cases where the obtained values are different; - The internal methods have been rewritten
equals()
,hashCode()
etc. , and theHashMap
internal specifications have been followed (not clearputValue
about the process you can go to see above ), and it is not prone toHash
value calculation errors;
Interviewer: What if I want to make my own Object
as K
how should I do it?
Answer: Rewrite hashCode()
and equals()
method.
-
The rewriting
hashCode()
is because the storage location of the stored data needs to be calculated, and care should be taken not to try to exclude the key part of an object from the hash code calculation to improve performance, which can be faster but may cause moreHash
collisions; -
The rewriting
equals()
method needs to comply with the reflexivity, symmetry, transitivity, consistency, and any non-null
reference valuex
, thesex.equals(null)
must be returnedfalse
, the purpose is to ensurekey
the uniqueness in the hash table;
Java collection's fast failure mechanism " fail-fast
"?
A: The error detection mechanism is a set of Java when multiple threads on the set of structural changes during operation, is likely to cause fail-fast
mechanism.
For example: suppose that there are two threads (thread 1, thread 2), thread 1 Iterator
traverses the elements in collection A, at some point thread 2 modified the structure of collection A (it is a structural modification, not a simple modification a collection of content elements), so this time the program will throw ConcurrentModificationException
an exception, producing fail-fast
mechanisms.
The reason: iterator access the contents of the collection directly when traversing and traversing using a process modCount
variable. If the content occurs during the collection is traversed change, it will change modCount
the value. Whenever the iterator using hashNext()/next()
the next before a traverse element will detect modCount
whether a variable is expectedmodCount
value, which is then returned to traverse; otherwise throws an exception, terminate traversal.
Solution:
-
In the traversal process, all related to the change
modCount
worthwhile place all togethersynchronized
. -
Use
CopyOnWriteArrayList
to replaceArrayList
;
ArrayList
And Vector
the difference?
A: The two classes implement List
interfaces ( List
interfaces inherited Collection
interfaces), they are ordered set, that element position is stored in these two sets are sequential, the equivalent of a dynamic array, we after the index position can be removed by an element, and wherein the data is allowed to repeat, which is HashSet
the maximum differences and the like set, HashSet
collection or the like can not be identified by index number to retrieve the elements therein, is not allowed There are repeating elements.
ArrayList
And Vector
the difference between the two main aspects:
-
Synchronization :
Vector
is thread-safe, that is between its thread synchronization method (plussynchronized
key), andArrayList
is thread safe, among which the method is thread sync. If only one thread will access the collection, it is best to useArrayList
, because it does not consider the problem of thread safety, so the efficiency will be higher; if multiple threads will access the collection, it is best to useVector
, because we do not need us Think about and write thread-safe code yourself. -
Data growth:
ArrayList
andVector
has an initial capacity size, when the storage into the inside thereof the number of elements exceeds the capacity need increasesArrayList
andVector
storage space, each memory space to increase, not only one memory cell increases, Instead, multiple storage units are added, and the number of storage units added each time is a certain balance between memory space utilization and program efficiency.Vector
The data is full (load factor 1) growth doubled (incremental expansion: 2 times the original volume), andArrayList
when the data amount reaches the half of the capacity (load factor 0.5) growth of the original volume (0.5 times + 1) spaces.
ArrayList
And LinkedList
the difference?
A: LinkedList
implements List
and Deque
interfaces, commonly referred to as a doubly linked list; ArrayList
implements List
an interface, dynamic arrays;
LinkedList
Higher efficiency when inserting and deleting dataArrayList
in the search for aindex
higher efficiency of data;LinkedList
RatioArrayList
requires more memory;
Array
And ArrayList
what is the difference? When to be Array
and not ArrayList
do?
Answer: The difference is:
Array
Can contain basic types and object types,ArrayList
only object types.Array
The size is fixed andArrayList
the size changes dynamically.ArrayList
Offers more methods and properties, suchaddAll()
as:removeAll()
, ,iterator()
and so on.
For basic types of data, collections use automatic binning to reduce coding workload. However, this approach is relatively slow when dealing with basic data types of fixed size.
HashSet
How to ensure that the data is not repeatable?
A: HashSet
The bottom layer is actually HashMap
, it's just that we HashSet
implemented the Set
interface and used the data as the K
value, and the V
value has been saved with a same dummy value . We can see the source code:
public boolean add(E e) {
// 调用HashMap的put方法,PRESENT是一个至始至终都相同的虚值
return map.put(e, PRESENT)==null;
}
Because HashMap
the K
value itself is not allowed to be repeated, and HashMap
if the value K/V
is the same in the middle , the V
old one will be overwritten with the new one V
, and then the old one will be returned V
. Then HashSet
executing this sentence in the will always return one false
, causing the insertion to fail, which guarantees The non-repeatability of data.