Problems encountered in Java development

Notes on some problems encountered in daily work:
1. The hash method of Map in JDK1.7 and 1.8 has changed, resulting in that the HashSet is read in order.
2. The usage of return: one is to return the value of the specified type of the method (this value is always determined), and the other is to end the execution of the method (just a return statement).
3. Iterator is still used at the bottom of for each.
4. The Hash table uses a mapping function f : key —> address to map the keyword to the storage location of the record in the table, so that when you want to find the record, you can directly calculate the record in the table according to the keyword and the mapping relationship. The storage location in the table, usually, this mapping relationship is called the Hash function, and the storage location calculated by the Hash function and the keyword (note that the storage location here is only the storage location in the table, not the actual physical address ) is called the Hash address.
5. Map <int, int >will report an error, the declaration of generic type must be a class, int is a basic data type rather than a class, here should be declared with the encapsulation class Integer of int, it should be Map <Integer, Integer >.
6. The default capacity of HashMap is 16, and the default load factor is 0.75 (3 /4 ), which can be modified in the construction method.

    static final int DEFAULT_INITIAL_CAPACITY = 16;    
    static final float DEFAULT_LOAD_FACTOR = 0.75f;

7. The difference between HashMap and TreeMap

HashMap: Stores key/value in an array, not thread-safe, allows null as key and value, key cannot be repeated, value can be repeated, does not guarantee that the element iteration order is in the order of insertion, and the hash value of the key is calculated first. The hashcode value is then calculated. Each time the capacity is expanded, the hash value of the key will be recalculated, which will consume resources. It is required that the key must rewrite the equals and hashcode methods. The
default initial capacity is 16, the load factor is 0.75, and the expansion is the old capacity times 2. Find elements fast, if the key is the same, compare the value, if the value is different, store the value according to the linked list structure, that is, there are multiple values ​​behind a key;
TreeMap: The implementation of NavigableMap based on red-black binary tree, thread is not safe, null is not allowed , the key cannot be repeated, and the value is allowed to be repeated. The elements stored in the TreeMap should implement the Comparable interface or the Comparator interface, and the elements will be iterated in the sorted order. The two compared keys must not throw classCastException. It is mainly used to automatically sort elements when storing elements, and output them in sorted order when iteratively output.
8. Deleting a non-existing Key in a Map will not report an error

        Map map = new Hashtable();
        map.remove("1");

9. HashMap is unsafe under multi-threading because of resize:

Exception in thread "新线程2" java.util.ConcurrentModificationException
    at java.util.HashMap$HashIterator.nextEntry(HashMap.java:922)
    at java.util.HashMap$EntryIterator.next(HashMap.java:962)
    at java.util.HashMap$EntryIterator.next(HashMap.java:960)
    at java.util.AbstractMap.toString(AbstractMap.java:518)
    at java.lang.String.valueOf(String.java:2849)
    at java.lang.StringBuilder.append(StringBuilder.java:128)
    at com.wxx.runnable.MyRunnable2.run(MyRunnable2.java:21)
    at java.lang.Thread.run(Thread.java:745)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325463502&siteId=291194637