< prev index next >

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

Print this page
rev 17661 : 8177290: add copy factory methods for unmodifiable List, Set, Map
8184690: add Collectors for collecting into unmodifiable List, Set, and Map
Reviewed-by: XXX


 972                 for (int i=0; i<targetSize; i++) {
 973                     if (!eq(ti.next(), si.next())) {
 974                         if (candidate != 0) {
 975                             // Back up source iterator to next candidate
 976                             for (int j=0; j<=i+1; j++)
 977                                 si.previous();
 978                         }
 979                         continue nextCand;
 980                     }
 981                 }
 982                 return candidate;
 983             }
 984         }
 985         return -1;  // No candidate matched the target
 986     }
 987 
 988 
 989     // Unmodifiable Wrappers
 990 
 991     /**
 992      * Returns an unmodifiable view of the specified collection.  This method
 993      * allows modules to provide users with "read-only" access to internal
 994      * collections.  Query operations on the returned collection "read through"
 995      * to the specified collection, and attempts to modify the returned
 996      * collection, whether direct or via its iterator, result in an
 997      * {@code UnsupportedOperationException}.<p>
 998      *
 999      * The returned collection does <i>not</i> pass the hashCode and equals
1000      * operations through to the backing collection, but relies on
1001      * {@code Object}'s {@code equals} and {@code hashCode} methods.  This
1002      * is necessary to preserve the contracts of these operations in the case
1003      * that the backing collection is a set or a list.<p>
1004      *
1005      * The returned collection will be serializable if the specified collection
1006      * is serializable.
1007      *
1008      * @param  <T> the class of the objects in the collection
1009      * @param  c the collection for which an unmodifiable view is to be
1010      *         returned.
1011      * @return an unmodifiable view of the specified collection.
1012      */
1013     public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
1014         return new UnmodifiableCollection<>(c);


1085             throw new UnsupportedOperationException();
1086         }
1087         @SuppressWarnings("unchecked")
1088         @Override
1089         public Spliterator<E> spliterator() {
1090             return (Spliterator<E>)c.spliterator();
1091         }
1092         @SuppressWarnings("unchecked")
1093         @Override
1094         public Stream<E> stream() {
1095             return (Stream<E>)c.stream();
1096         }
1097         @SuppressWarnings("unchecked")
1098         @Override
1099         public Stream<E> parallelStream() {
1100             return (Stream<E>)c.parallelStream();
1101         }
1102     }
1103 
1104     /**
1105      * Returns an unmodifiable view of the specified set.  This method allows
1106      * modules to provide users with "read-only" access to internal sets.
1107      * Query operations on the returned set "read through" to the specified
1108      * set, and attempts to modify the returned set, whether direct or via its
1109      * iterator, result in an {@code UnsupportedOperationException}.<p>
1110      *
1111      * The returned set will be serializable if the specified set
1112      * is serializable.
1113      *
1114      * @param  <T> the class of the objects in the set
1115      * @param  s the set for which an unmodifiable view is to be returned.
1116      * @return an unmodifiable view of the specified set.
1117      */
1118     public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
1119         return new UnmodifiableSet<>(s);
1120     }
1121 
1122     /**
1123      * @serial include
1124      */
1125     static class UnmodifiableSet<E> extends UnmodifiableCollection<E>
1126                                  implements Set<E>, Serializable {
1127         private static final long serialVersionUID = -9215047833775013803L;
1128 
1129         UnmodifiableSet(Set<? extends E> s)     {super(s);}
1130         public boolean equals(Object o) {return o == this || c.equals(o);}
1131         public int hashCode()           {return c.hashCode();}
1132     }
1133 
1134     /**
1135      * Returns an unmodifiable view of the specified sorted set.  This method
1136      * allows modules to provide users with "read-only" access to internal
1137      * sorted sets.  Query operations on the returned sorted set "read
1138      * through" to the specified sorted set.  Attempts to modify the returned
1139      * sorted set, whether direct, via its iterator, or via its
1140      * {@code subSet}, {@code headSet}, or {@code tailSet} views, result in
1141      * an {@code UnsupportedOperationException}.<p>
1142      *
1143      * The returned sorted set will be serializable if the specified sorted set
1144      * is serializable.
1145      *
1146      * @param  <T> the class of the objects in the set
1147      * @param s the sorted set for which an unmodifiable view is to be
1148      *        returned.
1149      * @return an unmodifiable view of the specified sorted set.
1150      */
1151     public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) {
1152         return new UnmodifiableSortedSet<>(s);
1153     }
1154 
1155     /**
1156      * @serial include
1157      */


