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

📄 abstractmap.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 页
字号:
/* AbstractMap.java -- Abstract implementation of most of Map   Copyright (C) 1998, 1999, 2000, 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;/** * An abstract implementation of Map to make it easier to create your own * implementations. In order to create an unmodifiable Map, subclass * AbstractMap and implement the <code>entrySet</code> (usually via an * AbstractSet).  To make it modifiable, also implement <code>put</code>, * and have <code>entrySet().iterator()</code> support <code>remove</code>. * <p> * * It is recommended that classes which extend this support at least the * no-argument constructor, and a constructor which accepts another Map. * Further methods in this class may be overridden if you have a more * efficient implementation. * * @author Original author unknown * @author Bryce McKinlay * @author Eric Blake <ebb9@email.byu.edu> * @see Map * @see Collection * @see HashMap * @see LinkedHashMap * @see TreeMap * @see WeakHashMap * @see IdentityHashMap * @since 1.2 * @status updated to 1.4 */public abstract class AbstractMap implements Map{  /** An "enum" of iterator types. */  // Package visible for use by subclasses.  static final int KEYS = 0,                   VALUES = 1,                   ENTRIES = 2;  /**   * The cache for {@link #keySet()}.   */  // Package visible for use by subclasses.  Set keys;  /**   * The cache for {@link #values()}.   */  // Package visible for use by subclasses.  Collection values;  /**   * The main constructor, for use by subclasses.   */  protected AbstractMap()  {  }  /**   * Remove all entries from this Map (optional operation). This default   * implementation calls entrySet().clear(). NOTE: If the entry set does   * not permit clearing, then this will fail, too. Subclasses often   * override this for efficiency.  Your implementation of entrySet() should   * not call <code>AbstractMap.clear</code> unless you want an infinite loop.   *   * @throws UnsupportedOperationException if <code>entrySet().clear()</code>   *         does not support clearing.   * @see Set#clear()   */  public void clear()  {    entrySet().clear();  }  /**   * Create a shallow copy of this Map, no keys or values are copied. The   * default implementation simply calls <code>super.clone()</code>.   *   * @return the shallow clone   * @throws CloneNotSupportedException if a subclass is not Cloneable   * @see Cloneable   * @see Object#clone()   */  protected Object clone() throws CloneNotSupportedException  {    AbstractMap copy = (AbstractMap) super.clone();    // Clear out the caches; they are stale.    copy.keys = null;    copy.values = null;    return copy;  }  /**   * Returns true if this contains a mapping for the given key. This   * implementation does a linear search, O(n), over the   * <code>entrySet()</code>, returning <code>true</code> if a match   * is found, <code>false</code> if the iteration ends. Many subclasses   * can implement this more efficiently.   *   * @param key the key to search for   * @return true if the map contains the key   * @throws NullPointerException if key is <code>null</code> but the map   *         does not permit null keys   * @see #containsValue(Object)   */  public boolean containsKey(Object key)  {    Iterator entries = entrySet().iterator();    int pos = size();    while (--pos >= 0)      if (equals(key, ((Map.Entry) entries.next()).getKey()))        return true;    return false;  }  /**   * Returns true if this contains at least one mapping with the given value.   * This implementation does a linear search, O(n), over the   * <code>entrySet()</code>, returning <code>true</code> if a match   * is found, <code>false</code> if the iteration ends. A match is   * defined as <code>(value == null ? v == null : value.equals(v))</code>   * Subclasses are unlikely to implement this more efficiently.   *   * @param value the value to search for   * @return true if the map contains the value   * @see #containsKey(Object)   */  public boolean containsValue(Object value)  {    Iterator entries = entrySet().iterator();    int pos = size();    while (--pos >= 0)      if (equals(value, ((Map.Entry) entries.next()).getValue()))        return true;    return false;  }  /**   * Returns a set view of the mappings in this Map.  Each element in the   * set must be an implementation of Map.Entry.  The set is backed by   * the map, so that changes in one show up in the other.  Modifications   * made while an iterator is in progress cause undefined behavior.  If   * the set supports removal, these methods must be valid:   * <code>Iterator.remove</code>, <code>Set.remove</code>,   * <code>removeAll</code>, <code>retainAll</code>, and <code>clear</code>.   * Element addition is not supported via this set.   *   * @return the entry set   * @see Map.Entry   */  public abstract Set entrySet();  /**   * Compares the specified object with this map for equality. Returns   * <code>true</code> if the other object is a Map with the same mappings,   * that is,<br>   * <code>o instanceof Map && entrySet().equals(((Map) o).entrySet();</code>   *   * @param o the object to be compared   * @return true if the object equals this map   * @see Set#equals(Object)   */  public boolean equals(Object o)  {    return (o == this ||            (o instanceof Map &&             entrySet().equals(((Map) o).entrySet())));  }  /**   * Returns the value mapped by the given key. Returns <code>null</code> if   * there is no mapping.  However, in Maps that accept null values, you   * must rely on <code>containsKey</code> to determine if a mapping exists.   * This iteration takes linear time, searching entrySet().iterator() of   * the key.  Many implementations override this method.   *   * @param key the key to look up   * @return the value associated with the key, or null if key not in map   * @throws NullPointerException if this map does not accept null keys   * @see #containsKey(Object)   */  public Object get(Object key)  {    Iterator entries = entrySet().iterator();    int pos = size();    while (--pos >= 0)      {        Map.Entry entry = (Map.Entry) entries.next();        if (equals(key, entry.getKey()))          return entry.getValue();      }    return null;  }  /**   * Returns the hash code for this map. As defined in Map, this is the sum   * of all hashcodes for each Map.Entry object in entrySet, or basically   * entrySet().hashCode().   *   * @return the hash code   * @see Map.Entry#hashCode()   * @see Set#hashCode()   */  public int hashCode()  {    return entrySet().hashCode();  }  /**   * Returns true if the map contains no mappings. This is implemented by   * <code>size() == 0</code>.   *   * @return true if the map is empty   * @see #size()   */  public boolean isEmpty()  {    return size() == 0;  }  /**   * Returns a set view of this map's keys. The set is backed by the map,   * so changes in one show up in the other. Modifications while an iteration   * is in progress produce undefined behavior. The set supports removal   * if entrySet() does, but does not support element addition.   * <p>   *   * This implementation creates an AbstractSet, where the iterator wraps   * the entrySet iterator, size defers to the Map's size, and contains   * defers to the Map's containsKey. The set is created on first use, and   * returned on subsequent uses, although since no synchronization occurs,   * there is a slight possibility of creating two sets.   *   * @return a Set view of the keys   * @see Set#iterator()   * @see #size()   * @see #containsKey(Object)   * @see #values()   */  public Set keySet()  {    if (keys == null)      keys = new AbstractSet()      {        public int size()        {          return AbstractMap.this.size();        }        public boolean contains(Object key)        {          return containsKey(key);        }        public Iterator iterator()        {          return new Iterator()          {            private final Iterator map_iterator = entrySet().iterator();            public boolean hasNext()            {              return map_iterator.hasNext();            }            public Object next()            {              return ((Map.Entry) map_iterator.next()).getKey();            }            public void remove()            {              map_iterator.remove();            }          };        }      };    return keys;  }  /**   * Associates the given key to the given value (optional operation). If the   * map already contains the key, its value is replaced. This implementation   * simply throws an UnsupportedOperationException. Be aware that in a map   * that permits <code>null</code> values, a null return does not always   * imply that the mapping was created.   *   * @param key the key to map   * @param value the value to be mapped   * @return the previous value of the key, or null if there was no mapping   * @throws UnsupportedOperationException if the operation is not supported

⌨️ 快捷键说明

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