[Interview questions-Java] Summary of 2022 interview questions dry goods (continuously updated)

foreword

This summary draws on the summaries of some bloggers. After reading through it, I removed some too simple interview questions, aiming to make a certain simplification and easy reading.

PS: Links to articles by these bloggers will be placed in the acknowledgment section at the end of this article.

1. Basics of Java

1.1 The role of Hashcode

There are two types of java collections, one is Listand the other is Set. The former is orderly and repeatable, while the latter is disorderly and non-repeatable. How to judge whether the element already exists when we insert it in the set, we can use the equals() method. But if there are too many elements, using this method will be slower.
So someone invented the hash algorithm to improve the efficiency of finding elements in the collection. This method divides the collection into several storage areas. Each object can calculate a hash code, and the hash codes can be grouped. Each group corresponds to a storage area. The object can be determined according to the hash code of an object. The area where it should be stored.
The hashCode method can be understood in this way: what it returns is a value converted according to the memory address of the object. In this way, when a new element is to be added to the collection, the hashCode method of this element is called first, and the physical location where it should be placed can be located at once.
If there is no element at this position, it can be directly stored at this position without any comparison; if there is already an element at this position, call its equals() method to compare with the new element, and if they are the same, then If it is not saved, other addresses will be hashed if they are not the same. In this way, the number of actual calls to the equals() method is greatly reduced, almost only one or two times.

1.2 What is the difference between String, String StringBuffer and StringBuilder?

String is a read-only string, it is not a basic data type, but an object. From the perspective of the underlying source code, it is a final character array. The referenced string cannot be changed. Once defined, it cannot be added, deleted or modified. Each operation on String will generate a new String object.

private final char value[];

Each + operation: Implicitly create a new StringBuilder object on the heap that is the same as the original string, and then call the append method to concatenate + the following characters.
Both StringBuffer and StringBuilder inherit the AbstractStringBuilder abstract class. We can see from the AbstractStringBuilder abstract class

/**
 * The value is used for character storage.
 */
char[] value;

Their underlying layers are variable character arrays, so when performing frequent string operations, it is recommended to use StringBuffer and StringBuilder for operations. In addition, StringBuffer adds a synchronization lock to the method or adds a synchronization lock to the called method, so it is thread-safe. StringBuilder does not add a synchronization lock to the method, so it is not thread-safe.

1.3 The difference between HashMap and HashTable

1. The difference between the two parent classes
HashMap is that they inherit from AbstractMapthe class, Hashtablebut inherit from Dictionarythe class. However, they all implement the three interfaces of map, Cloneable (reproducible), and Serializable (serializable) at the same time.
2. The interface provided to the outside world is different
Hashtable than HashMapmost, and provides two methods of elements() and contains(). The parent class that
the elements() method inherits from . The elements() method is used to return an enumeration of the values ​​in this . The contains() method determines whether the value passed in is included. It works the same as containsValue(). In fact, contansValue() just calls the contains() method. 3. The support for null is different : neither key nor value can be null. : The key can be null, but there can only be one such key, because the uniqueness of the key must be guaranteed; there can be multiple key values ​​whose corresponding value is null. 4. Different security p is not thread-safe. In a multi-threaded concurrent environment, problems such as deadlock may occur. Therefore, developers need to deal with multi-threaded security issues by themselves. It is thread-safe, and each of its methods has keywords, so it can be directly used in multithreading. Although thread-unsafe, its efficiency is much higher thanHashtableDictionnaryHashtable
Hashtable

Hashtable
HashMap

HashMa
Hashtablesynchronized
HashMapHashtable, this design is reasonable, because most usage scenarios are single-threaded. Thread-safety can be used when multi-threaded operations are required ConcurrentHashMap.
ConcurrentHashMapAlthough it is also thread-safe, its efficiency is Hashtablemany times higher than that of Because of ConcurrentHashMapthe use of segment locks, the entire data is not locked.
5. The initial capacity is different from the size of each expansion.
6. The method of calculating the hash value is different

Guess you like

Origin blog.csdn.net/lc1025082182/article/details/123029489