Article Directory
- Double row Collections Framework: Map
- 1. Common implementation class structure
- 2. [problem]
- 3. The storage structure is understood:
- 4. Common methods
- 5. Memory Structure:
- 5.1 HashMap implementation principles in jdk7:
- 5.2 HashMap in jdk8 and jdk7 achieved in terms of the underlying differences:
- 5.3 HashMap properties typical attributes of the underlying Description:
- The principle underlying the 5.4 LinkedHashMap (understand)
- 6. TreeMap use
- Read the configuration file 7. Properties
Double row Collections Framework: Map
1. Common implementation class structure
|----Map:双列数据,存储 key-value 对的数据 ---类似于高中的函数:y = f(x)
|----HashMap:作为Map的主要实现类;线程不安全的,效率高;存储null的key和value
|----LinkedHashMap:保证在遍历map元素时,可以照添加的顺序实现遍历。
原因:在原的HashMap底层结构基础上,添加了一对指针,指向前一个和后一个元素。
对于频繁的遍历操作,此类执行效率高于HashMap。
|----TreeMap:保证照添加的key-value对进行排序,实现排序遍历。此时考虑key的自然排序或定制排序
底层使用红黑树
|----Hashtable:作为古老的实现类;线程安全的,效率低;不能存储null的key和value
|----Properties:常用来处理配置文件。key和value都是String类型
HashMap的底层:数组+链表 (jdk7及之前)
数组+链表+红黑树 (jdk 8)
2. [problem]
- The realization of the principle underlying HashMap?
- HashMap and Hashtable similarities and differences?
- CurrentHashMap and Hashtable similarities and differences? (Temporarily not speak)
3. The storage structure is understood:
Map the key: disorderly, unrepeatable, use the Set stores all of the key -> class key where you want to override the equals () and hashCode () (to HashMap for example)
value in the Map: disorderly, reproducible, using the Collection store all value -> where the class value to rewrite equals ()
A key-value pair: key-value constitute an Entry object.
Map of entry: disorderly, unrepeatable, use the Set stores all entry
Icon:
4. Common methods
Add: put (Object key, Object value )
Delete: remove (Object key)
Modify: put (Object key, Object value )
query: get (Object key)
Length: size ()
traversal: keySet () / values () / entrySet ()
Clear Map:
map.clear (); // not null = Map
System.out.println (map.size ()); // 0
System.out.println (Map);} // {
Contains key: containsKey (Object key)
contains the value: containsValue (Object value)
5. Memory Structure:
5.1 HashMap implementation principles in jdk7:
HashMap map = new HashMap():
在实例化以后,底层创建了长度是16的一维数组Entry[] table。
...可能已经执行过多次put...
map.put(key1,value1):
- 首先,调用 key1 所在类的 hashCode() 计算key1 哈希值,此哈希值经过某种算法计算以后,得到在Entry数组中的存放位置。
+ 如果此位置上的数据为空,此时的 key1-value1 添加成功。 ----情况1
+ 如果此位置上的数据不为空,(意味着此位置上存在一个或多个数据(以链表形式存在)),比较key1和已经存在的一个或多个数据的哈希值:
= 如果key1的哈希值与已经存在的数据的哈希值都不相同,此时key1-value1添加成功。----情况2
= 如果key1的哈希值和已经存在的某一个数据(key2-value2)的哈希值相同,继续比较:调用key1所在类的equals(key2)方法,比较:
> 如果equals()返回false:此时key1-value1添加成功。----情况3
> 如果equals()返回true:使用value1替换value2。
补充:关于情况2和情况3:此时key1-value1和原来的数据以链表的方式存储。
在不断的添加过程中,会涉及到扩容问题,当超出临界值(且要存放的位置非空)时,扩容。默认的扩容方式:扩容为原来容量的2倍,并将原的数据复制过来。
5.2 HashMap in jdk8 and jdk7 achieved in terms of the underlying differences:
- new HashMap (): the bottom layer is not create an array of length 16
- Is the underlying array jdk 8: Node [], rather than the Entry []
- When the first call to put () method to create the underlying array of length 16
- jdk7 substructure only: + array list. jdk8 the underlying structure: Array + + red-black tree list.
- When the formation of the list, at sixes and sevens (jdk7: new elements point to the old elements .jdk8: old elements point to the new element)
- When the number of data elements on a certain position in the array index is present as a list> and the current length of the array 8> 64, then the data on the position of the index used to store red-black tree.
5.3 HashMap properties typical attributes of the underlying Description:
- DEFAULT_INITIAL_CAPACITY: default capacity of HashMap, 16
- DEFAULT_LOAD_FACTOR: HashMap default load factor: 0.75
- threshold: expansion threshold, capacity = fill factor: 16 0.75 => 12
- TREEIFY_THRESHOLD: Bucket in the chain length is greater than the default value, is converted to red-black tree: 8
- MIN_TREEIFY_CAPACITY: the minimum hash table capacity of the tub is Node tree: 64
The principle underlying the 5.4 LinkedHashMap (understand)
LinkedHashMap same underlying HashMap configuration used, since LinkedHashMap inherited HashMap.
Difference is: inside LinkedHashMap provides Entry, replacing the HashMap Node.
6. TreeMap use
TreeMap added to the key-value, the key requirement by the object must be created in the same class
as sort key according to: natural order, custom ordering
Read the configuration file 7. Properties
//Properties:常用来处理配置文件。key和value都是String类型
public static void main(String[] args) {
FileInputStream fis = null;
try {
Properties pros = new Properties();
fis = new FileInputStream("jdbc.properties");
pros.load(fis);//加载流对应的文件
String name = pros.getProperty("name");
String password = pros.getProperty("password");
System.out.println("name = " + name + ", password = " + password);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}