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

📄 collections.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/* Collections.java -- Utility class with methods to operate on collections   Copyright (C) 1998, 1999, 2000, 2001, 2002, 2004, 2005   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., 51 Franklin Street, Fifth Floor, Boston, MA02110-1301 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;import java.io.Serializable;/** * Utility class consisting of static methods that operate on, or return * Collections. Contains methods to sort, search, reverse, fill and shuffle * Collections, methods to facilitate interoperability with legacy APIs that * are unaware of collections, a method to return a list which consists of * multiple copies of one element, and methods which "wrap" collections to give * them extra properties, such as thread-safety and unmodifiability. * <p> * * All methods which take a collection throw a {@link NullPointerException} if * that collection is null. Algorithms which can change a collection may, but * are not required, to throw the {@link UnsupportedOperationException} that * the underlying collection would throw during an attempt at modification. * For example, * <code>Collections.singleton("").addAll(Collections.EMPTY_SET)</code> * does not throw a exception, even though addAll is an unsupported operation * on a singleton; the reason for this is that addAll did not attempt to * modify the set. * * @author Original author unknown * @author Eric Blake (ebb9@email.byu.edu) * @see Collection * @see Set * @see List * @see Map * @see Arrays * @since 1.2 * @status updated to 1.4 */public class Collections{  /**   * Constant used to decide cutoff for when a non-RandomAccess list should   * be treated as sequential-access. Basically, quadratic behavior is   * acceptable for small lists when the overhead is so small in the first   * place. I arbitrarily set it to 16, so it may need some tuning.   */  private static final int LARGE_LIST_SIZE = 16;  /**   * Determines if a list should be treated as a sequential-access one.   * Rather than the old method of JDK 1.3 of assuming only instanceof   * AbstractSequentialList should be sequential, this uses the new method   * of JDK 1.4 of assuming anything that does NOT implement RandomAccess   * and exceeds a large (unspecified) size should be sequential.   *   * @param l the list to check   * @return <code>true</code> if it should be treated as sequential-access   */  private static boolean isSequential(List l)  {    return ! (l instanceof RandomAccess) && l.size() > LARGE_LIST_SIZE;  }  /**   * This class is non-instantiable.   */  private Collections()  {  }  /**   * An immutable, serializable, empty Set.   * @see Serializable   */  public static final Set EMPTY_SET = new EmptySet();  /**   * The implementation of {@link #EMPTY_SET}. This class name is required   * for compatibility with Sun's JDK serializability.   *   * @author Eric Blake (ebb9@email.byu.edu)   */  private static final class EmptySet extends AbstractSet    implements Serializable  {    /**     * Compatible with JDK 1.4.     */    private static final long serialVersionUID = 1582296315990362920L;    /**     * A private constructor adds overhead.     */    EmptySet()    {    }    /**     * The size: always 0!     * @return 0.     */    public int size()    {      return 0;    }    /**     * Returns an iterator that does not iterate.     * @return A non-iterating iterator.     */    // This is really cheating! I think it's perfectly valid, though.    public Iterator iterator()    {      return EMPTY_LIST.iterator();    }    // The remaining methods are optional, but provide a performance    // advantage by not allocating unnecessary iterators in AbstractSet.    /**     * The empty set never contains anything.     * @param o The object to search for.     * @return <code>false</code>.     */    public boolean contains(Object o)    {      return false;    }    /**     * This is true only if the given collection is also empty.     * @param c The collection of objects which are to be compared     *          against the members of this set.     * @return <code>true</code> if c is empty.     */    public boolean containsAll(Collection c)    {      return c.isEmpty();    }    /**     * Equal only if the other set is empty.     * @param o The object to compare with this set.     * @return <code>true</code> if o is an empty instance of <code>Set</code>.     */    public boolean equals(Object o)    {      return o instanceof Set && ((Set) o).isEmpty();    }    /**     * The hashcode is always 0.     * @return 0.     */    public int hashCode()    {      return 0;    }    /**     * Always succeeds with a <code>false</code> result.     * @param o The object to remove.     * @return <code>false</code>.     */    public boolean remove(Object o)    {      return false;    }    /**     * Always succeeds with a <code>false</code> result.     * @param c The collection of objects which should     *          all be removed from this set.     * @return <code>false</code>.     */    public boolean removeAll(Collection c)    {      return false;    }    /**     * Always succeeds with a <code>false</code> result.     * @param c The collection of objects which should     *          all be retained within this set.     * @return <code>false</code>.     */    public boolean retainAll(Collection c)    {      return false;    }    /**     * The array is always empty.     * @return A new array with a size of 0.     */    public Object[] toArray()    {      return new Object[0];    }    /**     * We don't even need to use reflection!     * @param a An existing array, which can be empty.     * @return The original array with any existing     *         initial element set to null.     */    public Object[] toArray(Object[] a)    {      if (a.length > 0)        a[0] = null;      return a;    }    /**     * The string never changes.     *     * @return the string "[]".     */    public String toString()    {      return "[]";    }  } // class EmptySet  /**   * An immutable, serializable, empty List, which implements RandomAccess.   * @see Serializable   * @see RandomAccess   */  public static final List EMPTY_LIST = new EmptyList();  /**   * The implementation of {@link #EMPTY_LIST}. This class name is required   * for compatibility with Sun's JDK serializability.   *   * @author Eric Blake (ebb9@email.byu.edu)   */  private static final class EmptyList extends AbstractList    implements Serializable, RandomAccess  {    /**     * Compatible with JDK 1.4.     */    private static final long serialVersionUID = 8842843931221139166L;    /**     * A private constructor adds overhead.     */    EmptyList()    {    }    /**     * The size is always 0.     * @return 0.     */    public int size()    {      return 0;    }    /**     * No matter the index, it is out of bounds.  This     * method never returns, throwing an exception instead.     *     * @param index The index of the element to retrieve.     * @return the object at the specified index.     * @throws IndexOutOfBoundsException as any given index     *         is outside the bounds of an empty array.     */    public Object get(int index)    {      throw new IndexOutOfBoundsException();    }    // The remaining methods are optional, but provide a performance    // advantage by not allocating unnecessary iterators in AbstractList.    /**     * Never contains anything.     * @param o The object to search for.     * @return <code>false</code>.     */    public boolean contains(Object o)    {      return false;    }    /**     * This is true only if the given collection is also empty.     * @param c The collection of objects, which should be compared     *          against the members of this list.     * @return <code>true</code> if c is also empty.      */    public boolean containsAll(Collection c)    {      return c.isEmpty();    }    /**     * Equal only if the other list is empty.     * @param o The object to compare against this list.     * @return <code>true</code> if o is also an empty instance of     *         <code>List</code>.     */    public boolean equals(Object o)    {      return o instanceof List && ((List) o).isEmpty();    }    /**     * The hashcode is always 1.     * @return 1.     */    public int hashCode()    {      return 1;    }    /**     * Returns -1.     * @param o The object to search for.     * @return -1.     */    public int indexOf(Object o)    {      return -1;    }    /**     * Returns -1.     * @param o The object to search for.     * @return -1.     */    public int lastIndexOf(Object o)    {      return -1;    }    /**     * Always succeeds with <code>false</code> result.     * @param o The object to remove.     * @return -1.     */    public boolean remove(Object o)    {      return false;    }    /**     * Always succeeds with <code>false</code> result.     * @param c The collection of objects which should     *          all be removed from this list.     * @return <code>false</code>.     */    public boolean removeAll(Collection c)    {      return false;    }    /**     * Always succeeds with <code>false</code> result.     * @param c The collection of objects which should     *          all be retained within this list.     * @return <code>false</code>.     */    public boolean retainAll(Collection c)    {      return false;    }    /**     * The array is always empty.     * @return A new array with a size of 0.     */    public Object[] toArray()    {      return new Object[0];    }    /**     * We don't even need to use reflection!     * @param a An existing array, which can be empty.     * @return The original array with any existing     *         initial element set to null.     */    public Object[] toArray(Object[] a)    {      if (a.length > 0)        a[0] = null;      return a;    }    /**     * The string never changes.     *     * @return the string "[]".     */    public String toString()    {      return "[]";    }  } // class EmptyList  /**   * An immutable, serializable, empty Map.   * @see Serializable   */  public static final Map EMPTY_MAP = new EmptyMap();  /**   * The implementation of {@link #EMPTY_MAP}. This class name is required   * for compatibility with Sun's JDK serializability.   *   * @author Eric Blake (ebb9@email.byu.edu)   */  private static final class EmptyMap extends AbstractMap    implements Serializable  {    /**     * Compatible with JDK 1.4.

⌨️ 快捷键说明

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