src/share/classes/java/util/Collections.java

Print this page
rev 7893 : 8023275: Wrapping collections should override default methods
Reviewed-by:


  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 import java.io.Serializable;
  28 import java.io.ObjectOutputStream;
  29 import java.io.IOException;
  30 import java.io.InvalidObjectException;
  31 import java.lang.reflect.Array;
  32 import java.util.function.BiConsumer;
  33 import java.util.function.BiFunction;
  34 import java.util.function.Consumer;
  35 import java.util.function.Function;
  36 import java.util.function.Predicate;
  37 import java.util.function.UnaryOperator;

  38 import java.util.stream.Stream;
  39 import java.util.stream.StreamSupport;
  40 
  41 /**
  42  * This class consists exclusively of static methods that operate on or return
  43  * collections.  It contains polymorphic algorithms that operate on
  44  * collections, "wrappers", which return a new collection backed by a
  45  * specified collection, and a few other odds and ends.
  46  *
  47  * <p>The methods of this class all throw a <tt>NullPointerException</tt>
  48  * if the collections or class objects provided to them are null.
  49  *
  50  * <p>The documentation for the polymorphic algorithms contained in this class
  51  * generally includes a brief description of the <i>implementation</i>.  Such
  52  * descriptions should be regarded as <i>implementation notes</i>, rather than
  53  * parts of the <i>specification</i>.  Implementors should feel free to
  54  * substitute other algorithms, so long as the specification itself is adhered
  55  * to.  (For example, the algorithm used by <tt>sort</tt> does not have to be
  56  * a mergesort, but it does have to be <i>stable</i>.)
  57  *


1131             throw new UnsupportedOperationException();
1132         }
1133         public void clear() {
1134             throw new UnsupportedOperationException();
1135         }
1136 
1137         // Override default methods in Collection
1138         @Override
1139         public void forEach(Consumer<? super E> action) {
1140             c.forEach(action);
1141         }
1142         @Override
1143         public boolean removeIf(Predicate<? super E> filter) {
1144             throw new UnsupportedOperationException();
1145         }
1146         @SuppressWarnings("unchecked")
1147         @Override
1148         public Spliterator<E> spliterator() {
1149             return (Spliterator<E>)c.spliterator();
1150         }
1151 







1152     }
1153 
1154     /**
1155      * Returns an unmodifiable view of the specified set.  This method allows
1156      * modules to provide users with "read-only" access to internal sets.
1157      * Query operations on the returned set "read through" to the specified
1158      * set, and attempts to modify the returned set, whether direct or via its
1159      * iterator, result in an <tt>UnsupportedOperationException</tt>.<p>
1160      *
1161      * The returned set will be serializable if the specified set
1162      * is serializable.
1163      *
1164      * @param  <T> the class of the objects in the set
1165      * @param  s the set for which an unmodifiable view is to be returned.
1166      * @return an unmodifiable view of the specified set.
1167      */
1168     public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
1169         return new UnmodifiableSet<>(s);
1170     }
1171 


