73. Introduce the underlying data structure of HashMap

73. Introduce the underlying data structure of HashMap

We are all using JDK 1.8 now, and the bottom layer is composed of "array + linked list + red-black tree", as shown in the figure below, while before JDK 1.8 it was composed of "array + linked list".

1.Hash

Hash is called a "hash table", which is to convert an input of any length into a fixed-length output through a hash algorithm, and the output result is a hash value.
In fact, this conversion is a compressed mapping.
The space of the hash table is usually smaller than the space of the input . Different inputs may be hashed into the same output, so the input value cannot be uniquely determined from the hash table. This creates a Hash conflict .

74. Why should it be changed to "array + linked list + red-black tree"?

The main purpose is to improve the search performance when the hash conflict is serious ( the linked list is too long) . The search performance of using the linked list is O(n), while using the red-black tree is O(logn).

75. When do you use linked lists? When to use red-black tree?


For insertion , the default is to use linked list nodes. When the number of nodes at the same index position exceeds 8 after being added (threshold value 8): if the length of the array is greater than or equal to 64 at this time, it will trigger the linked list node to turn into a red-black tree node (treeifyBin); and if the length of the array is less than 64, then It will not trigger the conversion of the linked list to the red-black tree, but will expand the capacity, because the amount of data at this time is still relatively small.

For removal , when the number of nodes at the same index position reaches 6 after removal, and the node at this index position is a red-black tree node, it will trigger red-black tree node to link list node (untreeify).

 78. What is the resize process of HashMap?

 

77. What is the insertion process of HashMap?

79. In addition to HashMap, which Maps have been used, and how to choose when using them? 

 

 

90. Java memory structure (runtime data area)


Program Counter: Thread private. A small memory space that can be seen as a line number indicator of the bytecode executed by the current thread. If the thread is executing a Java method, this counter records the address of the virtual machine bytecode instruction being executed; if the thread is executing a Native method, the counter value is empty.

Java virtual machine stack: thread private. Its lifecycle is the same as a thread. The virtual machine stack describes the memory model of Java method execution: when each method is executed, a stack frame is created to store information such as local variable table, operand stack, dynamic link, and method exit. The process of each method from invocation to execution completion corresponds to the process of a stack frame being pushed into the virtual machine stack to popped out of the stack.

Native method stack: thread private. The functions played by the local method stack and the virtual machine stack are very similar. The difference between them is that the virtual machine stack serves the Java method (that is, bytecode) execution for the virtual machine, while the local method stack is used by the virtual machine. to the Native method service.

Java heap: shared by threads. For most applications, the Java heap is the largest piece of memory managed by the Java virtual machine. The Java heap is a memory area shared by all threads and created when the virtual machine starts. The sole purpose of this memory area is to store object instances, and almost all object instances allocate memory here.

Method area: Like the Java heap, it is a memory area shared by each thread. It is used to store class information (construction methods, interface definitions), constants, static variables, and code compiled by the instant compiler (words) that have been loaded by the virtual machine. section code) and other data. The method area is a concept defined in the JVM specification. Where it is placed, different implementations can be placed in different places.

Runtime constant pool: The runtime constant pool is part of the method area. In addition to the class version, fields, methods, interfaces and other description information in the Class file, there is also a constant pool, which is used to store various literals and symbol references generated during compilation. This part of the content will be displayed after the class is loaded. It is stored in the runtime constant pool in the method area.

String str = new String("hello");
In the above statement, the variable str is placed on the stack, the string object created with new is placed on the heap, and the literal "hello" is placed on the heap.

93. The process of class loading


The process of class loading includes: loading, verification, preparation, analysis, and initialization, where verification, preparation, and analysis are collectively referred to as connection.

Loading: Obtain the binary byte stream defining this class through the fully qualified name of a class, and generate a java.lang.Class object representing this class in memory.

Verification: Ensure that the information contained in the byte stream of the Class file meets the requirements of the current virtual machine and will not endanger the security of the virtual machine itself.

Preparation: Allocate memory for the static variable and set the initial value of the static variable. The initial value mentioned here is "usually" the zero value of the data type.

Resolution: Replace symbolic references in the constant pool with direct references.

Initialization: At the initialization stage, the actual execution of the Java initializer code defined in the class begins . Mainly static variable assignment actions and statements in static statement blocks (static{}) .

String, StringBuffer, StringBuilder difference

 

 

The problem is that
the source code of StringBuffer defines it as final type, why the value of StringBuffer can still be changed?

public final class StringBuffer
    extends AbstractStringBuilder
    implements java.io.Serializable, Comparable<StringBuffer>, CharSequence


problem solved
**

When the final modified member variable is a basic data type, it cannot be changed after assignment.
When final is modified as a reference variable, its pointed address cannot be changed after assignment, but the object content can be changed. **

 

 

When using the StringBuffer class, the StringBuffer object itself will be operated every time instead of generating a new object, so if you need to modify the string, it is recommended to use StringBuffer .

The StringBuilder class was proposed in Java 5. The biggest difference between it and StringBuffer is that the methods of StringBuilder are not thread-safe (cannot be accessed synchronously).

Since StringBuilder has a speed advantage over StringBuffer , it is recommended to use the StringBuilder class in most cases .

Guess you like

Origin blog.csdn.net/Hoshea_sun/article/details/129809510