1163 
1164         UnmodifiableSortedSet(SortedSet<E> s) {super(s); ss = s;}
1165 
1166         public Comparator<? super E> comparator() {return ss.comparator();}
1167 
1168         public SortedSet<E> subSet(E fromElement, E toElement) {
1169             return new UnmodifiableSortedSet<>(ss.subSet(fromElement,toElement));
1170         }
1171         public SortedSet<E> headSet(E toElement) {
1172             return new UnmodifiableSortedSet<>(ss.headSet(toElement));
1173         }
1174         public SortedSet<E> tailSet(E fromElement) {
1175             return new UnmodifiableSortedSet<>(ss.tailSet(fromElement));
1176         }
1177 
1178         public E first()                   {return ss.first();}
1179         public E last()                    {return ss.last();}
1180     }
1181 
1182     /**
1183      * Returns an unmodifiable view of the specified navigable set.  This method
1184      * allows modules to provide users with "read-only" access to internal
1185      * navigable sets.  Query operations on the returned navigable set "read
1186      * through" to the specified navigable set.  Attempts to modify the returned
1187      * navigable set, whether direct, via its iterator, or via its
1188      * {@code subSet}, {@code headSet}, or {@code tailSet} views, result in
1189      * an {@code UnsupportedOperationException}.<p>
1190      *
1191      * The returned navigable set will be serializable if the specified
1192      * navigable set is serializable.
1193      *
1194      * @param  <T> the class of the objects in the set
1195      * @param s the navigable set for which an unmodifiable view is to be
1196      *        returned
1197      * @return an unmodifiable view of the specified navigable set
1198      * @since 1.8
1199      */
1200     public static <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s) {
1201         return new UnmodifiableNavigableSet<>(s);
1202     }
1203 
1204     /**
1205      * Wraps a navigable set and disables all of the mutative operations.


1252         public Iterator<E> descendingIterator()
1253                                          { return descendingSet().iterator(); }
1254 
1255         public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
1256             return new UnmodifiableNavigableSet<>(
1257                 ns.subSet(fromElement, fromInclusive, toElement, toInclusive));
1258         }
1259 
1260         public NavigableSet<E> headSet(E toElement, boolean inclusive) {
1261             return new UnmodifiableNavigableSet<>(
1262                 ns.headSet(toElement, inclusive));
1263         }
1264 
1265         public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
1266             return new UnmodifiableNavigableSet<>(
1267                 ns.tailSet(fromElement, inclusive));
1268         }
1269     }
1270 
1271     /**
1272      * Returns an unmodifiable view of the specified list.  This method allows
1273      * modules to provide users with "read-only" access to internal
1274      * lists.  Query operations on the returned list "read through" to the
1275      * specified list, and attempts to modify the returned list, whether
1276      * direct or via its iterator, result in an
1277      * {@code UnsupportedOperationException}.<p>
1278      *
1279      * The returned list will be serializable if the specified list
1280      * is serializable. Similarly, the returned list will implement
1281      * {@link RandomAccess} if the specified list does.
1282      *
1283      * @param  <T> the class of the objects in the list
1284      * @param  list the list for which an unmodifiable view is to be returned.
1285      * @return an unmodifiable view of the specified list.
1286      */
1287     public static <T> List<T> unmodifiableList(List<? extends T> list) {
1288         return (list instanceof RandomAccess ?
1289                 new UnmodifiableRandomAccessList<>(list) :
1290                 new UnmodifiableList<>(list));
1291     }
1292 
1293     /**
1294      * @serial include


1398 
1399         public List<E> subList(int fromIndex, int toIndex) {
1400             return new UnmodifiableRandomAccessList<>(
1401                 list.subList(fromIndex, toIndex));
1402         }
1403 
1404         private static final long serialVersionUID = -2542308836966382001L;
1405 
1406         /**
1407          * Allows instances to be deserialized in pre-1.4 JREs (which do
1408          * not have UnmodifiableRandomAccessList).  UnmodifiableList has
1409          * a readResolve method that inverts this transformation upon
1410          * deserialization.
1411          */
1412         private Object writeReplace() {
1413             return new UnmodifiableList<>(list);
1414         }
1415     }
1416 
1417     /**
1418      * Returns an unmodifiable view of the specified map.  This method
1419      * allows modules to provide users with "read-only" access to internal
1420      * maps.  Query operations on the returned map "read through"
1421      * to the specified map, and attempts to modify the returned
1422      * map, whether direct or via its collection views, result in an
1423      * {@code UnsupportedOperationException}.<p>
1424      *
1425      * The returned map will be serializable if the specified map
1426      * is serializable.
1427      *
1428      * @param <K> the class of the map keys
1429      * @param <V> the class of the map values
1430      * @param  m the map for which an unmodifiable view is to be returned.
1431      * @return an unmodifiable view of the specified map.
1432      */
1433     public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) {
1434         return new UnmodifiableMap<>(m);
1435     }
1436 
1437     /**
1438      * @serial include
1439      */
1440     private static class UnmodifiableMap<K,V> implements Map<K,V>, Serializable {


1748                 public V getValue()      {return e.getValue();}
1749                 public V setValue(V value) {
1750                     throw new UnsupportedOperationException();
1751                 }
1752                 public int hashCode()    {return e.hashCode();}
1753                 public boolean equals(Object o) {
1754                     if (this == o)
1755                         return true;
1756                     if (!(o instanceof Map.Entry))
1757                         return false;
1758                     Map.Entry<?,?> t = (Map.Entry<?,?>)o;
1759                     return eq(e.getKey(),   t.getKey()) &&
1760                            eq(e.getValue(), t.getValue());
1761                 }
1762                 public String toString() {return e.toString();}
1763             }
1764         }
1765     }
1766 
1767     /**
1768      * Returns an unmodifiable view of the specified sorted map.  This method
1769      * allows modules to provide users with "read-only" access to internal
1770      * sorted maps.  Query operations on the returned sorted map "read through"
1771      * to the specified sorted map.  Attempts to modify the returned
1772      * sorted map, whether direct, via its collection views, or via its
1773      * {@code subMap}, {@code headMap}, or {@code tailMap} views, result in
1774      * an {@code UnsupportedOperationException}.<p>
1775      *
1776      * The returned sorted map will be serializable if the specified sorted map
1777      * is serializable.
1778      *
1779      * @param <K> the class of the map keys
1780      * @param <V> the class of the map values
1781      * @param m the sorted map for which an unmodifiable view is to be
1782      *        returned.
1783      * @return an unmodifiable view of the specified sorted map.
1784      */
1785     public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) {
1786         return new UnmodifiableSortedMap<>(m);
1787     }
1788 
1789     /**
1790      * @serial include


1792     static class UnmodifiableSortedMap<K,V>
1793           extends UnmodifiableMap<K,V>
1794           implements SortedMap<K,V>, Serializable {
1795         private static final long serialVersionUID = -8806743815996713206L;
1796 
1797         private final SortedMap<K, ? extends V> sm;
1798 
1799         UnmodifiableSortedMap(SortedMap<K, ? extends V> m) {super(m); sm = m; }
1800         public Comparator<? super K> comparator()   { return sm.comparator(); }
1801         public SortedMap<K,V> subMap(K fromKey, K toKey)
1802              { return new UnmodifiableSortedMap<>(sm.subMap(fromKey, toKey)); }
1803         public SortedMap<K,V> headMap(K toKey)
1804                      { return new UnmodifiableSortedMap<>(sm.headMap(toKey)); }
1805         public SortedMap<K,V> tailMap(K fromKey)
1806                    { return new UnmodifiableSortedMap<>(sm.tailMap(fromKey)); }
1807         public K firstKey()                           { return sm.firstKey(); }
1808         public K lastKey()                             { return sm.lastKey(); }
1809     }
1810 
1811     /**
1812      * Returns an unmodifiable view of the specified navigable map.  This method
1813      * allows modules to provide users with "read-only" access to internal
1814      * navigable maps.  Query operations on the returned navigable map "read
1815      * through" to the specified navigable map.  Attempts to modify the returned
1816      * navigable map, whether direct, via its collection views, or via its
1817      * {@code subMap}, {@code headMap}, or {@code tailMap} views, result in
1818      * an {@code UnsupportedOperationException}.<p>
1819      *
1820      * The returned navigable map will be serializable if the specified
1821      * navigable map is serializable.
1822      *
1823      * @param <K> the class of the map keys
1824      * @param <V> the class of the map values
1825      * @param m the navigable map for which an unmodifiable view is to be
1826      *        returned
1827      * @return an unmodifiable view of the specified navigable map
1828      * @since 1.8
1829      */
1830     public static <K,V> NavigableMap<K,V> unmodifiableNavigableMap(NavigableMap<K, ? extends V> m) {
1831         return new UnmodifiableNavigableMap<>(m);
1832     }
1833 
1834     /**




 972                 for (int i=0; i<targetSize; i++) {
 973                     if (!eq(ti.next(), si.next())) {
 974                         if (candidate != 0) {
 975                             // Back up source iterator to next candidate
 976                             for (int j=0; j<=i+1; j++)
 977                                 si.previous();
 978                         }
 979                         continue nextCand;
 980                     }
 981                 }
 982                 return candidate;
 983             }
 984         }
 985         return -1;  // No candidate matched the target
 986     }
 987 
 988 
 989     // Unmodifiable Wrappers
 990 
 991     /**
 992      * Returns an <a href="Collection.html#unmodview">unmodifiable view</a> of the
 993      * specified collection. Query operations on the returned collection "read through"

 994      * to the specified collection, and attempts to modify the returned
 995      * collection, whether direct or via its iterator, result in an
 996      * {@code UnsupportedOperationException}.<p>
 997      *
 998      * The returned collection does <i>not</i> pass the hashCode and equals
 999      * operations through to the backing collection, but relies on
1000      * {@code Object}'s {@code equals} and {@code hashCode} methods.  This
1001      * is necessary to preserve the contracts of these operations in the case
1002      * that the backing collection is a set or a list.<p>
1003      *
1004      * The returned collection will be serializable if the specified collection
1005      * is serializable.
1006      *
1007      * @param  <T> the class of the objects in the collection
1008      * @param  c the collection for which an unmodifiable view is to be
1009      *         returned.
1010      * @return an unmodifiable view of the specified collection.
1011      */
1012     public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
1013         return new UnmodifiableCollection<>(c);


