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

📄 collections.java

📁 gcc的组建
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
      return equals(key, k);    }    /**     * Single entry.     *     * @param value The value to look for.     * @return <code>true</code> if the value is the same as the one used by     *         this map.     */    public boolean containsValue(Object value)    {      return equals(value, v);    }    /**     * Single entry.     *     * @param key The key of the value to be retrieved.     * @return The singleton value if the key is the same as the     *         singleton key, null otherwise.     */    public Object get(Object key)    {      return equals(key, k) ? v : null;    }    /**     * Calculate the hashcode directly.     *     * @return The hashcode computed from the singleton key     *         and the singleton value.     */    public int hashCode()    {      return hashCode(k) ^ hashCode(v);    }    /**     * Return the keyset.     *     * @return A singleton containing the key.     */    public Set keySet()    {      if (keys == null)        keys = singleton(k);      return keys;    }    /**     * The size: always 1!     *     * @return 1.     */    public int size()    {      return 1;    }    /**     * Return the values. Technically, a singleton, while more specific than     * a general Collection, will work. Besides, that's what the JDK uses!     *     * @return A singleton containing the value.     */    public Collection values()    {      if (values == null)        values = singleton(v);      return values;    }    /**     * Obvious string.     *     * @return A string containing the string representations of the key     *         and its associated value.     */    public String toString()    {      return "{" + k + "=" + v + "}";    }  } // class SingletonMap  /**   * Sort a list according to the natural ordering of its elements. The list   * must be modifiable, but can be of fixed size. The sort algorithm is   * precisely that used by Arrays.sort(Object[]), which offers guaranteed   * nlog(n) performance. This implementation dumps the list into an array,   * sorts the array, and then iterates over the list setting each element from   * the array.   *   * @param l the List to sort   * @throws ClassCastException if some items are not mutually comparable   * @throws UnsupportedOperationException if the List is not modifiable   * @throws NullPointerException if some element is null   * @see Arrays#sort(Object[])   */  public static void sort(List l)  {    sort(l, null);  }  /**   * Sort a list according to a specified Comparator. The list must be   * modifiable, but can be of fixed size. The sort algorithm is precisely that   * used by Arrays.sort(Object[], Comparator), which offers guaranteed   * nlog(n) performance. This implementation dumps the list into an array,   * sorts the array, and then iterates over the list setting each element from   * the array.   *   * @param l the List to sort   * @param c the Comparator specifying the ordering for the elements, or   *        null for natural ordering   * @throws ClassCastException if c will not compare some pair of items   * @throws UnsupportedOperationException if the List is not modifiable   * @throws NullPointerException if null is compared by natural ordering   *        (only possible when c is null)   * @see Arrays#sort(Object[], Comparator)   */  public static void sort(List l, Comparator c)  {    Object[] a = l.toArray();    Arrays.sort(a, c);    ListIterator i = l.listIterator();    for (int pos = 0, alen = a.length;  pos < alen;  pos++)      {        i.next();        i.set(a[pos]);      }  }  /**   * Swaps the elements at the specified positions within the list. Equal   * positions have no effect.   *   * @param l the list to work on   * @param i the first index to swap   * @param j the second index   * @throws UnsupportedOperationException if list.set is not supported   * @throws IndexOutOfBoundsException if either i or j is &lt; 0 or &gt;=   *         list.size()   * @since 1.4   */  public static void swap(List l, int i, int j)  {    l.set(i, l.set(j, l.get(i)));  }  /**   * Returns a synchronized (thread-safe) collection wrapper backed by the   * given collection. Notice that element access through the iterators   * is thread-safe, but if the collection can be structurally modified   * (adding or removing elements) then you should synchronize around the   * iteration to avoid non-deterministic behavior:<br>   * <pre>   * Collection c = Collections.synchronizedCollection(new Collection(...));   * ...   * synchronized (c)   *   {   *     Iterator i = c.iterator();   *     while (i.hasNext())   *       foo(i.next());   *   }   * </pre><p>   *   * Since the collection might be a List or a Set, and those have incompatible   * equals and hashCode requirements, this relies on Object's implementation   * rather than passing those calls on to the wrapped collection. The returned   * Collection implements Serializable, but can only be serialized if   * the collection it wraps is likewise Serializable.   *   * @param c the collection to wrap   * @return a synchronized view of the collection   * @see Serializable   */  public static Collection synchronizedCollection(Collection c)  {    return new SynchronizedCollection(c);  }  /**   * The implementation of {@link #synchronizedCollection(Collection)}. This   * class name is required for compatibility with Sun's JDK serializability.   * Package visible, so that collections such as the one for   * Hashtable.values() can specify which object to synchronize on.   *   * @author Eric Blake (ebb9@email.byu.edu)   */  static class SynchronizedCollection    implements Collection, Serializable  {    /**     * Compatible with JDK 1.4.     */    private static final long serialVersionUID = 3053995032091335093L;    /**     * The wrapped collection. Package visible for use by subclasses.     * @serial the real collection     */    final Collection c;    /**     * The object to synchronize on.  When an instance is created via public     * methods, it will be this; but other uses like SynchronizedMap.values()     * must specify another mutex. Package visible for use by subclasses.     * @serial the lock     */    final Object mutex;    /**     * Wrap a given collection.     * @param c the collection to wrap     * @throws NullPointerException if c is null     */    SynchronizedCollection(Collection c)    {      this.c = c;      mutex = this;      if (c == null)        throw new NullPointerException();    }    /**     * Called only by trusted code to specify the mutex as well as the     * collection.     * @param sync the mutex     * @param c the collection     */    SynchronizedCollection(Object sync, Collection c)    {      this.c = c;      mutex = sync;    }    /**     * Adds the object to the underlying collection, first     * obtaining a lock on the mutex.     *     * @param o The object to add.     * @return <code>true</code> if the collection was modified as a result     *         of this action.     * @throws UnsupportedOperationException if this collection does not     *         support the add operation.     * @throws ClassCastException if o cannot be added to this collection due     *         to its type.     * @throws NullPointerException if o is null and this collection doesn't     *         support the addition of null values.     * @throws IllegalArgumentException if o cannot be added to this     *         collection for some other reason.     */    public boolean add(Object o)    {      synchronized (mutex)        {          return c.add(o);        }    }    /**     * Adds the objects in col to the underlying collection, first     * obtaining a lock on the mutex.     *     * @param col The collection to take the new objects from.     * @return <code>true</code> if the collection was modified as a result     *          of this action.     * @throws UnsupportedOperationException if this collection does not     *         support the addAll operation.     * @throws ClassCastException if some element of col cannot be added to this     *         collection due to its type.     * @throws NullPointerException if some element of col is null and this     *         collection does not support the addition of null values.     * @throws NullPointerException if col itself is null.     * @throws IllegalArgumentException if some element of col cannot be added     *         to this collection for some other reason.     */    public boolean addAll(Collection col)    {      synchronized (mutex)        {          return c.addAll(col);        }    }    /**     * Removes all objects from the underlying collection,     * first obtaining a lock on the mutex.     *     * @throws UnsupportedOperationException if this collection does not     *         support the clear operation.     */    public void clear()    {      synchronized (mutex)        {          c.clear();        }    }    /**     * Checks for the existence of o within the underlying     * collection, first obtaining a lock on the mutex.     *     * @param o the element to look for.     * @return <code>true</code> if this collection contains at least one     *         element e such that <code>o == null ? e == null : o.equals(e)</code>.     * @throws ClassCastException if the type of o is not a valid type for this     *         collection.     * @throws NullPointerException if o is null and this collection doesn't     *         support null values.     */    public boolean contains(Object o)    {      synchronized (mutex)        {          return c.contains(o);        }    }    /**     * Checks for the existence of each object in cl     * within the underlying collection, first obtaining     * a lock on the mutex.     *     * @param c1 the collection to test for.     * @return <code>true</code> if for every element o in c, contains(o)     *         would return <code>true</code>.     * @throws ClassCastException if the type of any element in cl is not a valid     *         type for this collection.     * @throws NullPointerException if some element of cl is null and this     *         collection does not support null values.     * @throws NullPointerException if cl itself is null.     */    public boolean containsAll(Collection c1)    {      synchronized (mutex)        {          return c.containsAll(c1);        }    }    /**     * Returns <code>true</code> if there are no objects in the underlying     * collection.  A lock on the mutex is obtained before the     * check is performed.     *     * @return <code>true</code> if this collection contains no elements.     */    public boolean isEmpty()    {      synchronized (mutex)        {          return c.isEmpty();        }    }    /**     * Returns a synchronized iterator wrapper around the underlying     * collection's iterator.  A lock on the mutex is obtained before     * retrieving the collection's iterator.     *     * @return An iterator over the elements in the underlying collection,     *         which returns each element in any order.     */    public Iterator iterator()    {      synchronized (mutex)        {          return new SynchronizedIterator(mutex, c.iterator());        }    }    /**     * Removes the specified object from the underlying collection,     * first obtaining a lock on the mutex.     *     * @param o The object to remove.     * @return <code>true</code> if the collection changed as a result of this call, that is,     *         if the collection contained at least one occurrence of o.     * @throws UnsupportedOperationException if this collection does not     *         support the remove operation.     * @throws ClassCastException if the type of o is not a valid type     *         for this collection.     * @throws NullPointerException if o is null and the collection doesn't     *         support null values.     */    public boolean remove(Object o)    {      synchronized (mutex)        {          return c.remove(o);        }    }    /**     * Removes all elements, e, of the underlying     * collection for which <code>col.contains(e)</code>     * returns <code>true</code>.  A lock on the mutex is obtained     * before the operation proceeds.     *     * @param col The collection of objects to be removed.     * @return <code>true</code> if this collection was modified as a result of this call.     * @throws UnsupportedOperationException if this collection does not     *   support the removeAll operation.     * @throws ClassCastException if the type of any element in c is not a valid     *   type for this collection.     * @throws NullPointerException if some element of c is null and this     *   collection does not support removing null values.     * @throws NullPointerException if c itself is null.     */    public boolean removeAll(Collection col)    {      synchronized (mutex)        {          return c.removeAll(col);        }    }    /**     * Retains all elements, e, of the underlying     * collection for which <code>col.contains(e)</code>     * returns <code>true</code>.  That is, every element that doesn't     * exist in col is removed.  A lock on the mutex is obtained     * before the operation proceeds.     *     * @param col The collection of objects to be removed.     * @return <code>true</code> if this collection was modified as a result of this call.     * @throws UnsupportedOperationException if this collection does not     *   support the removeAll operation.     * @throws ClassCastException if the type of any element in c is not a valid     *   type for this collection.     * @throws NullPointerException if some element of c is null and this     *   collection does not support removing null values.     * @throws NullPointerException if c itself is null.     */    public boolean retainAll(Collection col)    {      synchronized (mutex)        {          return c.retainAll(col);        }    }    /**     * Retrieves the size of the underlying collection.     * A lock on the mutex is obtained before the collection     * is accessed.     *     * @return Th

⌨️ 快捷键说明

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