Moment For Technology

Java Knowledge points

Posted on Aug. 8, 2022, 6:33 p.m. by Kiara Manda
Category: The code of life Tag: java

1.1 hashMap underlying source code, data structure

The underlying structure of hashMap is implemented in JDk1.7 by array + list, and in JDK1.8 by array + list + red-black tree. Take array + list as an example.

JDK1.8Before the PutMethods:

JDK1.8After the PutMethods:

1.2 What thread pools do Java come with?


Create a cacheable thread pool. If the length of the thread pool exceeds the processing requirement, you can recycle idle threads flexibly, or create a new thread if none is available. The characteristics of this type of thread pool are:

There is almost no limit to the number of worker threads that can be created (although there is a limit to the number of interger.max_value), giving you the flexibility to add threads to the thread pool.

If a task has not been submitted to the thread pool for a long time, that is, if a worker thread is idle for a specified period of time (default: 1 minute), the worker thread is automatically terminated. After termination, if you submit a new task, the thread pool creates a new worker thread.

When using CachedThreadPool, it is important to control the number of tasks, otherwise the system will crash due to a large number of threads running at the same time.


Creates a thread pool that specifies the number of worker threads. Each time a task is submitted, a worker thread is created, and if the number of worker threads reaches the initial maximum of the thread pool, the submitted task is deposited to the pool queue. A FixedThreadPool is a typical and excellent thread pool that has the advantages of increasing program efficiency and saving overhead when creating threads. However, when the thread pool is idle, that is, when there are no runnable tasks in the thread pool, it does not release worker threads and consumes some system resources.


Create a single-threaded Executor, that is, create a single worker thread to execute a task. It will use a single worker thread to execute the task, ensuring that all tasks are executed in the specified order (FIFO, LIFO, priority). If one thread terminates abnormally, another takes its place, ensuring sequential execution. The most important feature of a single-worker thread is that it is guaranteed that each task is executed sequentially and that no more than one thread is active at any given time.


Create a thread pool of fixed length, and support scheduled and periodic task execution. The execution is delayed by 3 seconds.

1.3. Differences between HashMap and HashTable

  • Different thread safety

HashMap is thread-unsafe, whereas HashTable is thread-safe, with its method Synchronize. In the case of multi-threaded concurrency, you can use HashTabl directly, but you have to add your own synchronization to use HashMap.

  • Whether contains method is provided

HashMap has only containsValue and containsKey methods; Contains contains, containsKey, and containsValue. The contains and containsValue methods are identical.

  • Whether to allow null values for key and value

A Hashtable does not allow null values for keys and values. In a HashMap, null can be used as a key, and there is only one such key; There can be one or more keys with a value of NULL.

  • Array initialization and expansion mechanism

The default capacity of a HashTable without specifying a capacity is 11, while a HashMap is 16. A HashTable does not require the capacity of the underlying array to be an integer power of 2, whereas a HashMap requires that the capacity be an integer power of 2.

When Hashtable is expanded, the capacity is doubled, while when HashMap is expanded, the capacity is doubled.

1.4 Differences between TreeSet and HashSet

A HashSet is implemented using a hash table. The elements are out of order, and the methods add(), remove(), and contains() are all O(1) methods.

TreeSet is implemented using a tree structure (red-black tree algorithm). Elements are sorted in order, but methods like add(), remove(), and contains() are all O(log (n)). It also provides ways to handle sorted sets, such as first(), last(), headSet(), tailSet(), and so on.

1.5 Difference between String Buffer and String Build

1. StringBuffer is completely equivalent to the methods and functionality found in StringBuilder.

2. StringBuffer methods are thread-safe because they are often modified with the synchronized keyword, whereas StringBuilder methods are thread-safe because they are not.

3. In a single-threaded program, StringBuilder is faster because it doesn't require locking and has no multithreaded safety, whereas StringBuffer requires locking every time and is less efficient

1.6 Final, Finally, Finalize

Final: Modifiers (keywords) can be used in three ways: to modify classes, variables, and methods. When modifying a class, it means that it cannot be derived from new subclasses, that is, cannot be inherited. Therefore, it is the opposite of abstract. When modifying a variable, the variable is not changed in use and must be given an initial value at declaration time. It can only be read in reference and cannot be modified, that is, it is constant. When you modify a method, you can only use it and cannot override it in a subclass.

Finally: Usually placed in try... The catch construction is followed by the final execution code block, which means that the program will execute whether it executes normally or if an exception occurs, as long as the JVM is not shut down. Code that releases external resources can be written ina finally block.

Finalize: A method defined in the Object class that allows Java to use the Finalize () method to do the necessary cleanup before the garbage collector clears objects from memory. This method is called by the garbage collector when objects are destroyed, and can be used to clean up system resources or do other cleanup work by overriding finalize().

1.7 == and Equals

== : If you are comparing primitive data types, then you are comparing values of variables

If you are comparing reference data types, then you are comparing address values (whether two objects point to the same block of memory)

Equals: If not overridden, equals compares the addresses of two objects.

If we override equals we tend to compare the contents of the properties in the object

The equals method is inherited from the Object class and is implemented using == by default

Pay attention to my public number [Bao Ge big data], more dry goods waiting for you to come...

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.