HashMap_get 방법 해석 실현

1 get()
    /**
     * Returns the value to which the specified key is mapped,
     * or {@code null} if this map contains no mapping for the key.
     *
     * 

More formally, if this map contains a mapping from a key * {@code k} to a value {@code v} such that {@code (key==null ? k==null : * key.equals(k))}, then this method returns {@code v}; otherwise * it returns {@code null}. (There can be at most one such mapping.) * *

A return value of {@code null} does not necessarily * indicate that the map contains no mapping for the key; it's also * possible that the map explicitly maps the key to {@code null}. * The {@link #containsKey containsKey} operation may be used to * distinguish these two cases. * * @see #put(Object, Object) */

public V get(Object key) { Node<K,V> e; // hash(key) key hash // e = getNode(hash(key), key) hash key // return (e = getNode(hash(key), key)) == null ? null : e.value; } /** * Implements Map.get and related methods. * * @param hash hash for key * @param key the key * @return the node, or null if none */ final Node<K,V> getNode(int hash, Object key) { // tab // first ( ) // e // n // k Node<K,V>[] tab; Node<K,V> first, e; int n; K k; // tab = table // n = tab.length ( ) // (n - 1) & hash // first = tab[(n - 1) & hash] ( ) // if ((tab = table) != null && (n = tab.length) > 0 && (first = tab[(n - 1) & hash]) != null) { // k = first.key key // , , if (first.hash == hash && // always check first node ((k = first.key) == key || (key != null && key.equals(k)))) // return first; // e = first.next // if ((e = first.next) != null) { // if (first instanceof TreeNode) // (TreeNode)first // , , return ((TreeNode<K,V>)first).getTreeNode(hash, key); // ( ) do { // k = e.key key // , key if (e.hash == hash && ((k = e.key) == key || (key != null && key.equals(k)))) // return e; // e = e.next // , do } while ((e = e.next) != null); } } // return null; }

2 getTreeNode()
        /**
         * Calls find for root node.
         */
        final TreeNode<K,V> getTreeNode(int h, Object k) {

			// root()      
			// find()          hash key    
			//           ,      ,   
            return ((parent != null) ? root() : this).find(h, k, null);
        }
        
        /**
         * Returns root of tree containing this node.
         */
        final TreeNode<K,V> root() {

			//       
            for (TreeNode<K,V> r = this, p;;) {

				// p = r.parent           
				//         
                if ((p = r.parent) == null)
                	//       
                    return r;

				//        ,          
                r = p;
            }
        }
        
        /**
         * Finds the node starting at root p with the given hash and key.
         * The kc argument caches comparableClassFor(key) upon first use
         * comparing keys.
         */
        final TreeNode<K,V> find(int h, Object k, Class<?> kc) {

			//    (    )
            TreeNode<K,V> p = this;
            do {

				// ph      hash
				// dir       ?         ?
				// pk       
				// pl      (   )   
				// pr      (   )   
				// q     
                int ph, dir; K pk;
                TreeNode<K,V> pl = p.left, pr = p.right, q;
                
				// ph = p.hash        hash
				//        hash       hash 
				if ((ph = p.hash) > h)
					//            , ,      
                    p = pl;

				//        hash       hash 
                else if (ph < h)
                	//            , ,      
                    p = pr;

				// pk = p.key      key
				//      key  
                else if ((pk = p.key) == k || (k != null && k.equals(pk)))
                	//      
                    return p;
                
                //   hash  ,key   ,     
                else if (pl == null)
                	//      
                    p = pr;

				//   hash  ,key   ,     
                else if (pr == null)
                	//      
                    p = pl;

				// kc = comparableClassFor(k)       
				// dir = compareComparables(kc, k, pk)       ,      ,
				//         ,  ,      
                else if ((kc != null ||
                          (kc = comparableClassFor(k)) != null) &&
                         (dir = compareComparables(kc, k, pk)) != 0)
                    //       xxx,     ,     ;
                    p = (dir < 0) ? pl : pr;
                
                // q = pr.find(h, k, kc)    
                //        
                else if ((q = pr.find(h, k, kc)) != null)
                	//     
                    return q;

				//            
                else
                    p = pl;

			//          
            } while (p != null);

			//          null
            return null;
        }

좋은 웹페이지 즐겨찾기