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


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


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


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


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


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


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


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