1992         public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
1993             return unmodifiableNavigableMap(
1994                 nm.subMap(fromKey, fromInclusive, toKey, toInclusive));
1995         }
1996 
1997         public NavigableMap<K, V> headMap(K toKey, boolean inclusive)
1998              { return unmodifiableNavigableMap(nm.headMap(toKey, inclusive)); }
1999         public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive)
2000            { return unmodifiableNavigableMap(nm.tailMap(fromKey, inclusive)); }
2001     }
2002 
2003     // Synch Wrappers
2004 
2005     /**
2006      * Returns a synchronized (thread-safe) collection backed by the specified
2007      * collection.  In order to guarantee serial access, it is critical that
2008      * <strong>all</strong> access to the backing collection is accomplished
2009      * through the returned collection.<p>
2010      *
2011      * It is imperative that the user manually synchronize on the returned
2012      * collection when traversing it via {@link Iterator} or
2013      * {@link Spliterator}:
2014      * <pre>
2015      *  Collection c = Collections.synchronizedCollection(myCollection);
2016      *     ...
2017      *  synchronized (c) {
2018      *      Iterator i = c.iterator(); // Must be in the synchronized block
2019      *      while (i.hasNext())
2020      *         foo(i.next());
2021      *  }
2022      * </pre>
2023      * Failure to follow this advice may result in non-deterministic behavior.
2024      *
2025      * <p>The returned collection does <i>not</i> pass the {@code hashCode}
2026      * and {@code equals} operations through to the backing collection, but
2027      * relies on {@code Object}'s equals and hashCode methods.  This is
2028      * necessary to preserve the contracts of these operations in the case
2029      * that the backing collection is a set or a list.<p>
2030      *
2031      * The returned collection will be serializable if the specified collection
2032      * is serializable.
2033      *


2103         }
2104         public void clear() {
2105             synchronized (mutex) {c.clear();}
2106         }
2107         public String toString() {
2108             synchronized (mutex) {return c.toString();}
2109         }
2110         // Override default methods in Collection
2111         @Override
2112         public void forEach(Consumer<? super E> consumer) {
2113             synchronized (mutex) {c.forEach(consumer);}
2114         }
2115         @Override
2116         public boolean removeIf(Predicate<? super E> filter) {
2117             synchronized (mutex) {return c.removeIf(filter);}
2118         }
2119         @Override
2120         public Spliterator<E> spliterator() {
2121             return c.spliterator(); // Must be manually synched by user!
2122         }








2123         private void writeObject(ObjectOutputStream s) throws IOException {
2124             synchronized (mutex) {s.defaultWriteObject();}
2125         }
2126     }
2127 
2128     /**
2129      * Returns a synchronized (thread-safe) set backed by the specified
2130      * set.  In order to guarantee serial access, it is critical that
2131      * <strong>all</strong> access to the backing set is accomplished
2132      * through the returned set.<p>
2133      *
2134      * It is imperative that the user manually synchronize on the returned
2135      * set when iterating over it:
2136      * <pre>
2137      *  Set s = Collections.synchronizedSet(new HashSet());
2138      *      ...
2139      *  synchronized (s) {
2140      *      Iterator i = s.iterator(); // Must be in the synchronized block
2141      *      while (i.hasNext())
2142      *          foo(i.next());


3155             return (Collection<E>) Arrays.asList(a);
3156         }
3157 
3158         public boolean addAll(Collection<? extends E> coll) {
3159             // Doing things this way insulates us from concurrent changes
3160             // in the contents of coll and provides all-or-nothing
3161             // semantics (which we wouldn't get if we type-checked each
3162             // element as we added it)
3163             return c.addAll(checkedCopyOf(coll));
3164         }
3165 
3166         // Override default methods in Collection
3167         @Override
3168         public void forEach(Consumer<? super E> action) {c.forEach(action);}
3169         @Override
3170         public boolean removeIf(Predicate<? super E> filter) {
3171             return c.removeIf(filter);
3172         }
3173         @Override
3174         public Spliterator<E> spliterator() {return c.spliterator();}




3175     }
3176 
3177     /**
3178      * Returns a dynamically typesafe view of the specified queue.
3179      * Any attempt to insert an element of the wrong type will result in
3180      * an immediate {@link ClassCastException}.  Assuming a queue contains
3181      * no incorrectly typed elements prior to the time a dynamically typesafe
3182      * view is generated, and that all subsequent access to the queue
3183      * takes place through the view, it is <i>guaranteed</i> that the
3184      * queue cannot contain an incorrectly typed element.
3185      *
3186      * <p>A discussion of the use of dynamically typesafe views may be
3187      * found in the documentation for the {@link #checkedCollection
3188      * checkedCollection} method.
3189      *
3190      * <p>The returned queue will be serializable if the specified queue
3191      * is serializable.
3192      *
3193      * <p>Since {@code null} is considered to be a value of any reference
3194      * type, the returned queue permits insertion of {@code null} elements


5080                 if (element != null)
5081                     Arrays.fill(a, 0, n, element);
5082             } else {
5083                 Arrays.fill(a, 0, n, element);
5084                 if (a.length > n)
5085                     a[n] = null;
5086             }
5087             return a;
5088         }
5089 
5090         public List<E> subList(int fromIndex, int toIndex) {
5091             if (fromIndex < 0)
5092                 throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
5093             if (toIndex > n)
5094                 throw new IndexOutOfBoundsException("toIndex = " + toIndex);
5095             if (fromIndex > toIndex)
5096                 throw new IllegalArgumentException("fromIndex(" + fromIndex +
5097                                                    ") > toIndex(" + toIndex + ")");
5098             return new CopiesList<>(toIndex - fromIndex, element);
5099         }
















5100     }
5101 
5102     /**
5103      * Returns a comparator that imposes the reverse of the <em>natural
5104      * ordering</em> on a collection of objects that implement the
5105      * {@code Comparable} interface.  (The natural ordering is the ordering
5106      * imposed by the objects' own {@code compareTo} method.)  This enables a
5107      * simple idiom for sorting (or maintaining) collections (or arrays) of
5108      * objects that implement the {@code Comparable} interface in
5109      * reverse-natural-order.  For example, suppose {@code a} is an array of
5110      * strings. Then: <pre>
5111      *          Arrays.sort(a, Collections.reverseOrder());
5112      * </pre> sorts the array in reverse-lexicographic (alphabetical) order.<p>
5113      *
5114      * The returned comparator is serializable.
5115      *
5116      * @param  <T> the class of the objects compared by the comparator
5117      * @return A comparator that imposes the reverse of the <i>natural
5118      *         ordering</i> on a collection of objects that implement
5119      *         the <tt>Comparable</tt> interface.


5487         public String toString()          { return s.toString(); }
5488         public int hashCode()             { return s.hashCode(); }
5489         public boolean equals(Object o)   { return o == this || s.equals(o); }
5490         public boolean containsAll(Collection<?> c) {return s.containsAll(c);}
5491         public boolean removeAll(Collection<?> c)   {return s.removeAll(c);}
5492         public boolean retainAll(Collection<?> c)   {return s.retainAll(c);}
5493         // addAll is the only inherited implementation
5494 
5495         // Override default methods in Collection
5496         @Override
5497         public void forEach(Consumer<? super E> action) {
5498             s.forEach(action);
5499         }
5500         @Override
5501         public boolean removeIf(Predicate<? super E> filter) {
5502             return s.removeIf(filter);
5503         }
5504 
5505         @Override
5506         public Spliterator<E> spliterator() {return s.spliterator();}




5507 
5508         private static final long serialVersionUID = 2454657854757543876L;
5509 
5510         private void readObject(java.io.ObjectInputStream stream)
5511             throws IOException, ClassNotFoundException
5512         {
5513             stream.defaultReadObject();
5514             s = m.keySet();
5515         }
5516     }
5517 
5518     /**
5519      * Returns a view of a {@link Deque} as a Last-in-first-out (Lifo)
5520      * {@link Queue}. Method <tt>add</tt> is mapped to <tt>push</tt>,
5521      * <tt>remove</tt> is mapped to <tt>pop</tt> and so on. This
5522      * view can be useful when you would like to use a method
5523      * requiring a <tt>Queue</tt> but you need Lifo ordering.
5524      *
5525      * <p>Each method invocation on the queue returned by this method
5526      * results in exactly one method invocation on the backing deque, with


5552         public E peek()                   { return q.peekFirst(); }
5553         public E element()                { return q.getFirst(); }
5554         public void clear()               {        q.clear(); }
5555         public int size()                 { return q.size(); }
5556         public boolean isEmpty()          { return q.isEmpty(); }
5557         public boolean contains(Object o) { return q.contains(o); }
5558         public boolean remove(Object o)   { return q.remove(o); }
5559         public Iterator<E> iterator()     { return q.iterator(); }
5560         public Object[] toArray()         { return q.toArray(); }
5561         public <T> T[] toArray(T[] a)     { return q.toArray(a); }
5562         public String toString()          { return q.toString(); }
5563         public boolean containsAll(Collection<?> c) {return q.containsAll(c);}
5564         public boolean removeAll(Collection<?> c)   {return q.removeAll(c);}
5565         public boolean retainAll(Collection<?> c)   {return q.retainAll(c);}
5566         // We use inherited addAll; forwarding addAll would be wrong
5567 
5568         // Override default methods in Collection
5569         @Override
5570         public void forEach(Consumer<? super E> action) {q.forEach(action);}
5571         @Override
5572         public Spliterator<E> spliterator() {return q.spliterator();}
5573         @Override
5574         public boolean removeIf(Predicate<? super E> filter) {
5575             return q.removeIf(filter);
5576         }






5577     }
5578 }


  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package java.util;
  27 import java.io.Serializable;
  28 import java.io.ObjectOutputStream;
  29 import java.io.IOException;

  30 import java.lang.reflect.Array;
  31 import java.util.function.BiConsumer;
  32 import java.util.function.BiFunction;
  33 import java.util.function.Consumer;
  34 import java.util.function.Function;
  35 import java.util.function.Predicate;
  36 import java.util.function.UnaryOperator;
  37 import java.util.stream.IntStream;
  38 import java.util.stream.Stream;
  39 import java.util.stream.StreamSupport;
  40 
  41 /**
  42  * This class consists exclusively of static methods that operate on or return
  43  * collections.  It contains polymorphic algorithms that operate on
  44  * collections, "wrappers", which return a new collection backed by a
  45  * specified collection, and a few other odds and ends.
  46  *
  47  * <p>The methods of this class all throw a <tt>NullPointerException</tt>
  48  * if the collections or class objects provided to them are null.
  49  *
  50  * <p>The documentation for the polymorphic algorithms contained in this class
  51  * generally includes a brief description of the <i>implementation</i>.  Such
  52  * descriptions should be regarded as <i>implementation notes</i>, rather than
  53  * parts of the <i>specification</i>.  Implementors should feel free to
  54  * substitute other algorithms, so long as the specification itself is adhered
  55  * to.  (For example, the algorithm used by <tt>sort</tt> does not have to be
  56  * a mergesort, but it does have to be <i>stable</i>.)
  57  *


1131             throw new UnsupportedOperationException();
1132         }
1133         public void clear() {
1134             throw new UnsupportedOperationException();
1135         }
1136 
1137         // Override default methods in Collection
1138         @Override
1139         public void forEach(Consumer<? super E> action) {
1140             c.forEach(action);
1141         }
1142         @Override
1143         public boolean removeIf(Predicate<? super E> filter) {
1144             throw new UnsupportedOperationException();
1145         }
1146         @SuppressWarnings("unchecked")
1147         @Override
1148         public Spliterator<E> spliterator() {
1149             return (Spliterator<E>)c.spliterator();
1150         }
1151         @Override
1152         public Stream<E> stream() {
1153             return (Stream<E>)c.stream();
1154         }
1155         @Override
1156         public Stream<E> parallelStream() {
1157             return (Stream<E>)c.parallelStream();
1158         }
1159     }
1160 
1161     /**
1162      * Returns an unmodifiable view of the specified set.  This method allows
1163      * modules to provide users with "read-only" access to internal sets.
1164      * Query operations on the returned set "read through" to the specified
1165      * set, and attempts to modify the returned set, whether direct or via its
1166      * iterator, result in an <tt>UnsupportedOperationException</tt>.<p>
1167      *
1168      * The returned set will be serializable if the specified set
1169      * is serializable.
1170      *
1171      * @param  <T> the class of the objects in the set
1172      * @param  s the set for which an unmodifiable view is to be returned.
1173      * @return an unmodifiable view of the specified set.
1174      */
1175     public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
1176         return new UnmodifiableSet<>(s);
1177     }
1178 


