Ali "JAVA intern latest entry test questions -2019" in answer to explain (a serial)

And strive to clear and complete and accurate (gradually improve, continuously updated)

1, String class Why is final

Firstly String source:

public final class String
    implements java.io.Serializable, Comparable<String>, CharSequence {
    /** The value is used for character storage. */
    private final char value[];
  • Class is limited to the final keyword, that it can not be inherited, no subclasses. That holds a String object reference, it must be the String class, but will not be another class.
  • value [] are used to store values, final keyword is modified, this description can not be replaced by other array array - the array address is not changed, but the value of each element of the array can be changed

private qualifier guaranteed String string array outside the class can not be modified. Since no external exposure can modify the interface, so the value of String Once created, that can not be modified.

  • Thread Safety

Because strings are immutable (can only be read but not write), multiple threads can share the same string instance.

  • String constant pool can greatly improve the spatial efficiency

      String constant pool, see     https://segmentfault.com/a/1190000009888357

2, HashMap source of JDK8, implementation principle underlying structure

  HashMap Hash of conflict resolution, the latter alone will write a blog.

  ConcurrentHashMap the lock segments, manufacturers like to ask (recently Huawei telephone interview asked me), simply say, is hashMap data is an array with multiple locks to lock, a lock part of the data.

Unlike previous lock with a whole array, a plurality of threads can access the data segment, natural efficiency is high. Back alone to write a blog, systematically discussed the issue.

  • First look at the source Node
    static class Node<K,V> implements Map.Entry<K,V> {
        final int hash;
        final K key;
        V value;
        Node<K,V> next;

        Node(int hash, K key, V value, Node<K,V> next) {
            this.hash = hash; this.key = key; this.value = value; this.next = next; }

   HashMap with  transient Node <K, V> [ ] table  stored-value, is the list array (array + hash chain + red-black tree), is essentially Hash hash, i.e., not close-packed array, as described under FIG.

HashMap structure

                              Figure 01

Why is there a red-black tree to see put (new element) source code fragment as follows:

  else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);

TreeNode source segment defined as follows:

    static final class TreeNode<K,V> extends LinkedHashMap.Entry<K,V> {
        TreeNode<K,V> parent;  // red-black tree links
        TreeNode<K,V> left;
        TreeNode<K,V> right;
        TreeNode<K,V> prev;    // needed to unlink next upon deletion
        boolean red;
        TreeNode(int hash, K key, V val, Node<K,V> next) {
            super(hash, key, val, next); }

 

  • And dynamic capacity expansion
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // aka 16

The default capacity-16. When resize, newCap = OLDCAP <<. 1 (binary 2, the left one, i.e. 2 * old double capacity, the capacity may not be a power of 2)

  • New elements
    public V put(K key, V value) {
        return putVal(hash(key), key, value, false, true);
    }

    1) If before this key has a value, put the operation will replace the old value with the new value.

    2) how to solve the conflict Hash

 hash (hash), is the key and there is a storage location mapping relationship f, we call the hash function. hash conflicts, that is, different key, according to the storage location of the same hash function calculated, and the element to be added back to the original hashCode conflict, so the storage location to be recalculated according to certain rules. Normal HashMap (java HashMap structure 8 as "1", the red-black tree) structure as shown below:

java8 HashMap in order to improve search efficiency, the list when a conflict is too high, greater than the threshold, the list will be converted to black tree

  • Load factor
static final float DEFAULT_LOAD_FACTOR = 0.75f;

The default load factor 0.75, the probability and statistics related to, see  http://en.wikipedia.org/wiki/Poisson_distribution

In order to reduce conflicts, hashMap when the array length> threshold value  will trigger expansion, all the elements of the rehash (hashCode recalculated and stored) and then into the container after the expansion, because it involves the calculation, data search, memory copy, movement, etc. operation is very time-consuming.

The critical value = current capacity * current load factor. The default threshold  =  DEFAULT_INITIAL_CAPACITY DEFAULT_LOAD_FACTOR * X = 16 0.75 = 12 is triggered expansion operation.

 

*****************************************************************************************************

Energy is limited, too much desire, focus on doing one thing on the line

  • 5 years to write the code, technology blog scrutiny of every word, and insist on zero-copy original
  • Meaning a blog that exercise logical rational, systematic deepen the understanding of knowledge, writing exercise, and if just a little help to others, then it is a very happy thing

*****************************************************************************************************

Guess you like

Origin www.cnblogs.com/NaughtyCat/p/alibaba-java-interview-serial-1.html