Analysis of common JAVA data structures and principles

Source: Internet
Author: User

Analysis of common JAVA data structures and principles

Not long ago, the interviewer asked me to explain how to understand the java data structure framework. I have also read some of the source code before. balabala has talked about a bunch of ideas and I will summarize them.

Three important interfaces and features of java. util package:List, Set (ensure that the elements in the Set are unique), and Map (maintain multiple key-value pairs to ensure that keys are unique ). The implementation of different sub-classes varies, such as synchronization (thread security) and order.
Common class inheritance tree:

The following uses the source code to explain the implementation principles of common classes and their differences.

Collection (interfaces of all Collection classes)
List and Set are inherited from the Collection interface. You can view the jdk api. Most of the common methods for operating the Set are defined in this interface.

Collections)
You have to mention the tool class for operations on the Collection class.CollectionsIt provides many convenient methods, such as finding the difference set, Union set, copy, and sort of the two sets.
Because most of the collection interface implementation classes are not synchronized, you can use the Collections. synchronized * method to create a synchronized collection class object.
For example, create a synchronous List:
List synList = Collections.synchronizedList(new ArrayList());
The implementation principle is to re-encapsulate the new object and use the keyword synchronized to synchronize the object. It is easy to understand the source code.
Collections source code:

// Collections. synchronizedList returns an instance of the static SynchronizedCollection class, and finally assigns the new ArrayList object to the Collection.
  
   
C. Static class SynchronizedCollection
   
    
Implements Collection
    
     
, Serializable {final Collection
     
      
C; // Backing Collection final Object mutex; // Object on which to synchronize SynchronizedCollection (Collection
      
        C) {if (c = null) throw new NullPointerException (); this. c = c; mutex = this ;}//... public boolean add (E e) {// The original ArrayList object is called when the set is operated, but synchronized (mutex) {return c. add (e );}}//...}
      
     
    
   
  

List)

ArrayList and Vector are linear tables that use Object arrays as containers to store data. Many methods are added to maintain this array so that its capacity can be dynamically increased, greatly improving development efficiency. The obvious difference between them is that ArrayList is non-synchronous and Vector is synchronous. ArrayList should be used to improve efficiency without considering multithreading.
Source code of ArrayList and Vector:

// ArrayList. addpublic boolean add (E e) {ensureCapacityInternal (size + 1); // Increments modCount !! // You can see that the added object is put into the elementData array. elementData [size ++] = e; return true;} // ArrayList. removepublic E remove (int index) {rangeCheck (index); modCount ++; E oldValue = elementData (index); int numMoved = size-index-1; if (numMoved> 0) // when the element is removed, the System generates the vacancy generated by the Group. the arraycopy method moves all the subsequent elements one by one. arraycopy calls the local method provided by the virtual machine to improve efficiency. arraycopy (elementData, index + 1, elementData, index, numMoved); elementData [-- size] = null; // Let gc do its work return oldValue ;} // The synchronized keyword public synchronized boolean add (E e) {modCount ++; ensureCapacityHelper (elementCount + 1); elementData [elementCount ++] = e; return true ;}

The shortlist is a linked list. You can understand the data structure and its implementation principle. Data inserted and deleted at random locations in a linked list is faster than that in a linear table and slower than in a linear table.
Schematic diagram of a two-way linked list:

Partial list source code:

// The Source Code clearly expresses the public class outline list of the schematic.
  
   
Extends AbstractSequentialList
   
    
Implements List
    
     
, Deque
     
      
, Cloneable, java. io. Serializable {// transient Node
      
        First; transient Node
       
         Last;} // Node class private static class Node
        
          {// E item; Node
         
           Next; Node
          
            Prev; Node (Node
           
             Prev, E element, Node
            
              Next) {this. item = element; this. next = next; this. prev = prev ;}}
            
           
          
         
        
       
      
     
    
   
  

You can choose to use ArrayList (for non-synchronous and non-frequent deletion) and Vector (for synchronization) based on the actual situation), sort list (frequently selected when inserting or deleting at any location ).

Map (store key-value pairs, unique key)

The implementation principle of the HashMap structure is to encapsulate the put key-value into an Entry object and store it in an Entry array. The position (array subscript) it is calculated based on the hash value of the key and the length of the array. If the current subscript of the array already has a value, the value of the current subscript of the array points to the newly added Entry object.
A little dizzy. See the figure below:

Look at the source code after reading the figure. It is very clear and does not need to be commented out.

Public class HashMap
  
   
Extends AbstractMap
   
    
Implements Map
    
     
, Cloneable, Serializable {transient Entry
     
      
[] Table; public V put (K key, V value) {if (key = null) return putForNullKey (value); int hash = hash (key ); int I = indexFor (hash, table. length); // traverses the current underlying Entry object chain. If the key already exists, replace
      
        E = table [I]; e! = Null; e = e. next) {Object k; if (e. hash = hash & (k = e. key) = key | key. equals (k) {V oldValue = e. value; e. value = value; e. recordAccess (this); return oldValue ;}} addEntry (hash, key, value, I); return null ;}} static class Entry
       
         Implements Map. Entry
        
          {Final K key; V value; Entry
         
           Next; int hash ;}
         
        
       
      
     
    
   
  

TreeMap is a node composed of Entry objects.Red/black treeBy default, data from put to TreeMap is sorted in the natural order of keys. Comparator is input for custom sorting when new TreeMap is used. I can't tell you a lot about the Internet.

Set (ensure the uniqueness of elements in the container)
This is becauseThe Set structure maintains a Map to store data. The key value uniqueness of the Map structure is used..
Some source code of HashSet:

Public class HashSet
  
   
Extends AbstractSet
   
    
Implements Set
    
     
, Cloneable, java. io. serializable {// meaningless Object as Map value private static final Object PRESENT = new Object (); public boolean add (E e) {return map. put (e, PRESENT) = null ;}}
    
   
  

HashSet and TreeSet maintain a HashMap and TreeMap by default.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.