1999         public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) {
2000             return unmodifiableNavigableMap(
2001                 nm.subMap(fromKey, fromInclusive, toKey, toInclusive));
2002         }
2003 
2004         public NavigableMap<K, V> headMap(K toKey, boolean inclusive)
2005              { return unmodifiableNavigableMap(nm.headMap(toKey, inclusive)); }
2006         public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive)
2007            { return unmodifiableNavigableMap(nm.tailMap(fromKey, inclusive)); }
2008     }
2009 
2010     // Synch Wrappers
2011 
2012     /**
2013      * Returns a synchronized (thread-safe) collection backed by the specified
2014      * collection.  In order to guarantee serial access, it is critical that
2015      * <strong>all</strong> access to the backing collection is accomplished
2016      * through the returned collection.<p>
2017      *
2018      * It is imperative that the user manually synchronize on the returned
2019      * collection when traversing it via {@link Iterator}, {@link Spliterator}
2020      * or {@link Stream}:
2021      * <pre>
2022      *  Collection c = Collections.synchronizedCollection(myCollection);
2023      *     ...
2024      *  synchronized (c) {
2025      *      Iterator i = c.iterator(); // Must be in the synchronized block
2026      *      while (i.hasNext())
2027      *         foo(i.next());
2028      *  }
2029      * </pre>
2030      * Failure to follow this advice may result in non-deterministic behavior.
2031      *
2032      * <p>The returned collection does <i>not</i> pass the {@code hashCode}
2033      * and {@code equals} operations through to the backing collection, but
2034      * relies on {@code Object}'s equals and hashCode methods.  This is
2035      * necessary to preserve the contracts of these operations in the case
2036      * that the backing collection is a set or a list.<p>
2037      *
2038      * The returned collection will be serializable if the specified collection
2039      * is serializable.
2040      *


2110         }
2111         public void clear() {
2112             synchronized (mutex) {c.clear();}
2113         }
2114         public String toString() {
2115             synchronized (mutex) {return c.toString();}
2116         }
2117         // Override default methods in Collection
2118         @Override
2119         public void forEach(Consumer<? super E> consumer) {
2120             synchronized (mutex) {c.forEach(consumer);}
2121         }
2122         @Override
2123         public boolean removeIf(Predicate<? super E> filter) {
2124             synchronized (mutex) {return c.removeIf(filter);}
2125         }
2126         @Override
2127         public Spliterator<E> spliterator() {
2128             return c.spliterator(); // Must be manually synched by user!
2129         }
2130         @Override
2131         public Stream<E> stream() {
2132             return c.stream(); // Must be manually synched by user!
2133         }
2134         @Override
2135         public Stream<E> parallelStream() {
2136             return c.parallelStream(); // Must be manually synched by user!
2137         }
2138         private void writeObject(ObjectOutputStream s) throws IOException {
2139             synchronized (mutex) {s.defaultWriteObject();}
2140         }
2141     }
2142 
2143     /**
2144      * Returns a synchronized (thread-safe) set backed by the specified
2145      * set.  In order to guarantee serial access, it is critical that
2146      * <strong>all</strong> access to the backing set is accomplished
2147      * through the returned set.<p>
2148      *
2149      * It is imperative that the user manually synchronize on the returned
2150      * set when iterating over it:
2151      * <pre>
2152      *  Set s = Collections.synchronizedSet(new HashSet());
2153      *      ...
2154      *  synchronized (s) {
2155      *      Iterator i = s.iterator(); // Must be in the synchronized block
2156      *      while (i.hasNext())
2157      *          foo(i.next());


3170             return (Collection<E>) Arrays.asList(a);
3171         }
3172 
3173         public boolean addAll(Collection<? extends E> coll) {
3174             // Doing things this way insulates us from concurrent changes
3175             // in the contents of coll and provides all-or-nothing
3176             // semantics (which we wouldn't get if we type-checked each
3177             // element as we added it)
3178             return c.addAll(checkedCopyOf(coll));
3179         }
3180 
3181         // Override default methods in Collection
3182         @Override
3183         public void forEach(Consumer<? super E> action) {c.forEach(action);}
3184         @Override
3185         public boolean removeIf(Predicate<? super E> filter) {
3186             return c.removeIf(filter);
3187         }
3188         @Override
3189         public Spliterator<E> spliterator() {return c.spliterator();}
3190         @Override
3191         public Stream<E> stream()           {return c.stream();}
3192         @Override
3193         public Stream<E> parallelStream()   {return c.parallelStream();}
3194     }
3195 
3196     /**
3197      * Returns a dynamically typesafe view of the specified queue.
3198      * Any attempt to insert an element of the wrong type will result in
3199      * an immediate {@link ClassCastException}.  Assuming a queue contains
3200      * no incorrectly typed elements prior to the time a dynamically typesafe
3201      * view is generated, and that all subsequent access to the queue
3202      * takes place through the view, it is <i>guaranteed</i> that the
3203      * queue cannot contain an incorrectly typed element.
3204      *
3205      * <p>A discussion of the use of dynamically typesafe views may be
3206      * found in the documentation for the {@link #checkedCollection
3207      * checkedCollection} method.
3208      *
3209      * <p>The returned queue will be serializable if the specified queue
3210      * is serializable.
3211      *
3212      * <p>Since {@code null} is considered to be a value of any reference
3213      * type, the returned queue permits insertion of {@code null} elements


5099                 if (element != null)
5100                     Arrays.fill(a, 0, n, element);
5101             } else {
5102                 Arrays.fill(a, 0, n, element);
5103                 if (a.length > n)
5104                     a[n] = null;
5105             }
5106             return a;
5107         }
5108 
5109         public List<E> subList(int fromIndex, int toIndex) {
5110             if (fromIndex < 0)
5111                 throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
5112             if (toIndex > n)
5113                 throw new IndexOutOfBoundsException("toIndex = " + toIndex);
5114             if (fromIndex > toIndex)
5115                 throw new IllegalArgumentException("fromIndex(" + fromIndex +
5116                                                    ") > toIndex(" + toIndex + ")");
5117             return new CopiesList<>(toIndex - fromIndex, element);
5118         }
5119 
5120         // Override default methods in Collection
5121         @Override
5122         public Stream<E> stream() {
5123             return IntStream.range(0, n).mapToObj(i -> element);
5124         }
5125 
5126         @Override
5127         public Stream<E> parallelStream() {
5128             return IntStream.range(0, n).parallel().mapToObj(i -> element);
5129         }
5130 
5131         @Override
5132         public Spliterator<E> spliterator() {
5133             return stream().spliterator();
5134         }
5135     }
5136 
5137     /**
5138      * Returns a comparator that imposes the reverse of the <em>natural
5139      * ordering</em> on a collection of objects that implement the
5140      * {@code Comparable} interface.  (The natural ordering is the ordering
5141      * imposed by the objects' own {@code compareTo} method.)  This enables a
5142      * simple idiom for sorting (or maintaining) collections (or arrays) of
5143      * objects that implement the {@code Comparable} interface in
5144      * reverse-natural-order.  For example, suppose {@code a} is an array of
5145      * strings. Then: <pre>
5146      *          Arrays.sort(a, Collections.reverseOrder());
5147      * </pre> sorts the array in reverse-lexicographic (alphabetical) order.<p>
5148      *
5149      * The returned comparator is serializable.
5150      *
5151      * @param  <T> the class of the objects compared by the comparator
5152      * @return A comparator that imposes the reverse of the <i>natural
5153      *         ordering</i> on a collection of objects that implement
5154      *         the <tt>Comparable</tt> interface.


5522         public String toString()          { return s.toString(); }
5523         public int hashCode()             { return s.hashCode(); }
5524         public boolean equals(Object o)   { return o == this || s.equals(o); }
5525         public boolean containsAll(Collection<?> c) {return s.containsAll(c);}
5526         public boolean removeAll(Collection<?> c)   {return s.removeAll(c);}
5527         public boolean retainAll(Collection<?> c)   {return s.retainAll(c);}
5528         // addAll is the only inherited implementation
5529 
5530         // Override default methods in Collection
5531         @Override
5532         public void forEach(Consumer<? super E> action) {
5533             s.forEach(action);
5534         }
5535         @Override
5536         public boolean removeIf(Predicate<? super E> filter) {
5537             return s.removeIf(filter);
5538         }
5539 
5540         @Override
5541         public Spliterator<E> spliterator() {return s.spliterator();}
5542         @Override
5543         public Stream<E> stream()           {return s.stream();}
5544         @Override
5545         public Stream<E> parallelStream()   {return s.parallelStream();}
5546 
5547         private static final long serialVersionUID = 2454657854757543876L;
5548 
5549         private void readObject(java.io.ObjectInputStream stream)
5550             throws IOException, ClassNotFoundException
5551         {
5552             stream.defaultReadObject();
5553             s = m.keySet();
5554         }
5555     }
5556 
5557     /**
5558      * Returns a view of a {@link Deque} as a Last-in-first-out (Lifo)
5559      * {@link Queue}. Method <tt>add</tt> is mapped to <tt>push</tt>,
5560      * <tt>remove</tt> is mapped to <tt>pop</tt> and so on. This
5561      * view can be useful when you would like to use a method
5562      * requiring a <tt>Queue</tt> but you need Lifo ordering.
5563      *
5564      * <p>Each method invocation on the queue returned by this method
5565      * results in exactly one method invocation on the backing deque, with


5591         public E peek()                   { return q.peekFirst(); }
5592         public E element()                { return q.getFirst(); }
5593         public void clear()               {        q.clear(); }
5594         public int size()                 { return q.size(); }
5595         public boolean isEmpty()          { return q.isEmpty(); }
5596         public boolean contains(Object o) { return q.contains(o); }
5597         public boolean remove(Object o)   { return q.remove(o); }
5598         public Iterator<E> iterator()     { return q.iterator(); }
5599         public Object[] toArray()         { return q.toArray(); }
5600         public <T> T[] toArray(T[] a)     { return q.toArray(a); }
5601         public String toString()          { return q.toString(); }
5602         public boolean containsAll(Collection<?> c) {return q.containsAll(c);}
5603         public boolean removeAll(Collection<?> c)   {return q.removeAll(c);}
5604         public boolean retainAll(Collection<?> c)   {return q.retainAll(c);}
5605         // We use inherited addAll; forwarding addAll would be wrong
5606 
5607         // Override default methods in Collection
5608         @Override
5609         public void forEach(Consumer<? super E> action) {q.forEach(action);}
5610         @Override


5611         public boolean removeIf(Predicate<? super E> filter) {
5612             return q.removeIf(filter);
5613         }
5614         @Override
5615         public Spliterator<E> spliterator() {return q.spliterator();}
5616         @Override
5617         public Stream<E> stream()           {return q.stream();}
5618         @Override
5619         public Stream<E> parallelStream()   {return q.parallelStream();}
5620     }
5621 }