Collection of common interview questions

What are the common collections?

A: The Mapinterface and the Collectioninterface is the parent interface of all collections framework:

  • CollectionSubinterface interface comprises: Listan interface and Setan interface;
    • ListInterface implementation class ArrayListare: LinkedList, , Stackand Vectorthe like;
    • SetInterface implementation class HashSetare: TreeSet, , LinkedHashSetand the like;
  • MapInterface implementation class HashMapare: TreeMap, Hashtable, , ConcurrentHashMapand Propertiesthe like;

HashMapAnd Hashtablethe difference?

  1. HashMapFailing to consider synchronization is thread-unsafe; Hashtableusing synchronizedkeywords is thread-safe;

  2. HashMapAllow K/Vare null; the latter K/Vare not allowed to null;

  3. HashMapInherit from AbstractMapclass; and Hashtableinherit from Dictionaryclass;

HashMapWhy not directly use the hashCode()processed hash value as tablethe 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 HashMapcapacity 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: HashMapI 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:

  1. First, it is more efficient than taking the rest operation;
  2. The second reason is that h&(length-1)it is only equivalent to when the length of the array is a power of two h%length;
  3. 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 HashMapquery efficiency.

If lengtha power of two is length - 1converted to binary it must be 11111……in the form, wherein hthe binary operation efficiency is very fast, and the space is not wasted; if lengthpower is not 2, for example lengthis 15, length - 114, corresponding to the binary system 1110, that hthe operation, the last one are 0, and 0001, 0011, 0101, 1001, 1011, 0111, 1101this 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 Hashconflict. Two times is enough. The purpose of the operation.

HashMapHow is it different in JDK 1.7and 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 HashMapmiddle Stringand Integersuch packaging suitable as K?

Answer: The characteristics of packaging String, Integersuch as, can guarantee Hashthe unchangeable value and calculation accuracy of the value, and can effectively reduce Hashthe probability of collision.

  • They are all finaltypes, that is, immutability and guaranteed immutability, keyand there will be no hashcases where the obtained values ​​are different;
  • The internal methods have been rewritten equals(), hashCode()etc. , and the HashMapinternal specifications have been followed (not clear putValueabout the process you can go to see above ), and it is not prone to Hashvalue calculation errors;

Interviewer: What if I want to make my own Objectas Khow 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 more Hashcollisions;

  • The rewriting equals()method needs to comply with the reflexivity, symmetry, transitivity, consistency, and any non- nullreference value x, these x.equals(null)must be returned false, the purpose is to ensure keythe 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-fastmechanism.

For example: suppose that there are two threads (thread 1, thread 2), thread 1 Iteratortraverses 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 ConcurrentModificationExceptionan exception, producing fail-fastmechanisms.

The reason: iterator access the contents of the collection directly when traversing and traversing using a process modCountvariable. If the content occurs during the collection is traversed change, it will change modCountthe value. Whenever the iterator using hashNext()/next()the next before a traverse element will detect modCountwhether a variable is expectedmodCountvalue, which is then returned to traverse; otherwise throws an exception, terminate traversal.

Solution:

  1. In the traversal process, all related to the change modCountworthwhile place all together synchronized.

  2. Use CopyOnWriteArrayListto replace ArrayList;

ArrayListAnd Vectorthe difference?

A: The two classes implement Listinterfaces ( Listinterfaces inherited Collectioninterfaces), 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 HashSetthe maximum differences and the like set, HashSetcollection or the like can not be identified by index number to retrieve the elements therein, is not allowed There are repeating elements.

ArrayListAnd Vectorthe difference between the two main aspects:

  1. Synchronization : Vectoris thread-safe, that is between its thread synchronization method (plus synchronizedkey), and ArrayListis thread safe, among which the method is thread sync. If only one thread will access the collection, it is best to use ArrayList, 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 use Vector, because we do not need us Think about and write thread-safe code yourself.

  2. Data growth: ArrayListand Vectorhas an initial capacity size, when the storage into the inside thereof the number of elements exceeds the capacity need increases ArrayListand Vectorstorage 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. VectorThe data is full (load factor 1) growth doubled (incremental expansion: 2 times the original volume), and ArrayListwhen the data amount reaches the half of the capacity (load factor 0.5) growth of the original volume (0.5 times + 1) spaces.

ArrayListAnd LinkedListthe difference?

A: LinkedListimplements Listand Dequeinterfaces, commonly referred to as a doubly linked list; ArrayListimplements Listan interface, dynamic arrays;

  1. LinkedListHigher efficiency when inserting and deleting data ArrayListin the search for a indexhigher efficiency of data;
  2. LinkedListRatio ArrayListrequires more memory;

ArrayAnd ArrayListwhat is the difference? When to be Arrayand not ArrayListdo?

Answer: The difference is:

  1. ArrayCan contain basic types and object types, ArrayListonly object types.
  2. ArrayThe size is fixed and ArrayListthe size changes dynamically.
  3. ArrayListOffers more methods and properties, such addAll()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.

HashSetHow to ensure that the data is not repeatable?

A: HashSetThe bottom layer is actually HashMap, it's just that we HashSetimplemented the Setinterface and used the data as the Kvalue, and the Vvalue 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 HashMapthe Kvalue itself is not allowed to be repeated, and HashMapif the value K/Vis the same in the middle , the Vold one will be overwritten with the new one V, and then the old one will be returned V. Then HashSetexecuting this sentence in the will always return one false, causing the insertion to fail, which guarantees The non-repeatability of data.

Published 94 original articles · liked 0 · visits 722

Guess you like

Origin blog.csdn.net/qq_46578181/article/details/105411856