1084             throw new UnsupportedOperationException();
1085         }
1086         @SuppressWarnings("unchecked")
1087         @Override
1088         public Spliterator<E> spliterator() {
1089             return (Spliterator<E>)c.spliterator();
1090         }
1091         @SuppressWarnings("unchecked")
1092         @Override
1093         public Stream<E> stream() {
1094             return (Stream<E>)c.stream();
1095         }
1096         @SuppressWarnings("unchecked")
1097         @Override
1098         public Stream<E> parallelStream() {
1099             return (Stream<E>)c.parallelStream();
1100         }
1101     }
1102 
1103     /**
1104      * Returns an <a href="Collection.html#unmodview">unmodifiable view</a> of the
1105      * specified set. Query operations on the returned set "read through" to the specified

1106      * set, and attempts to modify the returned set, whether direct or via its
1107      * iterator, result in an {@code UnsupportedOperationException}.<p>
1108      *
1109      * The returned set will be serializable if the specified set
1110      * is serializable.
1111      *
1112      * @param  <T> the class of the objects in the set
1113      * @param  s the set for which an unmodifiable view is to be returned.
1114      * @return an unmodifiable view of the specified set.
1115      */
1116     public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
1117         return new UnmodifiableSet<>(s);
1118     }
1119 
1120     /**
1121      * @serial include
1122      */
1123     static class UnmodifiableSet<E> extends UnmodifiableCollection<E>
1124                                  implements Set<E>, Serializable {
1125         private static final long serialVersionUID = -9215047833775013803L;
1126 
1127         UnmodifiableSet(Set<? extends E> s)     {super(s);}
1128         public boolean equals(Object o) {return o == this || c.equals(o);}
1129         public int hashCode()           {return c.hashCode();}
1130     }
1131 
1132     /**
1133      * Returns an <a href="Collection.html#unmodview">unmodifiable view</a> of the
1134      * specified sorted set. Query operations on the returned sorted set "read

1135      * through" to the specified sorted set.  Attempts to modify the returned
1136      * sorted set, whether direct, via its iterator, or via its
1137      * {@code subSet}, {@code headSet}, or {@code tailSet} views, result in
1138      * an {@code UnsupportedOperationException}.<p>
1139      *
1140      * The returned sorted set will be serializable if the specified sorted set
1141      * is serializable.
1142      *
1143      * @param  <T> the class of the objects in the set
1144      * @param s the sorted set for which an unmodifiable view is to be
1145      *        returned.
1146      * @return an unmodifiable view of the specified sorted set.
1147      */
1148     public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) {
1149         return new UnmodifiableSortedSet<>(s);
1150     }
1151 
1152     /**
1153      * @serial include
1154      */


