Java core technology interview succinctly -09 Hashtable, HashMap, TreeMap What is the difference?

1. Briefly about red-black tree

Since TreeMap is implemented by the red-black tree, so let's talk about what is a red-black tree.
First, the red-black tree is a self-balancing binary search tree , is a highly efficient binary search tree, which has a good efficiency, it can be completed in time to find O (logN), increasing the delete operation.
Learned a binary search tree students know, ordinary binary search tree in extreme cases, retreat into the list, then check the efficiency of additions and deletions will be relatively low.
To avoid this, there have been some self-balancing search trees, such as AVL, red-black trees. These self-balancing search trees by defining the nature of the left and right subtrees of any node level difference controlled within a predetermined range, in order to reach an equilibrium state.
Red-black tree properties defined below, is achieved by self-balancing :

	节点是红色或黑色。
	根是黑色。
	所有叶子都是黑色(叶子是NIL节点)。
	每个红色节点必须有两个黑色的子节点。(从每个叶子到根的所有路径上不能有两个连续的红色节点。)
	从任一节点到其每个叶子的所有简单路径都包含相同数目的黑色节点(简称黑高)。

Here Insert Picture Description

2.Map system

Or put this figure:
Here Insert Picture Description
First, we need to be clear HashMap, HashSet, TreeMap, TreeSet differences and relations:
Note: Map is to provide a key to the relationship between the value of the mapping, we can be understood as is used to store key-value pairs.
The Set is used to store objects. We can be understood as used to store keys. Of course, every Set on the ground floor will be support for maintaining a Map.

3.Hashtable, HashMap, TreeMap What is the difference?

Hashtable hash table is a Java class library provides an early implementation itself is synchronous , and does not support null keys and values , due to the performance overhead due to synchronization, it has rarely been recommended.
HashMap is more widely hash table implementations, the behavior largely consistent with HashTable, the main difference is that HashMap is not synchronized , support null keys and values and so on . Typically, the HashMap be put or get operation, constant-time performance can be achieved, so it is preferred to access most of the scene using a key, for example, implement a user ID storage structure and the user information corresponding to the running time.
TreeMap it is based on a red-black tree to provide sequential access of the Map, and HashMap different, it get, put, remove such operations are O (log (n)) time complexity, the specific order may be specified by Comparator determined, or be determined according to the natural order of the keys.

Published 68 original articles · won praise 3 · Views 2163

Guess you like

Origin blog.csdn.net/weixin_41724265/article/details/104369437