📄 collections.java
字号:
a[swap] = i.previous(); } else o = l.set(swap, i.previous()); i.set(o); } } /** * Obtain an immutable Set consisting of a single element. The return value * of this method is Serializable. * * @param o the single element * @return an immutable Set containing only o * @see Serializable */ public static Set singleton(Object o) { return new SingletonSet(o); } /** * The implementation of {@link #singleton(Object)}. This class name * is required for compatibility with Sun's JDK serializability. * * @author Eric Blake (ebb9@email.byu.edu) */ private static final class SingletonSet extends AbstractSet implements Serializable { /** * Compatible with JDK 1.4. */ private static final long serialVersionUID = 3193687207550431679L; /** * The single element; package visible for use in nested class. * @serial the singleton */ final Object element; /** * Construct a singleton. * @param o the element */ SingletonSet(Object o) { element = o; } /** * The size: always 1! * @return 1. */ public int size() { return 1; } /** * Returns an iterator over the lone element. */ public Iterator iterator() { return new Iterator() { /** * Flag to indicate whether or not the element has * been retrieved. */ private boolean hasNext = true; /** * Returns <code>true</code> if elements still remain to be * iterated through. * * @return <code>true</code> if the element has not yet been returned. */ public boolean hasNext() { return hasNext; } /** * Returns the element. * * @return The element used by this singleton. * @throws NoSuchElementException if the object * has already been retrieved. */ public Object next() { if (hasNext) { hasNext = false; return element; } else throw new NoSuchElementException(); } /** * Removes the element from the singleton. * As this set is immutable, this will always * throw an exception. * * @throws UnsupportedOperationException as the * singleton set doesn't support * <code>remove()</code>. */ public void remove() { throw new UnsupportedOperationException(); } }; } // The remaining methods are optional, but provide a performance // advantage by not allocating unnecessary iterators in AbstractSet. /** * The set only contains one element. * * @param o The object to search for. * @return <code>true</code> if o == the element of the singleton. */ public boolean contains(Object o) { return equals(o, element); } /** * This is true if the other collection only contains the element. * * @param c A collection to compare against this singleton. * @return <code>true</code> if c only contains either no elements or * elements equal to the element in this singleton. */ public boolean containsAll(Collection c) { Iterator i = c.iterator(); int pos = c.size(); while (--pos >= 0) if (! equals(i.next(), element)) return false; return true; } /** * The hash is just that of the element. * * @return The hashcode of the element. */ public int hashCode() { return hashCode(element); } /** * Returning an array is simple. * * @return An array containing the element. */ public Object[] toArray() { return new Object[] {element}; } /** * Obvious string. * * @return The string surrounded by enclosing * square brackets. */ public String toString() { return "[" + element + "]"; } } // class SingletonSet /** * Obtain an immutable List consisting of a single element. The return value * of this method is Serializable, and implements RandomAccess. * * @param o the single element * @return an immutable List containing only o * @see Serializable * @see RandomAccess * @since 1.3 */ public static List singletonList(Object o) { return new SingletonList(o); } /** * The implementation of {@link #singletonList(Object)}. This class name * is required for compatibility with Sun's JDK serializability. * * @author Eric Blake (ebb9@email.byu.edu) */ private static final class SingletonList extends AbstractList implements Serializable, RandomAccess { /** * Compatible with JDK 1.4. */ private static final long serialVersionUID = 3093736618740652951L; /** * The single element. * @serial the singleton */ private final Object element; /** * Construct a singleton. * @param o the element */ SingletonList(Object o) { element = o; } /** * The size: always 1! * @return 1. */ public int size() { return 1; } /** * Only index 0 is valid. * @param index The index of the element * to retrieve. * @return The singleton's element if the * index is 0. * @throws IndexOutOfBoundsException if * index is not 0. */ public Object get(int index) { if (index == 0) return element; throw new IndexOutOfBoundsException(); } // The remaining methods are optional, but provide a performance // advantage by not allocating unnecessary iterators in AbstractList. /** * The set only contains one element. * * @param o The object to search for. * @return <code>true</code> if o == the singleton element. */ public boolean contains(Object o) { return equals(o, element); } /** * This is true if the other collection only contains the element. * * @param c A collection to compare against this singleton. * @return <code>true</code> if c only contains either no elements or * elements equal to the element in this singleton. */ public boolean containsAll(Collection c) { Iterator i = c.iterator(); int pos = c.size(); while (--pos >= 0) if (! equals(i.next(), element)) return false; return true; } /** * Speed up the hashcode computation. * * @return The hashcode of the list, based * on the hashcode of the singleton element. */ public int hashCode() { return 31 + hashCode(element); } /** * Either the list has it or not. * * @param o The object to find the first index of. * @return 0 if o is the singleton element, -1 if not. */ public int indexOf(Object o) { return equals(o, element) ? 0 : -1; } /** * Either the list has it or not. * * @param o The object to find the last index of. * @return 0 if o is the singleton element, -1 if not. */ public int lastIndexOf(Object o) { return equals(o, element) ? 0 : -1; } /** * Sublists are limited in scope. * * @param from The starting bound for the sublist. * @param to The ending bound for the sublist. * @return Either an empty list if both bounds are * 0 or 1, or this list if the bounds are 0 and 1. * @throws IllegalArgumentException if <code>from > to</code> * @throws IndexOutOfBoundsException if either bound is greater * than 1. */ public List subList(int from, int to) { if (from == to && (to == 0 || to == 1)) return EMPTY_LIST; if (from == 0 && to == 1) return this; if (from > to) throw new IllegalArgumentException(); throw new IndexOutOfBoundsException(); } /** * Returning an array is simple. * * @return An array containing the element. */ public Object[] toArray() { return new Object[] {element}; } /** * Obvious string. * * @return The string surrounded by enclosing * square brackets. */ public String toString() { return "[" + element + "]"; } } // class SingletonList /** * Obtain an immutable Map consisting of a single key-value pair. * The return value of this method is Serializable. * * @param key the single key * @param value the single value * @return an immutable Map containing only the single key-value pair * @see Serializable * @since 1.3 */ public static Map singletonMap(Object key, Object value) { return new SingletonMap(key, value); } /** * The implementation of {@link #singletonMap(Object, Object)}. This class * name is required for compatibility with Sun's JDK serializability. * * @author Eric Blake (ebb9@email.byu.edu) */ private static final class SingletonMap extends AbstractMap implements Serializable { /** * Compatible with JDK 1.4. */ private static final long serialVersionUID = -6979724477215052911L; /** * The single key. * @serial the singleton key */ private final Object k; /** * The corresponding value. * @serial the singleton value */ private final Object v; /** * Cache the entry set. */ private transient Set entries; /** * Construct a singleton. * @param key the key * @param value the value */ SingletonMap(Object key, Object value) { k = key; v = value; } /** * There is a single immutable entry. * * @return A singleton containing the map entry. */ public Set entrySet() { if (entries == null) entries = singleton(new AbstractMap.BasicMapEntry(k, v) { /** * Sets the value of the map entry to the supplied value. * An exception is always thrown, as the map is immutable. * * @param o The new value. * @return The old value. * @throws UnsupportedOperationException as setting the value * is not supported. */ public Object setValue(Object o) { throw new UnsupportedOperationException(); } }); return entries; } // The remaining methods are optional, but provide a performance // advantage by not allocating unnecessary iterators in AbstractMap. /** * Single entry. * * @param key The key to look for. * @return <code>true</code> if the key is the same as the one used by * this map. */ public boolean containsKey(Object key) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -