jdk/src/share/classes/java/util/Collections.java
changeset 7803 56bc97d69d93
parent 7518 0282db800fe1
child 7816 55a18147b4bf
equal deleted inserted replaced
7802:74f2ee2b62ba 7803:56bc97d69d93
  1033      * @param  c the collection for which an unmodifiable view is to be
  1033      * @param  c the collection for which an unmodifiable view is to be
  1034      *         returned.
  1034      *         returned.
  1035      * @return an unmodifiable view of the specified collection.
  1035      * @return an unmodifiable view of the specified collection.
  1036      */
  1036      */
  1037     public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
  1037     public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c) {
  1038         return new UnmodifiableCollection<T>(c);
  1038         return new UnmodifiableCollection<>(c);
  1039     }
  1039     }
  1040 
  1040 
  1041     /**
  1041     /**
  1042      * @serial include
  1042      * @serial include
  1043      */
  1043      */
  1107      *
  1107      *
  1108      * @param  s the set for which an unmodifiable view is to be returned.
  1108      * @param  s the set for which an unmodifiable view is to be returned.
  1109      * @return an unmodifiable view of the specified set.
  1109      * @return an unmodifiable view of the specified set.
  1110      */
  1110      */
  1111     public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
  1111     public static <T> Set<T> unmodifiableSet(Set<? extends T> s) {
  1112         return new UnmodifiableSet<T>(s);
  1112         return new UnmodifiableSet<>(s);
  1113     }
  1113     }
  1114 
  1114 
  1115     /**
  1115     /**
  1116      * @serial include
  1116      * @serial include
  1117      */
  1117      */
  1139      * @param s the sorted set for which an unmodifiable view is to be
  1139      * @param s the sorted set for which an unmodifiable view is to be
  1140      *        returned.
  1140      *        returned.
  1141      * @return an unmodifiable view of the specified sorted set.
  1141      * @return an unmodifiable view of the specified sorted set.
  1142      */
  1142      */
  1143     public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) {
  1143     public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s) {
  1144         return new UnmodifiableSortedSet<T>(s);
  1144         return new UnmodifiableSortedSet<>(s);
  1145     }
  1145     }
  1146 
  1146 
  1147     /**
  1147     /**
  1148      * @serial include
  1148      * @serial include
  1149      */
  1149      */
  1156         UnmodifiableSortedSet(SortedSet<E> s) {super(s); ss = s;}
  1156         UnmodifiableSortedSet(SortedSet<E> s) {super(s); ss = s;}
  1157 
  1157 
  1158         public Comparator<? super E> comparator() {return ss.comparator();}
  1158         public Comparator<? super E> comparator() {return ss.comparator();}
  1159 
  1159 
  1160         public SortedSet<E> subSet(E fromElement, E toElement) {
  1160         public SortedSet<E> subSet(E fromElement, E toElement) {
  1161             return new UnmodifiableSortedSet<E>(ss.subSet(fromElement,toElement));
  1161             return new UnmodifiableSortedSet<>(ss.subSet(fromElement,toElement));
  1162         }
  1162         }
  1163         public SortedSet<E> headSet(E toElement) {
  1163         public SortedSet<E> headSet(E toElement) {
  1164             return new UnmodifiableSortedSet<E>(ss.headSet(toElement));
  1164             return new UnmodifiableSortedSet<>(ss.headSet(toElement));
  1165         }
  1165         }
  1166         public SortedSet<E> tailSet(E fromElement) {
  1166         public SortedSet<E> tailSet(E fromElement) {
  1167             return new UnmodifiableSortedSet<E>(ss.tailSet(fromElement));
  1167             return new UnmodifiableSortedSet<>(ss.tailSet(fromElement));
  1168         }
  1168         }
  1169 
  1169 
  1170         public E first()                   {return ss.first();}
  1170         public E first()                   {return ss.first();}
  1171         public E last()                    {return ss.last();}
  1171         public E last()                    {return ss.last();}
  1172     }
  1172     }
  1186      * @param  list the list for which an unmodifiable view is to be returned.
  1186      * @param  list the list for which an unmodifiable view is to be returned.
  1187      * @return an unmodifiable view of the specified list.
  1187      * @return an unmodifiable view of the specified list.
  1188      */
  1188      */
  1189     public static <T> List<T> unmodifiableList(List<? extends T> list) {
  1189     public static <T> List<T> unmodifiableList(List<? extends T> list) {
  1190         return (list instanceof RandomAccess ?
  1190         return (list instanceof RandomAccess ?
  1191                 new UnmodifiableRandomAccessList<T>(list) :
  1191                 new UnmodifiableRandomAccessList<>(list) :
  1192                 new UnmodifiableList<T>(list));
  1192                 new UnmodifiableList<>(list));
  1193     }
  1193     }
  1194 
  1194 
  1195     /**
  1195     /**
  1196      * @serial include
  1196      * @serial include
  1197      */
  1197      */
  1248                 }
  1248                 }
  1249             };
  1249             };
  1250         }
  1250         }
  1251 
  1251 
  1252         public List<E> subList(int fromIndex, int toIndex) {
  1252         public List<E> subList(int fromIndex, int toIndex) {
  1253             return new UnmodifiableList<E>(list.subList(fromIndex, toIndex));
  1253             return new UnmodifiableList<>(list.subList(fromIndex, toIndex));
  1254         }
  1254         }
  1255 
  1255 
  1256         /**
  1256         /**
  1257          * UnmodifiableRandomAccessList instances are serialized as
  1257          * UnmodifiableRandomAccessList instances are serialized as
  1258          * UnmodifiableList instances to allow them to be deserialized
  1258          * UnmodifiableList instances to allow them to be deserialized
  1265          * serialized in 1.4.1 and deserialized in 1.4 will become
  1265          * serialized in 1.4.1 and deserialized in 1.4 will become
  1266          * UnmodifiableList instances, as this method was missing in 1.4.
  1266          * UnmodifiableList instances, as this method was missing in 1.4.
  1267          */
  1267          */
  1268         private Object readResolve() {
  1268         private Object readResolve() {
  1269             return (list instanceof RandomAccess
  1269             return (list instanceof RandomAccess
  1270                     ? new UnmodifiableRandomAccessList<E>(list)
  1270                     ? new UnmodifiableRandomAccessList<>(list)
  1271                     : this);
  1271                     : this);
  1272         }
  1272         }
  1273     }
  1273     }
  1274 
  1274 
  1275     /**
  1275     /**
  1281         UnmodifiableRandomAccessList(List<? extends E> list) {
  1281         UnmodifiableRandomAccessList(List<? extends E> list) {
  1282             super(list);
  1282             super(list);
  1283         }
  1283         }
  1284 
  1284 
  1285         public List<E> subList(int fromIndex, int toIndex) {
  1285         public List<E> subList(int fromIndex, int toIndex) {
  1286             return new UnmodifiableRandomAccessList<E>(
  1286             return new UnmodifiableRandomAccessList<>(
  1287                 list.subList(fromIndex, toIndex));
  1287                 list.subList(fromIndex, toIndex));
  1288         }
  1288         }
  1289 
  1289 
  1290         private static final long serialVersionUID = -2542308836966382001L;
  1290         private static final long serialVersionUID = -2542308836966382001L;
  1291 
  1291 
  1294          * not have UnmodifiableRandomAccessList).  UnmodifiableList has
  1294          * not have UnmodifiableRandomAccessList).  UnmodifiableList has
  1295          * a readResolve method that inverts this transformation upon
  1295          * a readResolve method that inverts this transformation upon
  1296          * deserialization.
  1296          * deserialization.
  1297          */
  1297          */
  1298         private Object writeReplace() {
  1298         private Object writeReplace() {
  1299             return new UnmodifiableList<E>(list);
  1299             return new UnmodifiableList<>(list);
  1300         }
  1300         }
  1301     }
  1301     }
  1302 
  1302 
  1303     /**
  1303     /**
  1304      * Returns an unmodifiable view of the specified map.  This method
  1304      * Returns an unmodifiable view of the specified map.  This method
  1313      *
  1313      *
  1314      * @param  m the map for which an unmodifiable view is to be returned.
  1314      * @param  m the map for which an unmodifiable view is to be returned.
  1315      * @return an unmodifiable view of the specified map.
  1315      * @return an unmodifiable view of the specified map.
  1316      */
  1316      */
  1317     public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) {
  1317     public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K, ? extends V> m) {
  1318         return new UnmodifiableMap<K,V>(m);
  1318         return new UnmodifiableMap<>(m);
  1319     }
  1319     }
  1320 
  1320 
  1321     /**
  1321     /**
  1322      * @serial include
  1322      * @serial include
  1323      */
  1323      */
  1361             return keySet;
  1361             return keySet;
  1362         }
  1362         }
  1363 
  1363 
  1364         public Set<Map.Entry<K,V>> entrySet() {
  1364         public Set<Map.Entry<K,V>> entrySet() {
  1365             if (entrySet==null)
  1365             if (entrySet==null)
  1366                 entrySet = new UnmodifiableEntrySet<K,V>(m.entrySet());
  1366                 entrySet = new UnmodifiableEntrySet<>(m.entrySet());
  1367             return entrySet;
  1367             return entrySet;
  1368         }
  1368         }
  1369 
  1369 
  1370         public Collection<V> values() {
  1370         public Collection<V> values() {
  1371             if (values==null)
  1371             if (values==null)
  1398 
  1398 
  1399                     public boolean hasNext() {
  1399                     public boolean hasNext() {
  1400                         return i.hasNext();
  1400                         return i.hasNext();
  1401                     }
  1401                     }
  1402                     public Map.Entry<K,V> next() {
  1402                     public Map.Entry<K,V> next() {
  1403                         return new UnmodifiableEntry<K,V>(i.next());
  1403                         return new UnmodifiableEntry<>(i.next());
  1404                     }
  1404                     }
  1405                     public void remove() {
  1405                     public void remove() {
  1406                         throw new UnsupportedOperationException();
  1406                         throw new UnsupportedOperationException();
  1407                     }
  1407                     }
  1408                 };
  1408                 };
  1409             }
  1409             }
  1410 
  1410 
  1411             public Object[] toArray() {
  1411             public Object[] toArray() {
  1412                 Object[] a = c.toArray();
  1412                 Object[] a = c.toArray();
  1413                 for (int i=0; i<a.length; i++)
  1413                 for (int i=0; i<a.length; i++)
  1414                     a[i] = new UnmodifiableEntry<K,V>((Map.Entry<K,V>)a[i]);
  1414                     a[i] = new UnmodifiableEntry<>((Map.Entry<K,V>)a[i]);
  1415                 return a;
  1415                 return a;
  1416             }
  1416             }
  1417 
  1417 
  1418             public <T> T[] toArray(T[] a) {
  1418             public <T> T[] toArray(T[] a) {
  1419                 // We don't pass a to c.toArray, to avoid window of
  1419                 // We don't pass a to c.toArray, to avoid window of
  1420                 // vulnerability wherein an unscrupulous multithreaded client
  1420                 // vulnerability wherein an unscrupulous multithreaded client
  1421                 // could get his hands on raw (unwrapped) Entries from c.
  1421                 // could get his hands on raw (unwrapped) Entries from c.
  1422                 Object[] arr = c.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
  1422                 Object[] arr = c.toArray(a.length==0 ? a : Arrays.copyOf(a, 0));
  1423 
  1423 
  1424                 for (int i=0; i<arr.length; i++)
  1424                 for (int i=0; i<arr.length; i++)
  1425                     arr[i] = new UnmodifiableEntry<K,V>((Map.Entry<K,V>)arr[i]);
  1425                     arr[i] = new UnmodifiableEntry<>((Map.Entry<K,V>)arr[i]);
  1426 
  1426 
  1427                 if (arr.length > a.length)
  1427                 if (arr.length > a.length)
  1428                     return (T[])arr;
  1428                     return (T[])arr;
  1429 
  1429 
  1430                 System.arraycopy(arr, 0, a, 0, arr.length);
  1430                 System.arraycopy(arr, 0, a, 0, arr.length);
  1441              */
  1441              */
  1442             public boolean contains(Object o) {
  1442             public boolean contains(Object o) {
  1443                 if (!(o instanceof Map.Entry))
  1443                 if (!(o instanceof Map.Entry))
  1444                     return false;
  1444                     return false;
  1445                 return c.contains(
  1445                 return c.contains(
  1446                     new UnmodifiableEntry<Object,Object>((Map.Entry<?,?>) o));
  1446                     new UnmodifiableEntry<>((Map.Entry<?,?>) o));
  1447             }
  1447             }
  1448 
  1448 
  1449             /**
  1449             /**
  1450              * The next two methods are overridden to protect against
  1450              * The next two methods are overridden to protect against
  1451              * an unscrupulous List whose contains(Object o) method senses
  1451              * an unscrupulous List whose contains(Object o) method senses
  1515      * @param m the sorted map for which an unmodifiable view is to be
  1515      * @param m the sorted map for which an unmodifiable view is to be
  1516      *        returned.
  1516      *        returned.
  1517      * @return an unmodifiable view of the specified sorted map.
  1517      * @return an unmodifiable view of the specified sorted map.
  1518      */
  1518      */
  1519     public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) {
  1519     public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K, ? extends V> m) {
  1520         return new UnmodifiableSortedMap<K,V>(m);
  1520         return new UnmodifiableSortedMap<>(m);
  1521     }
  1521     }
  1522 
  1522 
  1523     /**
  1523     /**
  1524      * @serial include
  1524      * @serial include
  1525      */
  1525      */
  1533         UnmodifiableSortedMap(SortedMap<K, ? extends V> m) {super(m); sm = m;}
  1533         UnmodifiableSortedMap(SortedMap<K, ? extends V> m) {super(m); sm = m;}
  1534 
  1534 
  1535         public Comparator<? super K> comparator() {return sm.comparator();}
  1535         public Comparator<? super K> comparator() {return sm.comparator();}
  1536 
  1536 
  1537         public SortedMap<K,V> subMap(K fromKey, K toKey) {
  1537         public SortedMap<K,V> subMap(K fromKey, K toKey) {
  1538             return new UnmodifiableSortedMap<K,V>(sm.subMap(fromKey, toKey));
  1538             return new UnmodifiableSortedMap<>(sm.subMap(fromKey, toKey));
  1539         }
  1539         }
  1540         public SortedMap<K,V> headMap(K toKey) {
  1540         public SortedMap<K,V> headMap(K toKey) {
  1541             return new UnmodifiableSortedMap<K,V>(sm.headMap(toKey));
  1541             return new UnmodifiableSortedMap<>(sm.headMap(toKey));
  1542         }
  1542         }
  1543         public SortedMap<K,V> tailMap(K fromKey) {
  1543         public SortedMap<K,V> tailMap(K fromKey) {
  1544             return new UnmodifiableSortedMap<K,V>(sm.tailMap(fromKey));
  1544             return new UnmodifiableSortedMap<>(sm.tailMap(fromKey));
  1545         }
  1545         }
  1546 
  1546 
  1547         public K firstKey()           {return sm.firstKey();}
  1547         public K firstKey()           {return sm.firstKey();}
  1548         public K lastKey()            {return sm.lastKey();}
  1548         public K lastKey()            {return sm.lastKey();}
  1549     }
  1549     }
  1581      *
  1581      *
  1582      * @param  c the collection to be "wrapped" in a synchronized collection.
  1582      * @param  c the collection to be "wrapped" in a synchronized collection.
  1583      * @return a synchronized view of the specified collection.
  1583      * @return a synchronized view of the specified collection.
  1584      */
  1584      */
  1585     public static <T> Collection<T> synchronizedCollection(Collection<T> c) {
  1585     public static <T> Collection<T> synchronizedCollection(Collection<T> c) {
  1586         return new SynchronizedCollection<T>(c);
  1586         return new SynchronizedCollection<>(c);
  1587     }
  1587     }
  1588 
  1588 
  1589     static <T> Collection<T> synchronizedCollection(Collection<T> c, Object mutex) {
  1589     static <T> Collection<T> synchronizedCollection(Collection<T> c, Object mutex) {
  1590         return new SynchronizedCollection<T>(c, mutex);
  1590         return new SynchronizedCollection<>(c, mutex);
  1591     }
  1591     }
  1592 
  1592 
  1593     /**
  1593     /**
  1594      * @serial include
  1594      * @serial include
  1595      */
  1595      */
  1684      *
  1684      *
  1685      * @param  s the set to be "wrapped" in a synchronized set.
  1685      * @param  s the set to be "wrapped" in a synchronized set.
  1686      * @return a synchronized view of the specified set.
  1686      * @return a synchronized view of the specified set.
  1687      */
  1687      */
  1688     public static <T> Set<T> synchronizedSet(Set<T> s) {
  1688     public static <T> Set<T> synchronizedSet(Set<T> s) {
  1689         return new SynchronizedSet<T>(s);
  1689         return new SynchronizedSet<>(s);
  1690     }
  1690     }
  1691 
  1691 
  1692     static <T> Set<T> synchronizedSet(Set<T> s, Object mutex) {
  1692     static <T> Set<T> synchronizedSet(Set<T> s, Object mutex) {
  1693         return new SynchronizedSet<T>(s, mutex);
  1693         return new SynchronizedSet<>(s, mutex);
  1694     }
  1694     }
  1695 
  1695 
  1696     /**
  1696     /**
  1697      * @serial include
  1697      * @serial include
  1698      */
  1698      */
  1752      *
  1752      *
  1753      * @param  s the sorted set to be "wrapped" in a synchronized sorted set.
  1753      * @param  s the sorted set to be "wrapped" in a synchronized sorted set.
  1754      * @return a synchronized view of the specified sorted set.
  1754      * @return a synchronized view of the specified sorted set.
  1755      */
  1755      */
  1756     public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) {
  1756     public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s) {
  1757         return new SynchronizedSortedSet<T>(s);
  1757         return new SynchronizedSortedSet<>(s);
  1758     }
  1758     }
  1759 
  1759 
  1760     /**
  1760     /**
  1761      * @serial include
  1761      * @serial include
  1762      */
  1762      */
  1781             synchronized (mutex) {return ss.comparator();}
  1781             synchronized (mutex) {return ss.comparator();}
  1782         }
  1782         }
  1783 
  1783 
  1784         public SortedSet<E> subSet(E fromElement, E toElement) {
  1784         public SortedSet<E> subSet(E fromElement, E toElement) {
  1785             synchronized (mutex) {
  1785             synchronized (mutex) {
  1786                 return new SynchronizedSortedSet<E>(
  1786                 return new SynchronizedSortedSet<>(
  1787                     ss.subSet(fromElement, toElement), mutex);
  1787                     ss.subSet(fromElement, toElement), mutex);
  1788             }
  1788             }
  1789         }
  1789         }
  1790         public SortedSet<E> headSet(E toElement) {
  1790         public SortedSet<E> headSet(E toElement) {
  1791             synchronized (mutex) {
  1791             synchronized (mutex) {
  1792                 return new SynchronizedSortedSet<E>(ss.headSet(toElement), mutex);
  1792                 return new SynchronizedSortedSet<>(ss.headSet(toElement), mutex);
  1793             }
  1793             }
  1794         }
  1794         }
  1795         public SortedSet<E> tailSet(E fromElement) {
  1795         public SortedSet<E> tailSet(E fromElement) {
  1796             synchronized (mutex) {
  1796             synchronized (mutex) {
  1797                return new SynchronizedSortedSet<E>(ss.tailSet(fromElement),mutex);
  1797                return new SynchronizedSortedSet<>(ss.tailSet(fromElement),mutex);
  1798             }
  1798             }
  1799         }
  1799         }
  1800 
  1800 
  1801         public E first() {
  1801         public E first() {
  1802             synchronized (mutex) {return ss.first();}
  1802             synchronized (mutex) {return ss.first();}
  1831      * @param  list the list to be "wrapped" in a synchronized list.
  1831      * @param  list the list to be "wrapped" in a synchronized list.
  1832      * @return a synchronized view of the specified list.
  1832      * @return a synchronized view of the specified list.
  1833      */
  1833      */
  1834     public static <T> List<T> synchronizedList(List<T> list) {
  1834     public static <T> List<T> synchronizedList(List<T> list) {
  1835         return (list instanceof RandomAccess ?
  1835         return (list instanceof RandomAccess ?
  1836                 new SynchronizedRandomAccessList<T>(list) :
  1836                 new SynchronizedRandomAccessList<>(list) :
  1837                 new SynchronizedList<T>(list));
  1837                 new SynchronizedList<>(list));
  1838     }
  1838     }
  1839 
  1839 
  1840     static <T> List<T> synchronizedList(List<T> list, Object mutex) {
  1840     static <T> List<T> synchronizedList(List<T> list, Object mutex) {
  1841         return (list instanceof RandomAccess ?
  1841         return (list instanceof RandomAccess ?
  1842                 new SynchronizedRandomAccessList<T>(list, mutex) :
  1842                 new SynchronizedRandomAccessList<>(list, mutex) :
  1843                 new SynchronizedList<T>(list, mutex));
  1843                 new SynchronizedList<>(list, mutex));
  1844     }
  1844     }
  1845 
  1845 
  1846     /**
  1846     /**
  1847      * @serial include
  1847      * @serial include
  1848      */
  1848      */
  1901             return list.listIterator(index); // Must be manually synched by user
  1901             return list.listIterator(index); // Must be manually synched by user
  1902         }
  1902         }
  1903 
  1903 
  1904         public List<E> subList(int fromIndex, int toIndex) {
  1904         public List<E> subList(int fromIndex, int toIndex) {
  1905             synchronized (mutex) {
  1905             synchronized (mutex) {
  1906                 return new SynchronizedList<E>(list.subList(fromIndex, toIndex),
  1906                 return new SynchronizedList<>(list.subList(fromIndex, toIndex),
  1907                                             mutex);
  1907                                             mutex);
  1908             }
  1908             }
  1909         }
  1909         }
  1910 
  1910 
  1911         /**
  1911         /**
  1920          * serialized in 1.4.1 and deserialized in 1.4 will become
  1920          * serialized in 1.4.1 and deserialized in 1.4 will become
  1921          * SynchronizedList instances, as this method was missing in 1.4.
  1921          * SynchronizedList instances, as this method was missing in 1.4.
  1922          */
  1922          */
  1923         private Object readResolve() {
  1923         private Object readResolve() {
  1924             return (list instanceof RandomAccess
  1924             return (list instanceof RandomAccess
  1925                     ? new SynchronizedRandomAccessList<E>(list)
  1925                     ? new SynchronizedRandomAccessList<>(list)
  1926                     : this);
  1926                     : this);
  1927         }
  1927         }
  1928     }
  1928     }
  1929 
  1929 
  1930     /**
  1930     /**
  1942             super(list, mutex);
  1942             super(list, mutex);
  1943         }
  1943         }
  1944 
  1944 
  1945         public List<E> subList(int fromIndex, int toIndex) {
  1945         public List<E> subList(int fromIndex, int toIndex) {
  1946             synchronized (mutex) {
  1946             synchronized (mutex) {
  1947                 return new SynchronizedRandomAccessList<E>(
  1947                 return new SynchronizedRandomAccessList<>(
  1948                     list.subList(fromIndex, toIndex), mutex);
  1948                     list.subList(fromIndex, toIndex), mutex);
  1949             }
  1949             }
  1950         }
  1950         }
  1951 
  1951 
  1952         private static final long serialVersionUID = 1530674583602358482L;
  1952         private static final long serialVersionUID = 1530674583602358482L;
  1956          * not have SynchronizedRandomAccessList).  SynchronizedList has
  1956          * not have SynchronizedRandomAccessList).  SynchronizedList has
  1957          * a readResolve method that inverts this transformation upon
  1957          * a readResolve method that inverts this transformation upon
  1958          * deserialization.
  1958          * deserialization.
  1959          */
  1959          */
  1960         private Object writeReplace() {
  1960         private Object writeReplace() {
  1961             return new SynchronizedList<E>(list);
  1961             return new SynchronizedList<>(list);
  1962         }
  1962         }
  1963     }
  1963     }
  1964 
  1964 
  1965     /**
  1965     /**
  1966      * Returns a synchronized (thread-safe) map backed by the specified
  1966      * Returns a synchronized (thread-safe) map backed by the specified
  1988      *
  1988      *
  1989      * @param  m the map to be "wrapped" in a synchronized map.
  1989      * @param  m the map to be "wrapped" in a synchronized map.
  1990      * @return a synchronized view of the specified map.
  1990      * @return a synchronized view of the specified map.
  1991      */
  1991      */
  1992     public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {
  1992     public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m) {
  1993         return new SynchronizedMap<K,V>(m);
  1993         return new SynchronizedMap<>(m);
  1994     }
  1994     }
  1995 
  1995 
  1996     /**
  1996     /**
  1997      * @serial include
  1997      * @serial include
  1998      */
  1998      */
  2049         private transient Collection<V> values = null;
  2049         private transient Collection<V> values = null;
  2050 
  2050 
  2051         public Set<K> keySet() {
  2051         public Set<K> keySet() {
  2052             synchronized (mutex) {
  2052             synchronized (mutex) {
  2053                 if (keySet==null)
  2053                 if (keySet==null)
  2054                     keySet = new SynchronizedSet<K>(m.keySet(), mutex);
  2054                     keySet = new SynchronizedSet<>(m.keySet(), mutex);
  2055                 return keySet;
  2055                 return keySet;
  2056             }
  2056             }
  2057         }
  2057         }
  2058 
  2058 
  2059         public Set<Map.Entry<K,V>> entrySet() {
  2059         public Set<Map.Entry<K,V>> entrySet() {
  2060             synchronized (mutex) {
  2060             synchronized (mutex) {
  2061                 if (entrySet==null)
  2061                 if (entrySet==null)
  2062                     entrySet = new SynchronizedSet<Map.Entry<K,V>>(m.entrySet(), mutex);
  2062                     entrySet = new SynchronizedSet<>(m.entrySet(), mutex);
  2063                 return entrySet;
  2063                 return entrySet;
  2064             }
  2064             }
  2065         }
  2065         }
  2066 
  2066 
  2067         public Collection<V> values() {
  2067         public Collection<V> values() {
  2068             synchronized (mutex) {
  2068             synchronized (mutex) {
  2069                 if (values==null)
  2069                 if (values==null)
  2070                     values = new SynchronizedCollection<V>(m.values(), mutex);
  2070                     values = new SynchronizedCollection<>(m.values(), mutex);
  2071                 return values;
  2071                 return values;
  2072             }
  2072             }
  2073         }
  2073         }
  2074 
  2074 
  2075         public boolean equals(Object o) {
  2075         public boolean equals(Object o) {
  2127      *
  2127      *
  2128      * @param  m the sorted map to be "wrapped" in a synchronized sorted map.
  2128      * @param  m the sorted map to be "wrapped" in a synchronized sorted map.
  2129      * @return a synchronized view of the specified sorted map.
  2129      * @return a synchronized view of the specified sorted map.
  2130      */
  2130      */
  2131     public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m) {
  2131     public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m) {
  2132         return new SynchronizedSortedMap<K,V>(m);
  2132         return new SynchronizedSortedMap<>(m);
  2133     }
  2133     }
  2134 
  2134 
  2135 
  2135 
  2136     /**
  2136     /**
  2137      * @serial include
  2137      * @serial include
  2157             synchronized (mutex) {return sm.comparator();}
  2157             synchronized (mutex) {return sm.comparator();}
  2158         }
  2158         }
  2159 
  2159 
  2160         public SortedMap<K,V> subMap(K fromKey, K toKey) {
  2160         public SortedMap<K,V> subMap(K fromKey, K toKey) {
  2161             synchronized (mutex) {
  2161             synchronized (mutex) {
  2162                 return new SynchronizedSortedMap<K,V>(
  2162                 return new SynchronizedSortedMap<>(
  2163                     sm.subMap(fromKey, toKey), mutex);
  2163                     sm.subMap(fromKey, toKey), mutex);
  2164             }
  2164             }
  2165         }
  2165         }
  2166         public SortedMap<K,V> headMap(K toKey) {
  2166         public SortedMap<K,V> headMap(K toKey) {
  2167             synchronized (mutex) {
  2167             synchronized (mutex) {
  2168                 return new SynchronizedSortedMap<K,V>(sm.headMap(toKey), mutex);
  2168                 return new SynchronizedSortedMap<>(sm.headMap(toKey), mutex);
  2169             }
  2169             }
  2170         }
  2170         }
  2171         public SortedMap<K,V> tailMap(K fromKey) {
  2171         public SortedMap<K,V> tailMap(K fromKey) {
  2172             synchronized (mutex) {
  2172             synchronized (mutex) {
  2173                return new SynchronizedSortedMap<K,V>(sm.tailMap(fromKey),mutex);
  2173                return new SynchronizedSortedMap<>(sm.tailMap(fromKey),mutex);
  2174             }
  2174             }
  2175         }
  2175         }
  2176 
  2176 
  2177         public K firstKey() {
  2177         public K firstKey() {
  2178             synchronized (mutex) {return sm.firstKey();}
  2178             synchronized (mutex) {return sm.firstKey();}
  2244      * @return a dynamically typesafe view of the specified collection
  2244      * @return a dynamically typesafe view of the specified collection
  2245      * @since 1.5
  2245      * @since 1.5
  2246      */
  2246      */
  2247     public static <E> Collection<E> checkedCollection(Collection<E> c,
  2247     public static <E> Collection<E> checkedCollection(Collection<E> c,
  2248                                                       Class<E> type) {
  2248                                                       Class<E> type) {
  2249         return new CheckedCollection<E>(c, type);
  2249         return new CheckedCollection<>(c, type);
  2250     }
  2250     }
  2251 
  2251 
  2252     @SuppressWarnings("unchecked")
  2252     @SuppressWarnings("unchecked")
  2253     static <T> T[] zeroLengthArray(Class<T> type) {
  2253     static <T> T[] zeroLengthArray(Class<T> type) {
  2254         return (T[]) Array.newInstance(type, 0);
  2254         return (T[]) Array.newInstance(type, 0);
  2376      * @param type the type of element that {@code s} is permitted to hold
  2376      * @param type the type of element that {@code s} is permitted to hold
  2377      * @return a dynamically typesafe view of the specified set
  2377      * @return a dynamically typesafe view of the specified set
  2378      * @since 1.5
  2378      * @since 1.5
  2379      */
  2379      */
  2380     public static <E> Set<E> checkedSet(Set<E> s, Class<E> type) {
  2380     public static <E> Set<E> checkedSet(Set<E> s, Class<E> type) {
  2381         return new CheckedSet<E>(s, type);
  2381         return new CheckedSet<>(s, type);
  2382     }
  2382     }
  2383 
  2383 
  2384     /**
  2384     /**
  2385      * @serial include
  2385      * @serial include
  2386      */
  2386      */
  2422      * @return a dynamically typesafe view of the specified sorted set
  2422      * @return a dynamically typesafe view of the specified sorted set
  2423      * @since 1.5
  2423      * @since 1.5
  2424      */
  2424      */
  2425     public static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s,
  2425     public static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s,
  2426                                                     Class<E> type) {
  2426                                                     Class<E> type) {
  2427         return new CheckedSortedSet<E>(s, type);
  2427         return new CheckedSortedSet<>(s, type);
  2428     }
  2428     }
  2429 
  2429 
  2430     /**
  2430     /**
  2431      * @serial include
  2431      * @serial include
  2432      */
  2432      */
  2482      * @return a dynamically typesafe view of the specified list
  2482      * @return a dynamically typesafe view of the specified list
  2483      * @since 1.5
  2483      * @since 1.5
  2484      */
  2484      */
  2485     public static <E> List<E> checkedList(List<E> list, Class<E> type) {
  2485     public static <E> List<E> checkedList(List<E> list, Class<E> type) {
  2486         return (list instanceof RandomAccess ?
  2486         return (list instanceof RandomAccess ?
  2487                 new CheckedRandomAccessList<E>(list, type) :
  2487                 new CheckedRandomAccessList<>(list, type) :
  2488                 new CheckedList<E>(list, type));
  2488                 new CheckedList<>(list, type));
  2489     }
  2489     }
  2490 
  2490 
  2491     /**
  2491     /**
  2492      * @serial include
  2492      * @serial include
  2493      */
  2493      */
  2548                 }
  2548                 }
  2549             };
  2549             };
  2550         }
  2550         }
  2551 
  2551 
  2552         public List<E> subList(int fromIndex, int toIndex) {
  2552         public List<E> subList(int fromIndex, int toIndex) {
  2553             return new CheckedList<E>(list.subList(fromIndex, toIndex), type);
  2553             return new CheckedList<>(list.subList(fromIndex, toIndex), type);
  2554         }
  2554         }
  2555     }
  2555     }
  2556 
  2556 
  2557     /**
  2557     /**
  2558      * @serial include
  2558      * @serial include
  2565         CheckedRandomAccessList(List<E> list, Class<E> type) {
  2565         CheckedRandomAccessList(List<E> list, Class<E> type) {
  2566             super(list, type);
  2566             super(list, type);
  2567         }
  2567         }
  2568 
  2568 
  2569         public List<E> subList(int fromIndex, int toIndex) {
  2569         public List<E> subList(int fromIndex, int toIndex) {
  2570             return new CheckedRandomAccessList<E>(
  2570             return new CheckedRandomAccessList<>(
  2571                 list.subList(fromIndex, toIndex), type);
  2571                 list.subList(fromIndex, toIndex), type);
  2572         }
  2572         }
  2573     }
  2573     }
  2574 
  2574 
  2575     /**
  2575     /**
  2607      * @since 1.5
  2607      * @since 1.5
  2608      */
  2608      */
  2609     public static <K, V> Map<K, V> checkedMap(Map<K, V> m,
  2609     public static <K, V> Map<K, V> checkedMap(Map<K, V> m,
  2610                                               Class<K> keyType,
  2610                                               Class<K> keyType,
  2611                                               Class<V> valueType) {
  2611                                               Class<V> valueType) {
  2612         return new CheckedMap<K,V>(m, keyType, valueType);
  2612         return new CheckedMap<>(m, keyType, valueType);
  2613     }
  2613     }
  2614 
  2614 
  2615 
  2615 
  2616     /**
  2616     /**
  2617      * @serial include
  2617      * @serial include
  2675             // - good diagnostics in case of type mismatch
  2675             // - good diagnostics in case of type mismatch
  2676             // - all-or-nothing semantics
  2676             // - all-or-nothing semantics
  2677             // - protection from malicious t
  2677             // - protection from malicious t
  2678             // - correct behavior if t is a concurrent map
  2678             // - correct behavior if t is a concurrent map
  2679             Object[] entries = t.entrySet().toArray();
  2679             Object[] entries = t.entrySet().toArray();
  2680             List<Map.Entry<K,V>> checked =
  2680             List<Map.Entry<K,V>> checked = new ArrayList<>(entries.length);
  2681                 new ArrayList<Map.Entry<K,V>>(entries.length);
       
  2682             for (Object o : entries) {
  2681             for (Object o : entries) {
  2683                 Map.Entry<?,?> e = (Map.Entry<?,?>) o;
  2682                 Map.Entry<?,?> e = (Map.Entry<?,?>) o;
  2684                 Object k = e.getKey();
  2683                 Object k = e.getKey();
  2685                 Object v = e.getValue();
  2684                 Object v = e.getValue();
  2686                 typeCheck(k, v);
  2685                 typeCheck(k, v);
  2687                 checked.add(
  2686                 checked.add(
  2688                     new AbstractMap.SimpleImmutableEntry<K,V>((K) k, (V) v));
  2687                     new AbstractMap.SimpleImmutableEntry<>((K) k, (V) v));
  2689             }
  2688             }
  2690             for (Map.Entry<K,V> e : checked)
  2689             for (Map.Entry<K,V> e : checked)
  2691                 m.put(e.getKey(), e.getValue());
  2690                 m.put(e.getKey(), e.getValue());
  2692         }
  2691         }
  2693 
  2692 
  2694         private transient Set<Map.Entry<K,V>> entrySet = null;
  2693         private transient Set<Map.Entry<K,V>> entrySet = null;
  2695 
  2694 
  2696         public Set<Map.Entry<K,V>> entrySet() {
  2695         public Set<Map.Entry<K,V>> entrySet() {
  2697             if (entrySet==null)
  2696             if (entrySet==null)
  2698                 entrySet = new CheckedEntrySet<K,V>(m.entrySet(), valueType);
  2697                 entrySet = new CheckedEntrySet<>(m.entrySet(), valueType);
  2699             return entrySet;
  2698             return entrySet;
  2700         }
  2699         }
  2701 
  2700 
  2702         /**
  2701         /**
  2703          * We need this class in addition to CheckedSet as Map.Entry permits
  2702          * We need this class in addition to CheckedSet as Map.Entry permits
  2808 
  2807 
  2809             public boolean remove(Object o) {
  2808             public boolean remove(Object o) {
  2810                 if (!(o instanceof Map.Entry))
  2809                 if (!(o instanceof Map.Entry))
  2811                     return false;
  2810                     return false;
  2812                 return s.remove(new AbstractMap.SimpleImmutableEntry
  2811                 return s.remove(new AbstractMap.SimpleImmutableEntry
  2813                                 <Object, Object>((Map.Entry<?,?>)o));
  2812                                 <>((Map.Entry<?,?>)o));
  2814             }
  2813             }
  2815 
  2814 
  2816             public boolean removeAll(Collection<?> c) {
  2815             public boolean removeAll(Collection<?> c) {
  2817                 return batchRemove(c, false);
  2816                 return batchRemove(c, false);
  2818             }
  2817             }
  2841                     && containsAll(that); // Invokes safe containsAll() above
  2840                     && containsAll(that); // Invokes safe containsAll() above
  2842             }
  2841             }
  2843 
  2842 
  2844             static <K,V,T> CheckedEntry<K,V,T> checkedEntry(Map.Entry<K,V> e,
  2843             static <K,V,T> CheckedEntry<K,V,T> checkedEntry(Map.Entry<K,V> e,
  2845                                                             Class<T> valueType) {
  2844                                                             Class<T> valueType) {
  2846                 return new CheckedEntry<K,V,T>(e, valueType);
  2845                 return new CheckedEntry<>(e, valueType);
  2847             }
  2846             }
  2848 
  2847 
  2849             /**
  2848             /**
  2850              * This "wrapper class" serves two purposes: it prevents
  2849              * This "wrapper class" serves two purposes: it prevents
  2851              * the client from modifying the backing Map, by short-circuiting
  2850              * the client from modifying the backing Map, by short-circuiting
  2882                     if (o == this)
  2881                     if (o == this)
  2883                         return true;
  2882                         return true;
  2884                     if (!(o instanceof Map.Entry))
  2883                     if (!(o instanceof Map.Entry))
  2885                         return false;
  2884                         return false;
  2886                     return e.equals(new AbstractMap.SimpleImmutableEntry
  2885                     return e.equals(new AbstractMap.SimpleImmutableEntry
  2887                                     <Object, Object>((Map.Entry<?,?>)o));
  2886                                     <>((Map.Entry<?,?>)o));
  2888                 }
  2887                 }
  2889             }
  2888             }
  2890         }
  2889         }
  2891     }
  2890     }
  2892 
  2891 
  2925      * @since 1.5
  2924      * @since 1.5
  2926      */
  2925      */
  2927     public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K, V> m,
  2926     public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K, V> m,
  2928                                                         Class<K> keyType,
  2927                                                         Class<K> keyType,
  2929                                                         Class<V> valueType) {
  2928                                                         Class<V> valueType) {
  2930         return new CheckedSortedMap<K,V>(m, keyType, valueType);
  2929         return new CheckedSortedMap<>(m, keyType, valueType);
  2931     }
  2930     }
  2932 
  2931 
  2933     /**
  2932     /**
  2934      * @serial include
  2933      * @serial include
  2935      */
  2934      */
  2991         return (Iterator<T>) EmptyIterator.EMPTY_ITERATOR;
  2990         return (Iterator<T>) EmptyIterator.EMPTY_ITERATOR;
  2992     }
  2991     }
  2993 
  2992 
  2994     private static class EmptyIterator<E> implements Iterator<E> {
  2993     private static class EmptyIterator<E> implements Iterator<E> {
  2995         static final EmptyIterator<Object> EMPTY_ITERATOR
  2994         static final EmptyIterator<Object> EMPTY_ITERATOR
  2996             = new EmptyIterator<Object>();
  2995             = new EmptyIterator<>();
  2997 
  2996 
  2998         public boolean hasNext() { return false; }
  2997         public boolean hasNext() { return false; }
  2999         public E next() { throw new NoSuchElementException(); }
  2998         public E next() { throw new NoSuchElementException(); }
  3000         public void remove() { throw new IllegalStateException(); }
  2999         public void remove() { throw new IllegalStateException(); }
  3001     }
  3000     }
  3040     private static class EmptyListIterator<E>
  3039     private static class EmptyListIterator<E>
  3041         extends EmptyIterator<E>
  3040         extends EmptyIterator<E>
  3042         implements ListIterator<E>
  3041         implements ListIterator<E>
  3043     {
  3042     {
  3044         static final EmptyListIterator<Object> EMPTY_ITERATOR
  3043         static final EmptyListIterator<Object> EMPTY_ITERATOR
  3045             = new EmptyListIterator<Object>();
  3044             = new EmptyListIterator<>();
  3046 
  3045 
  3047         public boolean hasPrevious() { return false; }
  3046         public boolean hasPrevious() { return false; }
  3048         public E previous() { throw new NoSuchElementException(); }
  3047         public E previous() { throw new NoSuchElementException(); }
  3049         public int nextIndex()     { return 0; }
  3048         public int nextIndex()     { return 0; }
  3050         public int previousIndex() { return -1; }
  3049         public int previousIndex() { return -1; }
  3076         return (Enumeration<T>) EmptyEnumeration.EMPTY_ENUMERATION;
  3075         return (Enumeration<T>) EmptyEnumeration.EMPTY_ENUMERATION;
  3077     }
  3076     }
  3078 
  3077 
  3079     private static class EmptyEnumeration<E> implements Enumeration<E> {
  3078     private static class EmptyEnumeration<E> implements Enumeration<E> {
  3080         static final EmptyEnumeration<Object> EMPTY_ENUMERATION
  3079         static final EmptyEnumeration<Object> EMPTY_ENUMERATION
  3081             = new EmptyEnumeration<Object>();
  3080             = new EmptyEnumeration<>();
  3082 
  3081 
  3083         public boolean hasMoreElements() { return false; }
  3082         public boolean hasMoreElements() { return false; }
  3084         public E nextElement() { throw new NoSuchElementException(); }
  3083         public E nextElement() { throw new NoSuchElementException(); }
  3085     }
  3084     }
  3086 
  3085 
  3088      * The empty set (immutable).  This set is serializable.
  3087      * The empty set (immutable).  This set is serializable.
  3089      *
  3088      *
  3090      * @see #emptySet()
  3089      * @see #emptySet()
  3091      */
  3090      */
  3092     @SuppressWarnings("unchecked")
  3091     @SuppressWarnings("unchecked")
  3093     public static final Set EMPTY_SET = new EmptySet<Object>();
  3092     public static final Set EMPTY_SET = new EmptySet<>();
  3094 
  3093 
  3095     /**
  3094     /**
  3096      * Returns the empty set (immutable).  This set is serializable.
  3095      * Returns the empty set (immutable).  This set is serializable.
  3097      * Unlike the like-named field, this method is parameterized.
  3096      * Unlike the like-named field, this method is parameterized.
  3098      *
  3097      *
  3148      * The empty list (immutable).  This list is serializable.
  3147      * The empty list (immutable).  This list is serializable.
  3149      *
  3148      *
  3150      * @see #emptyList()
  3149      * @see #emptyList()
  3151      */
  3150      */
  3152     @SuppressWarnings("unchecked")
  3151     @SuppressWarnings("unchecked")
  3153     public static final List EMPTY_LIST = new EmptyList<Object>();
  3152     public static final List EMPTY_LIST = new EmptyList<>();
  3154 
  3153 
  3155     /**
  3154     /**
  3156      * Returns the empty list (immutable).  This list is serializable.
  3155      * Returns the empty list (immutable).  This list is serializable.
  3157      *
  3156      *
  3158      * <p>This example illustrates the type-safe way to obtain an empty list:
  3157      * <p>This example illustrates the type-safe way to obtain an empty list:
  3222      *
  3221      *
  3223      * @see #emptyMap()
  3222      * @see #emptyMap()
  3224      * @since 1.3
  3223      * @since 1.3
  3225      */
  3224      */
  3226     @SuppressWarnings("unchecked")
  3225     @SuppressWarnings("unchecked")
  3227     public static final Map EMPTY_MAP = new EmptyMap<Object,Object>();
  3226     public static final Map EMPTY_MAP = new EmptyMap<>();
  3228 
  3227 
  3229     /**
  3228     /**
  3230      * Returns the empty map (immutable).  This map is serializable.
  3229      * Returns the empty map (immutable).  This map is serializable.
  3231      *
  3230      *
  3232      * <p>This example illustrates the type-safe way to obtain an empty set:
  3231      * <p>This example illustrates the type-safe way to obtain an empty set:
  3284      *
  3283      *
  3285      * @param o the sole object to be stored in the returned set.
  3284      * @param o the sole object to be stored in the returned set.
  3286      * @return an immutable set containing only the specified object.
  3285      * @return an immutable set containing only the specified object.
  3287      */
  3286      */
  3288     public static <T> Set<T> singleton(T o) {
  3287     public static <T> Set<T> singleton(T o) {
  3289         return new SingletonSet<T>(o);
  3288         return new SingletonSet<>(o);
  3290     }
  3289     }
  3291 
  3290 
  3292     static <E> Iterator<E> singletonIterator(final E e) {
  3291     static <E> Iterator<E> singletonIterator(final E e) {
  3293         return new Iterator<E>() {
  3292         return new Iterator<E>() {
  3294             private boolean hasNext = true;
  3293             private boolean hasNext = true;
  3337      * @param o the sole object to be stored in the returned list.
  3336      * @param o the sole object to be stored in the returned list.
  3338      * @return an immutable list containing only the specified object.
  3337      * @return an immutable list containing only the specified object.
  3339      * @since 1.3
  3338      * @since 1.3
  3340      */
  3339      */
  3341     public static <T> List<T> singletonList(T o) {
  3340     public static <T> List<T> singletonList(T o) {
  3342         return new SingletonList<T>(o);
  3341         return new SingletonList<>(o);
  3343     }
  3342     }
  3344 
  3343 
  3345     /**
  3344     /**
  3346      * @serial include
  3345      * @serial include
  3347      */
  3346      */
  3379      * @return an immutable map containing only the specified key-value
  3378      * @return an immutable map containing only the specified key-value
  3380      *         mapping.
  3379      *         mapping.
  3381      * @since 1.3
  3380      * @since 1.3
  3382      */
  3381      */
  3383     public static <K,V> Map<K,V> singletonMap(K key, V value) {
  3382     public static <K,V> Map<K,V> singletonMap(K key, V value) {
  3384         return new SingletonMap<K,V>(key, value);
  3383         return new SingletonMap<>(key, value);
  3385     }
  3384     }
  3386 
  3385 
  3387     /**
  3386     /**
  3388      * @serial include
  3387      * @serial include
  3389      */
  3388      */
  3421         }
  3420         }
  3422 
  3421 
  3423         public Set<Map.Entry<K,V>> entrySet() {
  3422         public Set<Map.Entry<K,V>> entrySet() {
  3424             if (entrySet==null)
  3423             if (entrySet==null)
  3425                 entrySet = Collections.<Map.Entry<K,V>>singleton(
  3424                 entrySet = Collections.<Map.Entry<K,V>>singleton(
  3426                     new SimpleImmutableEntry<K,V>(k, v));
  3425                     new SimpleImmutableEntry<>(k, v));
  3427             return entrySet;
  3426             return entrySet;
  3428         }
  3427         }
  3429 
  3428 
  3430         public Collection<V> values() {
  3429         public Collection<V> values() {
  3431             if (values==null)
  3430             if (values==null)
  3453      * @see    List#addAll(int, Collection)
  3452      * @see    List#addAll(int, Collection)
  3454      */
  3453      */
  3455     public static <T> List<T> nCopies(int n, T o) {
  3454     public static <T> List<T> nCopies(int n, T o) {
  3456         if (n < 0)
  3455         if (n < 0)
  3457             throw new IllegalArgumentException("List length = " + n);
  3456             throw new IllegalArgumentException("List length = " + n);
  3458         return new CopiesList<T>(n, o);
  3457         return new CopiesList<>(n, o);
  3459     }
  3458     }
  3460 
  3459 
  3461     /**
  3460     /**
  3462      * @serial include
  3461      * @serial include
  3463      */
  3462      */
  3527             if (toIndex > n)
  3526             if (toIndex > n)
  3528                 throw new IndexOutOfBoundsException("toIndex = " + toIndex);
  3527                 throw new IndexOutOfBoundsException("toIndex = " + toIndex);
  3529             if (fromIndex > toIndex)
  3528             if (fromIndex > toIndex)
  3530                 throw new IllegalArgumentException("fromIndex(" + fromIndex +
  3529                 throw new IllegalArgumentException("fromIndex(" + fromIndex +
  3531                                                    ") > toIndex(" + toIndex + ")");
  3530                                                    ") > toIndex(" + toIndex + ")");
  3532             return new CopiesList<E>(toIndex - fromIndex, element);
  3531             return new CopiesList<>(toIndex - fromIndex, element);
  3533         }
  3532         }
  3534     }
  3533     }
  3535 
  3534 
  3536     /**
  3535     /**
  3537      * Returns a comparator that imposes the reverse of the <i>natural
  3536      * Returns a comparator that imposes the reverse of the <i>natural
  3593             return reverseOrder();
  3592             return reverseOrder();
  3594 
  3593 
  3595         if (cmp instanceof ReverseComparator2)
  3594         if (cmp instanceof ReverseComparator2)
  3596             return ((ReverseComparator2<T>)cmp).cmp;
  3595             return ((ReverseComparator2<T>)cmp).cmp;
  3597 
  3596 
  3598         return new ReverseComparator2<T>(cmp);
  3597         return new ReverseComparator2<>(cmp);
  3599     }
  3598     }
  3600 
  3599 
  3601     /**
  3600     /**
  3602      * @serial include
  3601      * @serial include
  3603      */
  3602      */
  3672      * @since 1.4
  3671      * @since 1.4
  3673      * @see Enumeration
  3672      * @see Enumeration
  3674      * @see ArrayList
  3673      * @see ArrayList
  3675      */
  3674      */
  3676     public static <T> ArrayList<T> list(Enumeration<T> e) {
  3675     public static <T> ArrayList<T> list(Enumeration<T> e) {
  3677         ArrayList<T> l = new ArrayList<T>();
  3676         ArrayList<T> l = new ArrayList<>();
  3678         while (e.hasMoreElements())
  3677         while (e.hasMoreElements())
  3679             l.add(e.nextElement());
  3678             l.add(e.nextElement());
  3680         return l;
  3679         return l;
  3681     }
  3680     }
  3682 
  3681 
  3817      * @return the set backed by the map
  3816      * @return the set backed by the map
  3818      * @throws IllegalArgumentException if <tt>map</tt> is not empty
  3817      * @throws IllegalArgumentException if <tt>map</tt> is not empty
  3819      * @since 1.6
  3818      * @since 1.6
  3820      */
  3819      */
  3821     public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
  3820     public static <E> Set<E> newSetFromMap(Map<E, Boolean> map) {
  3822         return new SetFromMap<E>(map);
  3821         return new SetFromMap<>(map);
  3823     }
  3822     }
  3824 
  3823 
  3825     /**
  3824     /**
  3826      * @serial include
  3825      * @serial include
  3827      */
  3826      */
  3881      * @param deque the deque
  3880      * @param deque the deque
  3882      * @return the queue
  3881      * @return the queue
  3883      * @since  1.6
  3882      * @since  1.6
  3884      */
  3883      */
  3885     public static <T> Queue<T> asLifoQueue(Deque<T> deque) {
  3884     public static <T> Queue<T> asLifoQueue(Deque<T> deque) {
  3886         return new AsLIFOQueue<T>(deque);
  3885         return new AsLIFOQueue<>(deque);
  3887     }
  3886     }
  3888 
  3887 
  3889     /**
  3888     /**
  3890      * @serial include
  3889      * @serial include
  3891      */
  3890      */