⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 linkedhashmap.java

📁 this gcc-g++-3.3.1.tar.gz is a source file of gcc, you can learn more about gcc through this codes f
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* LinkedHashMap.java -- a class providing hashtable data structure,   mapping Object --> Object, with linked list traversal   Copyright (C) 2001, 2002 Free Software Foundation, Inc.This file is part of GNU Classpath.GNU Classpath is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2, or (at your option)any later version.GNU Classpath is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with GNU Classpath; see the file COPYING.  If not, write to theFree Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307 USA.Linking this library statically or dynamically with other modules ismaking a combined work based on this library.  Thus, the terms andconditions of the GNU General Public License cover the wholecombination.As a special exception, the copyright holders of this library give youpermission to link this library with independent modules to produce anexecutable, regardless of the license terms of these independentmodules, and to copy and distribute the resulting executable underterms of your choice, provided that you also meet, for each linkedindependent module, the terms and conditions of the license of thatmodule.  An independent module is a module which is not derived fromor based on this library.  If you modify this library, you may extendthis exception to your version of the library, but you are notobligated to do so.  If you do not wish to do so, delete thisexception statement from your version. */package java.util;/** * This class provides a hashtable-backed implementation of the * Map interface, with predictable traversal order. * <p> * * It uses a hash-bucket approach; that is, hash collisions are handled * by linking the new node off of the pre-existing node (or list of * nodes).  In this manner, techniques such as linear probing (which * can cause primary clustering) and rehashing (which does not fit very * well with Java's method of precomputing hash codes) are avoided.  In * addition, this maintains a doubly-linked list which tracks either * insertion or access order. * <p> * * In insertion order, calling <code>put</code> adds the key to the end of * traversal, unless the key was already in the map; changing traversal order * requires removing and reinserting a key.  On the other hand, in access * order, all calls to <code>put</code> and <code>get</code> cause the * accessed key to move to the end of the traversal list.  Note that any * accesses to the map's contents via its collection views and iterators do * not affect the map's traversal order, since the collection views do not * call <code>put</code> or <code>get</code>. * <p> * * One of the nice features of tracking insertion order is that you can * copy a hashtable, and regardless of the implementation of the original, * produce the same results when iterating over the copy.  This is possible * without needing the overhead of <code>TreeMap</code>. * <p> * * When using this {@link #LinkedHashMap(int, float, boolean) constructor}, * you can build an access-order mapping.  This can be used to implement LRU * caches, for example.  By overriding {@link #removeEldestEntry(Map.Entry)}, * you can also control the removal of the oldest entry, and thereby do * things like keep the map at a fixed size. * <p> * * Under ideal circumstances (no collisions), LinkedHashMap offers O(1)  * performance on most operations (<code>containsValue()</code> is, * of course, O(n)).  In the worst case (all keys map to the same  * hash code -- very unlikely), most operations are O(n).  Traversal is * faster than in HashMap (proportional to the map size, and not the space * allocated for the map), but other operations may be slower because of the * overhead of the maintaining the traversal order list. * <p> * * LinkedHashMap accepts the null key and null values.  It is not * synchronized, so if you need multi-threaded access, consider using:<br> * <code>Map m = Collections.synchronizedMap(new LinkedHashMap(...));</code> * <p> * * The iterators are <i>fail-fast</i>, meaning that any structural * modification, except for <code>remove()</code> called on the iterator * itself, cause the iterator to throw a * {@link ConcurrentModificationException} rather than exhibit * non-deterministic behavior. * * @author Eric Blake (ebb9@email.byu.edu) * @see Object#hashCode() * @see Collection * @see Map * @see HashMap * @see TreeMap * @see Hashtable * @since 1.4 * @status updated to 1.4 */public class LinkedHashMap extends HashMap{  /**   * Compatible with JDK 1.4.   */  private static final long serialVersionUID = 3801124242820219131L;  /**   * The oldest Entry to begin iteration at.   */  transient LinkedHashEntry root;  /**   * The iteration order of this linked hash map: <code>true</code> for   * access-order, <code>false</code> for insertion-order.   *   * @serial true for access order traversal   */  final boolean accessOrder;  /**   * Class to represent an entry in the hash table. Holds a single key-value   * pair and the doubly-linked insertion order list.   */  class LinkedHashEntry extends HashEntry  {    /**     * The predecessor in the iteration list. If this entry is the root     * (eldest), pred points to the newest entry.     */    LinkedHashEntry pred;    /** The successor in the iteration list, null if this is the newest. */    LinkedHashEntry succ;    /**     * Simple constructor.     *     * @param key the key     * @param value the value     */    LinkedHashEntry(Object key, Object value)    {      super(key, value);      if (root == null)        {          root = this;          pred = this;        }      else        {          pred = root.pred;          pred.succ = this;          root.pred = this;        }    }    /**     * Called when this entry is accessed via put or get. This version does     * the necessary bookkeeping to keep the doubly-linked list in order,     * after moving this element to the newest position in access order.     */    void access()    {      if (accessOrder && succ != null)        {          modCount++;          if (this == root)            {              root = succ;              pred.succ = this;              succ = null;            }          else            {              pred.succ = succ;              succ.pred = pred;              succ = null;              pred = root.pred;              pred.succ = this;            }        }    }    /**     * Called when this entry is removed from the map. This version does     * the necessary bookkeeping to keep the doubly-linked list in order.     *     * @return the value of this key as it is removed     */    Object cleanup()    {      if (this == root)        {          root = succ;          if (succ != null)            succ.pred = pred;        }      else if (succ == null)        {          pred.succ = null;          root.pred = pred;        }      else        {          pred.succ = succ;          succ.pred = pred;        }      return value;    }  } // class LinkedHashEntry  /**   * Construct a new insertion-ordered LinkedHashMap with the default   * capacity (11) and the default load factor (0.75).   */  public LinkedHashMap()  {    super();    accessOrder = false;  }  /**   * Construct a new insertion-ordered LinkedHashMap from the given Map,   * with initial capacity the greater of the size of <code>m</code> or   * the default of 11.   * <p>   *   * Every element in Map m will be put into this new HashMap, in the   * order of m's iterator.   *   * @param m a Map whose key / value pairs will be put into   *          the new HashMap.  <b>NOTE: key / value pairs   *          are not cloned in this constructor.</b>   * @throws NullPointerException if m is null   */  public LinkedHashMap(Map m)  {    super(m);    accessOrder = false;  }  /**

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -