Hashmap, hashset, hashtable

xiaoxiao2021-03-06  21

Hashmap / havehtable / hashset

HashTable and HashMap: Surface: HashTable does not allow NULL values ​​(Key and Value can not be), HashMap allows NULL values ​​(Key and Value can be). HashTable method is synchronized, and HashMap does not synchronize, so you have to manually synchronize the difference in HashMap in multi-threaded occasion, just like vector and arraylist. Hashtable has a Contains (Object Value), features and containsValue (Object Value) features. Hashtable uses ENUMERATION, HashMap uses Iterator. Interior: Hashtable's Hash array default size is 11, the increased mode is OLD * 2 1. The default size of the HASH array in HashMap is 16, and must be 2 index. The hash value of the hash value, the HashTable uses the object's HashCode, the code is like this: int has = key.hashcode (); int index = (hash & 0x7fffffff)% Tab.Length; HashMap recalculate the HASH value, and use With instead of demodulation: int has = hash (k); int i = indexfor (hash, table.Length); static int hash (Object x) {int h = x.hashcode (); h = ~ (h << 9); h ^ = (H >>> 14); h = (h << 4); h ^ = (h >>> 10); Return H;} static int indexfor (int h, int layth) { Return H & (Length-1);} Hashset, Hashmap: Hashmap can be seen as three views: Key's set, Value's Collection, entry's set. Here is the HashSet is actually a view of Hashmap. Hashset is implemented in HashMap, and it is different from HashMap that it does not need to be KEY and VALUE.

Inserting an object to havehset is actually only internal to do

Public Boolean Add (Object O) {

Return map.put (o, present) == NULL;

Hashmap is a hash map, which is an implementation based on Hash Table, which can play elements in a constant time, or find a group of Key-Value Pair.

Hashset is a collection, it is important to look up time, all of which must have havehcode ()

转载请注明原文地址:https://www.9cbs.com/read-67909.html

New Post(0)