Chapter III hash code implementation

3.1 hashcode和equals

hashTable java code is (very important)

hashCode

Definitions: jdk calculated according to the address or number or a character string object type int value

Compare and equals

dfdsf

3.2 Links Law Code (a very important part hash)

Be compact design problem

HashMap underlying list is an array and implemented, which is above the fastener method. First, when inserted, will be then calculated based on the hash value of the key corresponding to the array index was calculated as the index = hashcode% table.length, (the subscript is the above-mentioned bucket), when the index has been above when present it will form a list of elements, the elements inserted into the trailing end, if no elements are present above the standard, then the element directly into this position.
When the query, the same will first calculate the corresponding index based on the hash key, and then to to find the corresponding position, if there are many elements of this index above, then will have to find until you find correspondence on this list Elements.

Open-addressable code for 3-3 (19:01)

3-4 HashTable source code analysis (18:06)

3-5 HashMap source code analysis (43:44)

3-6 HashSet source code analysis (08:26)

3-7 LinkedHashMap Profile

Baidu CTO Wang Haifeng: Baidu map has become AI era portray real-world critical infrastructure
bi-directional pointer

If linkedhashmap appears interview, topics such as: LRU leetcode 146 this topic often appears in the title of the design design

LinkedHashMap
1、继承hashMap hashmap+linkedlist
2、Entry before, After;
. 3, sequential / insertion has traversed in the output
LinkedHashMap achieve Hash table and linked list, a doubly linked list, and ensures that rely on iterative sequence is inserted sequence.

3-8 Leetcode combat exercises (to be done)

Very important to a question, the back should be back down
design problem of a subclass of categories
all related to O (1) basically HashMap

When writing put and get time, always be in the sky, began writing the put

put
there before
until no
full
not full
first
non-first

Design problems are generally difficult to medium kinds of questions, not difficult

public class LRUCache {
    class Node {
        int key;
        int value;
        Node next;
        Node pre;

        public Node(int key, int value) {
            this.key = key;
            this.value = value;
        }
    }

    private HashMap<Integer, Node> map;
    private int capacity;
    private Node head;
    private Node tail;

    public LRUCache(int capacity) {
        map = new HashMap<>();
        this.capacity = capacity;
        head = null;
        tail = null;
    }

    public int get(int key) {
        Node node = map.get(key);
        if (node == null) {
            return -1;
        }
        if (node != tail) {
            if (node == head) {
                head = head.next;
            } else {
                node.pre.next = node.next;
                node.next.pre = node.pre;
            }
            tail.next = node;
            node.pre = tail;
            node.next = null;
            tail = node;
        }
        return node.value;
    }
    code modify
    public void put(int key, int value) {
        Node node = map.get(key);
        if (node != null) {
            node.value = value;
            if (node != tail) {
                if (node == head) {
                    head = head.next;
                } else {
                    node.pre.next = node.next;
                    node.next.pre = node.pre;
                }
                tail.next = node;
                node.pre = tail;
                node.next = null;
                tail = node;
            }

        } else {
            Node newNode = new Node(key, value);
            if (capacity == 0) {
                Node temp = head;
                head = head.next;
                temp.next = null;
                map.remove(temp.key);
                capacity++;
            }
            if (head == null && tail == null) {
                head = newNode;
            } else {
                tail.next = newNode;
                newNode.pre = tail;
                newNode.next = null;
            }
            tail = newNode;
            map.put(key, newNode);
            capacity--;
        }
    }
}

3-9 Bloom Filter Bloom filter

Determine whether an element in the collection: HashSet
if particularly large amount of data?
A: bit computing method

New Algorithm - "Bloom filter (Bloom Filter)
so that each character through the k hash fucntion and corresponding positions is set to 1
determines whether each position 1, if 1, it is thought to exist in this collection, if there is not a 1, it is not certain that this collection
drawbacks: certain errors

3-10 Bloom Filter Bloom filter implemented Code

Does not support remove

Solution: counting sequencing using

3-11 do questions Notes

HashMap / HashSet :
get() & put)() 的时间复杂度:O(1)
containsKey(value)的时间复杂度:O(1)
containsValue is O(n)
because without the key it doesn't know where it is and the algorithm has to go over all the values stored in the map.

Guess you like

Origin www.cnblogs.com/andrewcao95/p/di-san-zhang-san-lie-biao-dai-ma-shi-xian.html