Day01
Two-column collection:
In real life, some of the data are mapped relationships exist, that is, the existence of pairs, such as couples. A singleton collection cannot exhibit a mapping relationship, so it learns a double-column collection.
A double-column collection has no iterators.
1.Map
Two-column collection:
———————— | Map if it is the implementation of the Map class interface collection class, has the characteristics: the stored data are in the form of key-value pairs exist, the key is not repeatable, the value can be repeated.
———————————— | The Hashmap is also based on a hash table.
———————————— | Treemap
———————————— | Hashtable
1-1 HashMap
The implementation of a hash table-based map interface.
Storage principle of HashMap:
When adding elements to HashMap, the Hashcode method is first called to get the hash value of the element, and then the operation is calculated to calculate the location of the element in the hash table.
Case one: If no element is stored so far, the element can be added directly to the hash table.
Case two: If the calculated position currently has other elements, then the Equals method of the element is also called to compare with the element at this location, if equals returns false, then the element is allowed to be stored, if the Equals method returns True, Then the element is treated as a repeating element and is not allowed to be stored.
Iteration
Way One keyset
Way two: Values
Way three: EntrySet
Storage principle of HashMap
1-2 TreeMap
TreeMap is also based on the red-black tree (binary tree) data structure implementation, characteristics: The key of the element will be sorted storage.
TreeMap things to be aware of:
1. When adding elements to TreeMap, if the keys of the elements are in a natural order, they are sorted by the natural order characteristics of the keys.
2. When adding elements to TreeMap, if the key of the element does not have a natural order, then the class to which the key belongs must implement the comparable interface, and the key comparison rule is defined on the CompareTo method.
3. When adding elements to TreeMap, if the key itself does not have a natural order, and the class to which the key belongs does not implement the comparable interface, the comparer must be created when the TreeMap object is created.
Java Fourth Week study diary