📄 collections.html
字号:
<p>The returned set will be serializable if the specified set is serializable.<DD><DL><DT><B>Parameters:</B><DD><CODE>s</CODE> - the set to be "wrapped" in a synchronized set.<DT><B>Returns:</B><DD>a synchronized view of the specified set.</DL></DD></DL><HR><A NAME="synchronizedSortedSet(java.util.SortedSet)"><!-- --></A><H3>synchronizedSortedSet</H3><PRE>public static <A HREF="../../java/util/SortedSet.html">SortedSet</A> <B>synchronizedSortedSet</B>(<A HREF="../../java/util/SortedSet.html">SortedSet</A> s)</PRE><DL><DD>Returns a synchronized (thread-safe) sorted set backed by the specified sorted set. In order to guarantee serial access, it is critical that <strong>all</strong> access to the backing sorted set is accomplished through the returned sorted set (or its views).<p> It is imperative that the user manually synchronize on the returned sorted set when iterating over it or any of its <tt>subSet</tt>, <tt>headSet</tt>, or <tt>tailSet</tt> views. <pre> SortedSet s = Collections.synchronizedSortedSet(new HashSortedSet()); ... synchronized(s) { Iterator i = s.iterator(); // Must be in the synchronized block while (i.hasNext()) foo(i.next()); } </pre> or: <pre> SortedSet s = Collections.synchronizedSortedSet(new HashSortedSet()); SortedSet s2 = s.headSet(foo); ... synchronized(s) { // Note: s, not s2!!! Iterator i = s2.iterator(); // Must be in the synchronized block while (i.hasNext()) foo(i.next()); } </pre> Failure to follow this advice may result in non-deterministic behavior. <p>The returned sorted set will be serializable if the specified sorted set is serializable.<DD><DL><DT><B>Parameters:</B><DD><CODE>s</CODE> - the sorted set to be "wrapped" in a synchronized sorted set.<DT><B>Returns:</B><DD>a synchronized view of the specified sorted set.</DL></DD></DL><HR><A NAME="synchronizedList(java.util.List)"><!-- --></A><H3>synchronizedList</H3><PRE>public static <A HREF="../../java/util/List.html">List</A> <B>synchronizedList</B>(<A HREF="../../java/util/List.html">List</A> list)</PRE><DL><DD>Returns a synchronized (thread-safe) list backed by the specified list. In order to guarantee serial access, it is critical that <strong>all</strong> access to the backing list is accomplished through the returned list.<p> It is imperative that the user manually synchronize on the returned list when iterating over it: <pre> List list = Collections.synchronizedList(new ArrayList()); ... synchronized(list) { Iterator i = list.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); } </pre> Failure to follow this advice may result in non-deterministic behavior. <p>The returned list will be serializable if the specified list is serializable.<DD><DL><DT><B>Parameters:</B><DD><CODE>list</CODE> - the list to be "wrapped" in a synchronized list.<DT><B>Returns:</B><DD>a synchronized view of the specified list.</DL></DD></DL><HR><A NAME="synchronizedMap(java.util.Map)"><!-- --></A><H3>synchronizedMap</H3><PRE>public static <A HREF="../../java/util/Map.html">Map</A> <B>synchronizedMap</B>(<A HREF="../../java/util/Map.html">Map</A> m)</PRE><DL><DD>Returns a synchronized (thread-safe) map backed by the specified map. In order to guarantee serial access, it is critical that <strong>all</strong> access to the backing map is accomplished through the returned map.<p> It is imperative that the user manually synchronize on the returned map when iterating over any of its collection views: <pre> Map m = Collections.synchronizedMap(new HashMap()); ... Set s = m.keySet(); // Needn't be in synchronized block ... synchronized(m) { // Synchronizing on m, not s! Iterator i = s.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); } </pre> Failure to follow this advice may result in non-deterministic behavior. <p>The returned map will be serializable if the specified map is serializable.<DD><DL><DT><B>Parameters:</B><DD><CODE>m</CODE> - the map to be "wrapped" in a synchronized map.<DT><B>Returns:</B><DD>a synchronized view of the specified map.</DL></DD></DL><HR><A NAME="synchronizedSortedMap(java.util.SortedMap)"><!-- --></A><H3>synchronizedSortedMap</H3><PRE>public static <A HREF="../../java/util/SortedMap.html">SortedMap</A> <B>synchronizedSortedMap</B>(<A HREF="../../java/util/SortedMap.html">SortedMap</A> m)</PRE><DL><DD>Returns a synchronized (thread-safe) sorted map backed by the specified sorted map. In order to guarantee serial access, it is critical that <strong>all</strong> access to the backing sorted map is accomplished through the returned sorted map (or its views).<p> It is imperative that the user manually synchronize on the returned sorted map when iterating over any of its collection views, or the collections views of any of its <tt>subMap</tt>, <tt>headMap</tt> or <tt>tailMap</tt> views. <pre> SortedMap m = Collections.synchronizedSortedMap(new HashSortedMap()); ... Set s = m.keySet(); // Needn't be in synchronized block ... synchronized(m) { // Synchronizing on m, not s! Iterator i = s.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); } </pre> or: <pre> SortedMap m = Collections.synchronizedSortedMap(new HashSortedMap()); SortedMap m2 = m.subMap(foo, bar); ... Set s2 = m2.keySet(); // Needn't be in synchronized block ... synchronized(m) { // Synchronizing on m, not m2 or s2! Iterator i = s.iterator(); // Must be in synchronized block while (i.hasNext()) foo(i.next()); } </pre> Failure to follow this advice may result in non-deterministic behavior. <p>The returned sorted map will be serializable if the specified sorted map is serializable.<DD><DL><DT><B>Parameters:</B><DD><CODE>m</CODE> - the sorted map to be "wrapped" in a synchronized sorted map.<DT><B>Returns:</B><DD>a synchronized view of the specified sorted map.</DL></DD></DL><HR><A NAME="singleton(java.lang.Object)"><!-- --></A><H3>singleton</H3><PRE>public static <A HREF="../../java/util/Set.html">Set</A> <B>singleton</B>(<A HREF="../../java/lang/Object.html">Object</A> o)</PRE><DL><DD>Returns an immutable set containing only the specified object. The returned set is serializable.<DD><DL><DT><B>Parameters:</B><DD><CODE>o</CODE> - the sole object to be stored in the returned set.<DT><B>Returns:</B><DD>an immutable set containing only the specified object.</DL></DD></DL><HR><A NAME="singletonList(java.lang.Object)"><!-- --></A><H3>singletonList</H3><PRE>public static <A HREF="../../java/util/List.html">List</A> <B>singletonList</B>(<A HREF="../../java/lang/Object.html">Object</A> o)</PRE><DL><DD>Returns an immutable list containing only the specified object. The returned list is serializable.<DD><DL><DT><B>Parameters:</B><DD><CODE>o</CODE> - the sole object to be stored in the returned list.<DT><B>Returns:</B><DD>an immutable list containing only the specified object.<DT><B>Since: </B><DD>1.3</DD></DL></DD></DL><HR><A NAME="singletonMap(java.lang.Object, java.lang.Object)"><!-- --></A><H3>singletonMap</H3><PRE>public static <A HREF="../../java/util/Map.html">Map</A> <B>singletonMap</B>(<A HREF="../../java/lang/Object.html">Object</A> key, <A HREF="../../java/lang/Object.html">Object</A> value)</PRE><DL><DD>Returns an immutable map, mapping only the specified key to the specified value. The returned map is serializable.<DD><DL><DT><B>Parameters:</B><DD><CODE>key</CODE> - the sole key to be stored in the returned map.<DD><CODE>value</CODE> - the value to which the returned map maps <tt>key</tt>.<DT><B>Returns:</B><DD>an immutable map containing only the specified key-value mapping.<DT><B>Since: </B><DD>1.3</DD></DL></DD></DL><HR><A NAME="nCopies(int, java.lang.Object)"><!-- --></A><H3>nCopies</H3><PRE>public static <A HREF="../../java/util/List.html">List</A> <B>nCopies</B>(int n, <A HREF="../../java/lang/Object.html">Object</A> o)</PRE><DL><DD>Returns an immutable list consisting of <tt>n</tt> copies of the specified object. The newly allocated data object is tiny (it contains a single reference to the data object). This method is useful in combination with the <tt>List.addAll</tt> method to grow lists. The returned list is serializable.<DD><DL><DT><B>Parameters:</B><DD><CODE>n</CODE> - the number of elements in the returned list.<DD><CODE>o</CODE> - the element to appear repeatedly in the returned list.<DT><B>Returns:</B><DD>an immutable list consisting of <tt>n</tt> copies of the specified object.<DT><B>Throws:</B><DD><CODE><A HREF="../../java/lang/IllegalArgumentException.html">IllegalArgumentException</A></CODE> - if n < 0.<DT><B>See Also: </B><DD><A HREF="../../java/util/List.html#addAll(java.util.Collection)"><CODE>List.addAll(Collection)</CODE></A>, <A HREF="../../java/util/List.html#addAll(int, java.util.Collection)"><CODE>List.addAll(int, Collection)</CODE></A></DL></DD></DL><HR><A NAME="reverseOrder()"><!-- --></A><H3>reverseOrder</H3><PRE>public static <A HREF="../../java/util/Comparator.html">Comparator</A> <B>reverseOrder</B>()</PRE><DL><DD>Returns a comparator that imposes the reverse of the <i>natural ordering</i> on a collection of objects that implement the <tt>Comparable</tt> interface. (The natural ordering is the ordering imposed by the objects' own <tt>compareTo</tt> method.) This enables a simple idiom for sorting (or maintaining) collections (or arrays) of objects that implement the <tt>Comparable</tt> interface in reverse-natural-order. For example, suppose a is an array of strings. Then: <pre> Arrays.sort(a, Collections.reverseOrder()); </pre> sorts the array in reverse-lexicographic (alphabetical) order.<p> The returned comparator is serializable.<DD><DL><DT><B>Returns:</B><DD>a comparator that imposes the reverse of the <i>natural ordering</i> on a collection of objects that implement the <tt>Comparable</tt> interface.<DT><B>See Also: </B><DD><A HREF="../../java/lang/Comparable.html"><CODE>Comparable</CODE></A></DL></DD></DL><HR><A NAME="enumeration(java.util.Collection)"><!-- --></A><H3>enumeration</H3><PRE>public static <A HREF="../../java/util/Enumeration.html">Enumeration</A> <B>enumeration</B>(<A HREF="../../java/util/Collection.html">Collection</A> c)</PRE><DL><DD>Returns an enumeration over the specified collection. This provides interoperatbility with legacy APIs that require an enumeration as input.<DD><DL><DT><B>Parameters:</B><DD><CODE>c</CODE> - the collection for whi
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -