Summary of collection framework knowledge points + Dachang interview questions

Collection: the most basic interface of collection List, Set, Queue

Map: the basic interface of the mapping table

Iterator: Iterator, which can traverse the data in the collection

(Note: There is an error in the figure below. When the ArrayList is expanded, the capacity is expanded by 1.5 times)

 

List

List is an ordered Collection. There are three implementation classes in Java List: ArrayList, Vector and LinkedList. 

ArrayList is the most commonly used implementation class of List. It is implemented internally through an array. It is suitable for random search and traversal, but not suitable for insertion and deletion (more expensive)

Implementation principle: array, when the capacity is not enough, create a new capacity array, use arraycopy to copy the original array

Vector

Like ArrayList, it is implemented through an array. The difference is that it supports thread synchronization. Only one thread can write Vector at a time, avoiding inconsistencies caused by simultaneous writing by multiple threads, but synchronization requires high costs. So access is slower.

LinkedList

Using a linked list to store data is suitable for dynamic insertion and deletion, and the speed of random access and traversal is slow. In addition, it also provides methods that are not defined in the List interface, which are specially used to operate the header and tail elements, and can be used as stacks and queues. and bidirectional queues are used.

 

Set

Set is stored in disorder and cannot be repeated. The essence of object equality is the hashCode value (the serial number calculated by java based on the memory address)

HashSet (hash table)

The storage elements in the HashSet are not stored in the order, but are obtained according to the hash value, and the hash value of the element is obtained through the hashcod method. HashSet first judges the hash values ​​of two elements. If the hash values ​​are the same, then compares the equals method. If the result of equls is true, HashSet is regarded as the same element. Not the same element if equals is false. 
If the hash value is the same, it will be extended under the same hash value, as shown in the figure:

HashSet determines the location of elements in memory by hashCode value. Multiple elements can be stored in one hashCode position

 

TreeSet (binary tree)

TreeSet() is to use the principle of binary tree to sort the objects of the new add() in the specified order (ascending order, descending order). Every time an object is added, it will be sorted, and the object will be inserted into the specified position of the binary tree. (Arranged out of order, not repeatable)

LinkHashSet(HashSet+LinkedHashMap)

It inherits from HashSet and is implemented based on LinkedHashMap. The bottom layer of LinkedHashSet uses LinkedHashMap to save all elements. It inherits from HashSet, and all its methods operate the same as HashSet. Therefore, the implementation of LinkedHashSet is very simple. It only provides four construction methods, and by passing an identification parameter, call the parent The constructor of the class is implemented by constructing a LinkedHashMap at the bottom layer. The related operations are the same as those of the parent class HashSet, just call the method of the parent class HashSet directly.

 

Map

HashMap (array + linked list + red-black tree)

HashMap stores data according to the hashCode value of the key. In most cases, its value can be directly located, so it has a fast access speed, but the traversal order is uncertain. HashMap only allows the key of one record to be null, and the value of multiple records is allowed to be null. HashMap is not thread-safe, that is, multiple threads can write to HashMap at the same time at any time, which may lead to data inconsistency. If you need to meet thread safety, you can use the synchronizedMap method of Collections to make HashMap thread-safe, or use ConcurrentHashMap. We use the following picture to introduce the structure of HashMap:

Inside the HashMap is an array, and each element in the array is a one-way linked list. In the figure above, each green entity is an instance of the nested class Entry, and Entry contains four attributes: key, value, hash value and next for a one-way linked list.

1. Capacity: The current array capacity, which is always 2^n, can be expanded. After expansion, the array size is twice the current size.

2. loadFactor: load factor, the default is 0.75. 

3. threshold: the threshold of expansion, equal to capacity * loadFactor

Implementation of JAVA8

Java8 has made some modifications to HashMap. The big difference is the use of red-black tree, so it is composed of array + linked list + red-black tree. 
According to the introduction of Java7 HashMap, we know that when searching, we can quickly locate the specific subscript of the array according to the hash value, but after that, we need to compare one by one along the linked list to find what we need. The time complexity depends on The length of the linked list is O(n). In order to reduce this part of the overhead, in Java8, when the number of elements in the linked list exceeds 8, the linked list will be converted into a red-black tree, and the time complexity can be reduced to O(logN) when searching in these positions. 
 
 

HashTable (thread safe)

Hashtable is a legacy class. Many common functions of mapping are similar to HashMap. The difference is that it inherits from the Dictionary class and is thread-safe. Only one thread can write to Hashtable at any one time. The concurrency is not as good as ConcurrentHashMap, because ConcurrentHashMap introduces segmentation Lock. Hashtable is not recommended to be used in new code. It can be replaced by HashMap when thread safety is not required, and ConcurrentHashMap can be replaced by thread safety when thread safety is required. 

TreeMap (sortable)

TreeMap implements the SortedMap interface, which can sort the records it saves according to the key. The default is the ascending order of the key value. You can also specify the sorting comparator. When using Iterator to traverse the TreeMap, the records obtained are sorted. 
If using sorted maps, TreeMap is recommended. 
When using TreeMap, the key must implement the Comparable interface or pass in a custom Comparator when constructing the TreeMap, otherwise an exception of type java.lang.ClassCastException will be thrown at runtime

LinkHashMap (record insertion order)

LinkedHashMap is a subclass of HashMap, which saves the insertion order of records. When using Iterator to traverse LinkedHashMap, the records obtained first must be inserted first, and can also be constructed with parameters, sorted according to the access order. 

 

interview questions

1. The difference between ArrayList and Vector

Both classes implement the List interface, and they are ordered collections that can be repeated.

The difference between the two is mainly in two parts:

Synchronization: Vector is thread-safe, indicating that its threads are synchronized, while ArrayList is thread-unsafe and unsynchronized. When only one thread accesses, use ArrayList without considering thread safety, which is more efficient; multiple threads access collections, use Vecctor without consideration, and write thread-safe code

Data growth: Both have an initial capacity. When the capacity is insufficient, Vector will double its original size each time, and ArrayList will grow to 1.5 times its original size by default. Both ArrayList and Vector can set the initial space size, and Vector can also set the growth
space size, while ArrayList does not provide a method to set the growth space

2. Talk about the storage performance and characteristics of ArrayList, Vector, LinkedList

Both ArrayList and Vector use arrays to store data. The number of elements in this array is larger than the actual stored data
for adding and inserting elements. They both allow indexing elements directly by serial number, but inserting elements involves
memory operations such as moving array elements, so indexing Data is fast but inserting data is slow, because Vector uses 
the synchronized method (thread safety). 
Usually the performance is worse than ArrayList, while LinkedList uses a doubly linked list for storage. Indexing data by serial number
requires forward or backward traversal, but when inserting data, you only need to record the front and rear items of this item, so
the insertion speed is faster
ArrayList is fast when searching, and LinkedList has more advantages when inserting and deleting. 

3. What is the difference between fail-fast and fail-safe?

Iterator's failsafe is based on making a copy of the underlying collection, so it is not affected by modifications on the source collection.
All collection classes under package java.util are fail-fast, while
all classes under package java.util.concurrent are fail-safe. Fail - fast iterators throw 
ConcurrentModificationException, while fail-safe iterators never throw

4. The data structure of hashmap

(The data structures in the java programming language are only arrays and linked lists) HashMap uses a combination of arrays and linked lists, which is called linked list hashing (hashing)

5. What is the working principle of HashMap?

HashMap stores elements in the form of key-value pairs. HashMap requires a hash function, which uses the hashCode() and equals() methods to add and retrieve elements to the collection. Call the put method, hashMap will calculate the hash value of the key, and then store the key-value pair in the appropriate index. If the key is not there, the value will be updated to a new value. Some important features of HashMap are its capacity, load factor and threshold
resizing.

6. When will Hashmap be expanded?

When the number of hashmap elements exceeds the loadFactor of the array size, the array is expanded. The default loadFactor is 0.75, and the default array size is 16. When the number exceeds 12, it will be doubled, and then recalculate each element in the array. position in .

7. What are the characteristics of the three interfaces of List, Map, and Set when accessing elements?

First of all, both List and Set are collections of single-column elements, and their parent interfaces are Collection. Set does not allow repeated elements, that is, there cannot be two equal elements (equals), so the set collection method has a boolean return value to judge whether it is equal. The Set collection is unordered and cannot be taken out arbitrarily. Iterator can be used Iterate through the elements. List represents an ordered collection. According to the order added by add, the specified index points to the entire object, and an object can be arbitrarily taken out.

Secondly, Map is different from List and Set. It is a double-column collection. The put method is used to store key/value, and the same key value cannot be stored. The repeated rules are also judged by equals. At the same time, all key values ​​​​or values ​​​​can be taken out separately. , can also get the composite value (Map.Entry).

List holds elements in a specific order and may have duplicate elements. Set cannot have duplicate elements, internal ordering.
Map saves key-value values, and value can have multiple values

(HashSet is stored according to a certain calculation method of the hashcode value, rather than directly according to 
the size of the hashCode value.)

8. The elements in the Set cannot be repeated, so what method is used to distinguish whether it is repeated? Is it == or 
equals()? What is the difference between them?

To determine whether an element is repeated, use equals to determine.

  1.  == is to judge whether two variables or instances point to the same memory space, and equals is to judge whether the values ​​of the memory spaces pointed to by two variables or instances are the same 
  2. == refers to comparing memory addresses, and equals() compares the contents of strings
  3. == refers to whether the usage is the same, equals() refers to whether the values ​​are the same

9. Two objects have the same value (x.equals(y) == true), but they can have different hash codes. Is this sentence correct? 

right. If objects are to be stored in HashSet or HashMap, their equals are equal, then their
hashcode values ​​must be equal. 
If it is not to be saved in HashSet or HashMap, it has nothing to do with hashcode. At this time,
it is possible to have different hashcodes. For example, the objects stored in arrayList do not need to implement hashcode.
Of course , we have no reason not to implement it, and we usually implement it. of

10. What is the difference between heap and stack

Java memory is divided into stack memory and heap memory. Stack memory means that after the program enters a method, a separate storage space is opened for this method to store local variables in the method. After the method ends, the memory is released. Heap memory is used to store data that is not placed on the method stack, such as new objects. Local variables are placed on the heap after being modified with final.

11. What are the basic interfaces of the Java collection class framework?

The most basic interfaces in the java collection class are:

Collection: Represents a set of objects, each of which is its child element. 
Set: A Collection that does not contain duplicate elements. 
List: A sequential collection that can contain repeated elements. 
Map: An object that can map a key (key) to a value (value), and the key cannot be repeated

12. What is the difference between HashSet and TreeSet?

HashSet is implemented using a hash table. The elements are out of order, and the content placed cannot be repeated. Only null can be placed once.

TreeSet is implemented by a binary tree structure, the elements in it are ordered, and null cannot be placed

13. What is the underlying implementation of HashSet? 

The implementation of HashSet depends on HashMap, and the constructor of hashSet will initialize a hashMap object. HashSet does not allow duplicate values, therefore, the value of HashSet is stored in HashMap as the key of HashMap, and returns false when the stored value already exists

14. What is the implementation principle of LinkedHashMap?

LinkedHashMap is also implemented based on HashMap. The difference is that it defines an Entry header. This header is not placed in the table, but is additionally independent. LinkedHashMap inherits the Entry in hashMap and adds two attributes Entry before, after, and header Combined to form a doubly linked list to achieve sorting by insertion order or access order.

LinkedHashMap defines the sorting mode accessOrder, which is a boolean variable, for the access order, it is true; for the insertion order, it is false. In general, there is no need to specify a sorting mode, and the iteration order is the insertion order by default. 

15. Why does the collection class not implement the Cloneable and Serializable interfaces

The semantics and meaning of cloning or serialization are implementation-specific.
Therefore, it should be determined by the specific implementation of the collection class how to be cloned or serialized

16. What is an Iterator

The Iterator interface provides many methods for iterating over collection elements. Every collection class contains methods that return iterator instances. The iterator can delete the elements of the underlying collection during the iteration process, but it cannot directly call the remove() method of the collection, and can be deleted through the remove() method of the iterator.

17. What is the difference between Iterator and ListIterator

Iterator can be used to traverse Set and List collections, but ListIterator can only be used to traverse List. 
Iterator can only traverse the collection forward, and ListIterator can both forward and backward. 
ListIterator implements the Iterator interface and contains other functions, such as: adding elements, replacing elements
, getting the index of the previous and next element, etc.

18. What is the difference between an array (Array) and a list (ArrayList)? When should you use Array instead of ArrayList?

Array can contain basic types and object types, ArrayList can only contain object types, Array size is fixed, ArrayList is dynamic,

ArrayList is slower when dealing with fixed-size primitive data types.

19. What are the best practices of Java collection class framework?

 If the size of the elements is fixed and known in advance, we should use Array instead of ArrayList. 
 Some collection classes allow specifying an initial capacity. Therefore, if we can estimate the number of stored elements, we can set the initial capacity to avoid recalculating the hash value or expanding the capacity. 
 Always use generics for type safety, readability and robustness reasons. At the same time, using generics can also avoid ClassCastException at runtime. 
 Using the immutable class provided by JDK as the key of Map can avoid implementing hashCode() and equals() methods for our own class. 
 When programming, interface is preferred over implementation. 
 When the underlying collection is actually empty, return a collection or array with length 0, do not return null

20. What are Comparable and Comparator interfaces for? list their differences

The Comparable interface only provides one method compareTo(), which can sort two objects. Specifically, she returns negative numbers, 0, and positive numbers to indicate that the input object is less than, equal to, or greater than the existing object. Comparator provides compare() and equals() methods, the former is used to sort two input parameters, return negative numbers, 0, and positive numbers represent less than, equal to, greater than. The equals() method requires an object as a parameter, which is used to determine whether the input parameter is equal to the comparator. This method returns true only if the input parameter is also a comparator and the sorting result of the input parameter and the current comparator is the same. 

21. The difference between Collection and Collections

collection is the upper-level interface of the collection class, and the interfaces inherited from it are mainly set and list. The collections class is a helper class for the collection class. It provides a series of static methods for searching, sorting, thread safety and other operations
on various collections

 

 

 

Guess you like

Origin blog.csdn.net/qq_40513633/article/details/107258744