JDK source code interpretation

1. StringBuffer and StringBuilder principle

  1. At the bottom is nothing more than the maintenance of a char array, append every time go out into the char array of characters which put it in the final sb.toString () when using a new String () method to char array contents inside are transformed into String, so that the entire process only produces a StringBuilder object to a String object, space-saving. StringBuilder loss in performance only in that char array need not enough time expansion, expansion of the array to copy, reducing the efficiency to some extent.

  2. StringBuffer and StringBuilder uses exactly the same, only difference is that StringBuffer is thread-safe, it made all the methods are synchronized, StringBuilder is a non-thread-safe, so the scene does not involve thread-safe, such as internal methods, try to use StringBuilder, avoid synchronous consumption brings.

  3. Further, there is an optimization of the StringBuffer and StringBuilder point above said, when there is loss of the expansion performance, if it can be estimated to be spliced ​​to a length of string, try using the constructor specifies their length.

2. A set of principles that can be iterated

  1. The compiler will automatically use the keyword for this transformation at compile time for the use of iterators goal, which is the foreach loop principle. Furthermore, we then draw two conclusions:

  2. ArrayList has been able to use the foreach loop through, because all of the sub-interface ArrayList Collection List are, and is sub-interface Collection Iterable, the parent ArrayList class AbstractList correctly implement the method iterator Iterable interface. Before I write ArrayList with a foreach loop directly reported null pointer exception because I wrote it myself ArrayList does not implement the Iterable interface

  3. Any collection, whether it is provided by the JDK or write your own, as long as you want to use foreach loop iterates, it is necessary to properly implement the Iterable interface.

3. Cache value principle

  1. valueOf Byte, Short, Integer, Long, Char these boxing class () method is based on 128-bit boundaries do cache, and if it is less than -128 128 will fetch buffer value inside the reference.

  2. Float and Double are not cached value, because float is not fixed.

4. A set of principles

1. ArrayList

  1. May be null, and stored in order to come up with a consistent order, duplicate data can be stored, thread-safe.
  2. Underlying data structure is an array, the default initial size of 10. However, if we do not pass a List size, the default does not allocate space. Only when using add, add elements, will open up space. Every time the lack of space, the expansion in capacity expansion of 1.5 times based on the original.
  3. ElementDate used for transient keyword modifications, such serialized ArrayList when he can not be serialized, the purpose may be because no data exists List node, the direct serialization, a waste of space, therefore, had a writeObject this method, only serialization List all nodes in the data.
  4. Traverse: Use for general circulation.

2. LinkedList

  1. May be null, and stored in order to come up with a consistent order, duplicate data can be stored, thread-safe.
  2. Underlying data structure is a doubly linked list.
  3. Find principle: When the index is less than half the size of the array when (size >> 1 represents the size / 2, using the code shift operations to enhance the operational efficiency), look backward; otherwise, look forward.
  4. Traverse: Use a foreach loop.

3. CopyOnWriteArrayList

  1. May be null, and stored in order to come up with a consistent order, duplicate data can be stored, thread-safe.
  2. A bottom for the Object [] array.
  3. Principle: that is, to add remove modify operations and so on, will copy data out, so that no error occurs, however, because all the data to replicate, resulting in excessive resource usage. Source code using volatile forced refresh data backups for each thread. To ensure the correctness of data operations.
  4. Idea: separate read and write, eventual consistency.
  5. Usage: CopyOnWriteArrayList applies to read operations far more complicated scenario modification operation.

4. HashMap

  1. key and value can be null, and stored in order to come up with the order may be inconsistent, duplicate data can be stored, thread-safe.
  2. The underlying hash table. Node is stored inside the singly linked list.
  3. Principle: The default initialization time, there are 16 spaces. For all it requires a lot of contrast and fast, then if all with equal () to do obviously too low efficiency, so the solution is, whenever you want contrast, first with hashCode () to compare, if hashCode () is not the same, then show that these are certainly not equal (that is, then do not have to equal () and then compared to), if hashCode () the same, this time to compare their equal (), if equal () are the same, then the two objects are really the same, and so can greatly improve the efficiency but also to ensure the absolute correctness of the contrast!
  4. Persistence: Using the transient modification of the data area. Because on different virtual machines, resulting hashCode is not the same, HashCode and the underlying implementation related to different virtual machines may have different HashCode algorithm . So, persistence hashMap useless, however, java use writeObject method, re-persistent data, to ensure that on different platforms, is still available.

5. LinkedHashMap

  1. key and value may be null, and stored in order to come up in the same order, data can be stored in duplicate, thread-safe.
  2. Underlying data structure is a doubly linked list.
  3. LRU: the new LinkedHashMap (): boolean parameter a can be passed, to false insertion order sort, and in accordance with the access method ordered true, the first access, then access is completed, into the final list.

6. TreeMap

  1. key is not null, the value may be null, according to the natural order of the key, or rewriting a comparator that can be stored in the duplicate data, thread-safe.
  2. Substructure is red-black tree.
    1. Red-black tree features:
      1. The root node and the leaf nodes are black, which is a leaf node node Null
      2. Two children each red node are black node, in other words, can not have two consecutive red nodes
      3. The number of black nodes from the root to leaf nodes are all on the same
    2. length:
      1. Black Tree path the shortest possible path is all black nodes
      2. Red-black tree in the longest possible path is the path of red and black

Guess you like

Origin blog.csdn.net/weixin_38608626/article/details/88592422