1160 
1161         UnmodifiableSortedSet(SortedSet<E> s) {super(s); ss = s;}
1162 
1163         public Comparator<? super E> comparator() {return ss.comparator();}
1164 
1165         public SortedSet<E> subSet(E fromElement, E toElement) {
1166             return new UnmodifiableSortedSet<>(ss.subSet(fromElement,toElement));
1167         }
1168         public SortedSet<E> headSet(E toElement) {
1169             return new UnmodifiableSortedSet<>(ss.headSet(toElement));
1170         }
1171         public SortedSet<E> tailSet(E fromElement) {
1172             return new UnmodifiableSortedSet<>(ss.tailSet(fromElement));
1173         }
1174 
1175         public E first()                   {return ss.first();}
1176         public E last()                    {return ss.last();}
1177     }
1178 
1179     /**
1180      * Returns an <a href="Collection.html#unmodview">unmodifiable view</a> of the
1181      * specified navigable set. Query operations on the returned navigable set "read

1182      * through" to the specified navigable set.  Attempts to modify the returned
1183      * navigable set, whether direct, via its iterator, or via its
1184      * {@code subSet}, {@code headSet}, or {@code tailSet} views, result in
1185      * an {@code UnsupportedOperationException}.<p>
1186      *
1187      * The returned navigable set will be serializable if the specified
1188      * navigable set is serializable.
1189      *
1190      * @param  <T> the class of the objects in the set
1191      * @param s the navigable set for which an unmodifiable view is to be
1192      *        returned
1193      * @return an unmodifiable view of the specified navigable set
1194      * @since 1.8
1195      */
1196     public static <T> NavigableSet<T> unmodifiableNavigableSet(NavigableSet<T> s) {
1197         return new UnmodifiableNavigableSet<>(s);
1198     }
1199 
1200     /**
1201      * Wraps a navigable set and disables all of the mutative operations.


1248         public Iterator<E> descendingIterator()
1249                                          { return descendingSet().iterator(); }
1250 
1251         public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
1252             return new UnmodifiableNavigableSet<>(
1253                 ns.subSet(fromElement, fromInclusive, toElement, toInclusive));
1254         }
1255 
1256         public NavigableSet<E> headSet(E toElement, boolean inclusive) {
1257             return new UnmodifiableNavigableSet<>(
1258                 ns.headSet(toElement, inclusive));
1259         }
1260 
1261         public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
1262             return new UnmodifiableNavigableSet<>(
1263                 ns.tailSet(fromElement, inclusive));
1264         }
1265     }
1266 
1267     /**
1268      * Returns an <a href="Collection.html#unmodview">unmodifiable view</a> of the
1269      * specified list. Query operations on the returned list "read through" to the

1270      * specified list, and attempts to modify the returned list, whether
1271      * direct or via its iterator, result in an
1272      * {@code UnsupportedOperationException}.<p>
1273      *
1274      * The returned list will be serializable if the specified list
1275      * is serializable. Similarly, the returned list will implement
1276      * {@link RandomAccess} if the specified list does.
1277      *
1278      * @param  <T> the class of the objects in the list
1279      * @param  list the list for which an unmodifiable view is to be returned.
1280      * @return an unmodifiable view of the specified list.
1281      */
1282     public static <T> List<T> unmodifiableList(List<? extends T> list) {
1283         return (list instanceof RandomAccess ?
1284                 new UnmodifiableRandomAccessList<>(list) :
1285                 new UnmodifiableList<>(list));
1286     }
1287 
1288     /**
1289      * @serial include


1393 
1394         public List<E> subList(int fromIndex, int toIndex) {
1395             return new UnmodifiableRandomAccessList<>(
1396                 list.subList(fromIndex, toIndex));
1397         }
1398 
1399         private static final long serialVersionUID = -2542308836966382001L;
1400 
1401         /**
1402          * Allows instances to be deserialized in pre-1.4 JREs (which do
1403          * not have UnmodifiableRandomAccessList).  UnmodifiableList has
1404          * a readResolve method that inverts this transformation upon
1405          * deserialization.
1406          */
1407         private Object writeReplace() {
1408             return new UnmodifiableList<>(list);
1409         }
1410     }
1411 
1412     /**
1413      * Returns an <a href="Collection.html#unmodview">unmodifiable view</a> of the
1414      * specified map. Query operations on the returned map "read through"

1415      * to the specified map, and attempts to modify the returned
1416      * map, whether direct or via its collection views, result in an
1417      * {@code UnsupportedOperationException}.<p>
1418      *
1419      * The returned map will be serializable if the specified map
1420      * is serializable.
1421      *
1422      * @param <K> the class of the map keys
1423      * @param <V> the class of the map values
1424      * @param  m the map for which an unmodifiable view is to be returned.
1425      * @return an unmodifiable view of the specified map.
1426      */
1427     public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) {
1428         return new UnmodifiableMap<>(m);
1429     }
1430 
1431     /**
1432      * @serial include
1433      */
1434     private static class UnmodifiableMap<K,V> implements Map<K,V>, Serializable {


1742                 public V getValue()      {return e.getValue();}
1743                 public V setValue(V value) {
1744                     throw new UnsupportedOperationException();
1745                 }
1746                 public int hashCode()    {return e.hashCode();}
1747                 public boolean equals(Object o) {
1748                     if (this == o)
1749                         return true;
1750                     if (!(o instanceof Map.Entry))
1751                         return false;
1752                     Map.Entry<?,?> t = (Map.Entry<?,?>)o;
1753                     return eq(e.getKey(),   t.getKey()) &&
1754                            eq(e.getValue(), t.getValue());
1755                 }
1756                 public String toString() {return e.toString();}
1757             }
1758         }
1759     }
1760 
1761     /**
1762      * Returns an <a href="Collection.html#unmodview">unmodifiable view</a> of the
1763      * specified sorted map. Query operations on the returned sorted map "read through"

1764      * to the specified sorted map.  Attempts to modify the returned
1765      * sorted map, whether direct, via its collection views, or via its
1766      * {@code subMap}, {@code headMap}, or {@code tailMap} views, result in
1767      * an {@code UnsupportedOperationException}.<p>
1768      *
1769      * The returned sorted map will be serializable if the specified sorted map
1770      * is serializable.
1771      *
1772      * @param <K> the class of the map keys
1773      * @param <V> the class of the map values
1774      * @param m the sorted map for which an unmodifiable view is to be
1775      *        returned.
1776      * @return an unmodifiable view of the specified sorted map.
1777      */
1778     public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) {
1779         return new UnmodifiableSortedMap<>(m);
1780     }
1781 
1782     /**
1783      * @serial include


1785     static class UnmodifiableSortedMap<K,V>
1786           extends UnmodifiableMap<K,V>
1787           implements SortedMap<K,V>, Serializable {
1788         private static final long serialVersionUID = -8806743815996713206L;
1789 
1790         private final SortedMap<K, ? extends V> sm;
1791 
1792         UnmodifiableSortedMap(SortedMap<K, ? extends V> m) {super(m); sm = m; }
1793         public Comparator<? super K> comparator()   { return sm.comparator(); }
1794         public SortedMap<K,V> subMap(K fromKey, K toKey)
1795              { return new UnmodifiableSortedMap<>(sm.subMap(fromKey, toKey)); }
1796         public SortedMap<K,V> headMap(K toKey)
1797                      { return new UnmodifiableSortedMap<>(sm.headMap(toKey)); }
1798         public SortedMap<K,V> tailMap(K fromKey)
1799                    { return new UnmodifiableSortedMap<>(sm.tailMap(fromKey)); }
1800         public K firstKey()                           { return sm.firstKey(); }
1801         public K lastKey()                             { return sm.lastKey(); }
1802     }
1803 
1804     /**
1805      * Returns an <a href="Collection.html#unmodview">unmodifiable view</a> of the
1806      * specified navigable map. Query operations on the returned navigable map "read

1807      * through" to the specified navigable map.  Attempts to modify the returned
1808      * navigable map, whether direct, via its collection views, or via its
1809      * {@code subMap}, {@code headMap}, or {@code tailMap} views, result in
1810      * an {@code UnsupportedOperationException}.<p>
1811      *
1812      * The returned navigable map will be serializable if the specified
1813      * navigable map is serializable.
1814      *
1815      * @param <K> the class of the map keys
1816      * @param <V> the class of the map values
1817      * @param m the navigable map for which an unmodifiable view is to be
1818      *        returned
1819      * @return an unmodifiable view of the specified navigable map
1820      * @since 1.8
1821      */
1822     public static <K,V> NavigableMap<K,V> unmodifiableNavigableMap(NavigableMap<K, ? extends V> m) {
1823         return new UnmodifiableNavigableMap<>(m);
1824     }
1825 
1826     /**


< prev index next >