|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjava.util.Collections
public class Collections
Utility class consisting of static methods that operate on, or return Collections. Contains methods to sort, search, reverse, fill and shuffle Collections, methods to facilitate interoperability with legacy APIs that are unaware of collections, a method to return a list which consists of multiple copies of one element, and methods which "wrap" collections to give them extra properties, such as thread-safety and unmodifiability.
All methods which take a collection throw a NullPointerException
if
that collection is null. Algorithms which can change a collection may, but
are not required, to throw the UnsupportedOperationException
that
the underlying collection would throw during an attempt at modification.
For example,
Collections.singleton("").addAll(Collections.EMPTY_SET)
does not throw a exception, even though addAll is an unsupported operation
on a singleton; the reason for this is that addAll did not attempt to
modify the set.
Collection
,
Set
,
List
,
Map
,
Arrays
Field Summary | |
---|---|
static List |
EMPTY_LIST
An immutable, serializable, empty List, which implements RandomAccess. |
static Map |
EMPTY_MAP
An immutable, serializable, empty Map. |
static Set |
EMPTY_SET
An immutable, serializable, empty Set. |
Method Summary | ||
---|---|---|
static
|
addAll(Collection<? super T> c,
T... a)
Adds all the specified elements to the given collection, in a similar way to the addAll method of the Collection . |
|
static
|
asLifoQueue(java.util.Deque<T> deque)
Returns a view of a Deque as a stack or LIFO (Last-In-First-Out)
Queue . |
|
static
|
binarySearch(List<? extends Comparable<? super T>> l,
T key)
Perform a binary search of a List for a key, using the natural ordering of the elements. |
|
static
|
binarySearch(List<? extends T> l,
T key,
Comparator<? super T> c)
Perform a binary search of a List for a key, using a supplied Comparator. |
|
static
|
checkedCollection(Collection<E> c,
Class<E> type)
Returns a dynamically typesafe view of the given collection, where any modification is first checked to ensure that the type of the new data is appropriate. |
|
static
|
checkedList(List<E> l,
Class<E> type)
Returns a dynamically typesafe view of the given list, where any modification is first checked to ensure that the type of the new data is appropriate. |
|
static
|
checkedMap(Map<K,V> m,
Class<K> keyType,
Class<V> valueType)
Returns a dynamically typesafe view of the given map, where any modification is first checked to ensure that the type of the new data is appropriate. |
|
static
|
checkedSet(Set<E> s,
Class<E> type)
Returns a dynamically typesafe view of the given set, where any modification is first checked to ensure that the type of the new data is appropriate. |
|
static
|
checkedSortedMap(SortedMap<K,V> m,
Class<K> keyType,
Class<V> valueType)
Returns a dynamically typesafe view of the given sorted map, where any modification is first checked to ensure that the type of the new data is appropriate. |
|
static
|
checkedSortedSet(SortedSet<E> s,
Class<E> type)
Returns a dynamically typesafe view of the given sorted set, where any modification is first checked to ensure that the type of the new data is appropriate. |
|
static
|
copy(List<? super T> dest,
List<? extends T> source)
Copy one list to another. |
|
static boolean |
disjoint(Collection<?> c1,
Collection<?> c2)
Returns true if the two specified collections have no elements in common. |
|
static
|
emptyList()
Returns an immutable, serializable parameterized empty list. |
|
static
|
emptyMap()
Returns an immutable, serializable parameterized empty map. |
|
static
|
emptySet()
Returns an immutable, serializable parameterized empty set. |
|
static
|
enumeration(Collection<T> c)
Returns an Enumeration over a collection. |
|
static
|
fill(List<? super T> l,
T val)
Replace every element of a list with a given value. |
|
static int |
frequency(Collection<?> c,
Object o)
Returns the frequency of the specified object within the supplied collection. |
|
static int |
indexOfSubList(List<?> source,
List<?> target)
Returns the starting index where the specified sublist first occurs in a larger list, or -1 if there is no matching position. |
|
static int |
lastIndexOfSubList(List<?> source,
List<?> target)
Returns the starting index where the specified sublist last occurs in a larger list, or -1 if there is no matching position. |
|
static
|
list(Enumeration<T> e)
Returns an ArrayList holding the elements visited by a given Enumeration. |
|
static
|
max(Collection<? extends T> c)
Find the maximum element in a Collection, according to the natural ordering of the elements. |
|
static
|
max(Collection<? extends T> c,
Comparator<? super T> order)
Find the maximum element in a Collection, according to a specified Comparator. |
|
static
|
min(Collection<? extends T> c)
Find the minimum element in a Collection, according to the natural ordering of the elements. |
|
static
|
min(Collection<? extends T> c,
Comparator<? super T> order)
Find the minimum element in a Collection, according to a specified Comparator. |
|
static
|
nCopies(int n,
T o)
Creates an immutable list consisting of the same object repeated n times. |
|
static
|
newSetFromMap(Map<E,Boolean> map)
Returns a set backed by the supplied map. |
|
static
|
replaceAll(List<T> list,
T oldval,
T newval)
Replace all instances of one object with another in the specified list. |
|
static void |
reverse(List<?> l)
Reverse a given list. |
|
static
|
reverseOrder()
Get a comparator that implements the reverse of natural ordering. |
|
static
|
reverseOrder(Comparator<T> c)
Get a comparator that implements the reverse of the ordering specified by the given Comparator. |
|
static void |
rotate(List<?> list,
int distance)
Rotate the elements in a list by a specified distance. |
|
static void |
shuffle(List<?> l)
Shuffle a list according to a default source of randomness. |
|
static void |
shuffle(List<?> l,
Random r)
Shuffle a list according to a given source of randomness. |
|
static
|
singleton(T o)
Obtain an immutable Set consisting of a single element. |
|
static
|
singletonList(T o)
Obtain an immutable List consisting of a single element. |
|
static
|
singletonMap(K key,
V value)
Obtain an immutable Map consisting of a single key-value pair. |
|
static
|
sort(List<T> l)
Sort a list according to the natural ordering of its elements. |
|
static
|
sort(List<T> l,
Comparator<? super T> c)
Sort a list according to a specified Comparator. |
|
static void |
swap(List<?> l,
int i,
int j)
Swaps the elements at the specified positions within the list. |
|
static
|
synchronizedCollection(Collection<T> c)
Returns a synchronized (thread-safe) collection wrapper backed by the given collection. |
|
static
|
synchronizedList(List<T> l)
Returns a synchronized (thread-safe) list wrapper backed by the given list. |
|
static
|
synchronizedMap(Map<K,V> m)
Returns a synchronized (thread-safe) map wrapper backed by the given map. |
|
static
|
synchronizedSet(Set<T> s)
Returns a synchronized (thread-safe) set wrapper backed by the given set. |
|
static
|
synchronizedSortedMap(SortedMap<K,V> m)
Returns a synchronized (thread-safe) sorted map wrapper backed by the given map. |
|
static
|
synchronizedSortedSet(SortedSet<T> s)
Returns a synchronized (thread-safe) sorted set wrapper backed by the given set. |
|
static
|
unmodifiableCollection(Collection<? extends T> c)
Returns an unmodifiable view of the given collection. |
|
static
|
unmodifiableList(List<? extends T> l)
Returns an unmodifiable view of the given list. |
|
static
|
unmodifiableMap(Map<? extends K,? extends V> m)
Returns an unmodifiable view of the given map. |
|
static
|
unmodifiableSet(Set<? extends T> s)
Returns an unmodifiable view of the given set. |
|
static
|
unmodifiableSortedMap(SortedMap<K,? extends V> m)
Returns an unmodifiable view of the given sorted map. |
|
static
|
unmodifiableSortedSet(SortedSet<T> s)
Returns an unmodifiable view of the given sorted set. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final Set EMPTY_SET
Serializable
public static final List EMPTY_LIST
Serializable
,
RandomAccess
public static final Map EMPTY_MAP
Serializable
Method Detail |
---|
public static final <T> Set<T> emptySet()
EMPTY_SET
, the set returned by
this method is type-safe.
public static final <T> List<T> emptyList()
EMPTY_LIST
, the list returned by
this method is type-safe.
public static final <K,V> Map<K,V> emptyMap()
EMPTY_MAP
, the map returned by
this method is type-safe.
public static <T> int binarySearch(List<? extends Comparable<? super T>> l, T key)
This algorithm behaves in log(n) time for RandomAccess
lists,
and uses a linear search with O(n) link traversals and log(n) comparisons
with AbstractSequentialList
lists. Note: although the
specification allows for an infinite loop if the list is unsorted, it will
not happen in this (Classpath) implementation.
l
- the list to search (must be sorted)key
- the value to search for
ClassCastException
- if key could not be compared with one of the
elements of l
NullPointerException
- if a null element has compareTo calledsort(List)
public static <T> int binarySearch(List<? extends T> l, T key, Comparator<? super T> c)
This algorithm behaves in log(n) time for RandomAccess
lists,
and uses a linear search with O(n) link traversals and log(n) comparisons
with AbstractSequentialList
lists. Note: although the
specification allows for an infinite loop if the list is unsorted, it will
not happen in this (Classpath) implementation.
l
- the list to search (must be sorted)key
- the value to search forc
- the comparator by which the list is sorted
ClassCastException
- if key could not be compared with one of the
elements of l
NullPointerException
- if a null element is compared with natural
ordering (only possible when c is null)sort(List, Comparator)
public static <T> void copy(List<? super T> dest, List<? extends T> source)
dest
- the destination listsource
- the source list
IndexOutOfBoundsException
- if the destination list is shorter
than the source list (the destination will be unmodified)
UnsupportedOperationException
- if dest.listIterator() does not
support the set operationpublic static <T> Enumeration<T> enumeration(Collection<T> c)
c
- the Collection to iterate over
public static <T> void fill(List<? super T> l, T val)
l
- the list to fill.val
- the object to vill the list with.
UnsupportedOperationException
- if l.listIterator() does not
support the set operation.public static int indexOfSubList(List<?> source, List<?> target)
target.size() > source.size()
, this returns -1,
otherwise this implementation uses brute force, checking for
source.sublist(i, i + target.size()).equals(target)
for all possible i.
source
- the list to searchtarget
- the sublist to search for
public static int lastIndexOfSubList(List<?> source, List<?> target)
target.size() > source.size()
, this returns -1,
otherwise this implementation uses brute force, checking for
source.sublist(i, i + target.size()).equals(target)
for all possible i.
source
- the list to searchtarget
- the sublist to search for
public static <T> ArrayList<T> list(Enumeration<T> e)
e
- the enumeration to put in a list
ArrayList
public static <T extends Object & Comparable<? super T>> T max(Collection<? extends T> c)
c
- the Collection to find the maximum element of
NoSuchElementException
- if c is empty
ClassCastException
- if elements in c are not mutually comparable
NullPointerException
- if null.compareTo is calledpublic static <T> T max(Collection<? extends T> c, Comparator<? super T> order)
c
- the Collection to find the maximum element oforder
- the Comparator to order the elements by, or null for natural
ordering
NoSuchElementException
- if c is empty
ClassCastException
- if elements in c are not mutually comparable
NullPointerException
- if null is compared by natural ordering
(only possible when order is null)public static <T extends Object & Comparable<? super T>> T min(Collection<? extends T> c)
c
- the Collection to find the minimum element of
NoSuchElementException
- if c is empty
ClassCastException
- if elements in c are not mutually comparable
NullPointerException
- if null.compareTo is calledpublic static <T> T min(Collection<? extends T> c, Comparator<? super T> order)
c
- the Collection to find the minimum element oforder
- the Comparator to order the elements by, or null for natural
ordering
NoSuchElementException
- if c is empty
ClassCastException
- if elements in c are not mutually comparable
NullPointerException
- if null is compared by natural ordering
(only possible when order is null)public static <T> List<T> nCopies(int n, T o)
n
- the number of times to repeat the objecto
- the object to repeat
IllegalArgumentException
- if n < 0List.addAll(Collection)
,
Serializable
,
RandomAccess
public static <T> boolean replaceAll(List<T> list, T oldval, T newval)
oldval == null ? e == null : oldval.equals(e)
.
list
- the list to iterate overoldval
- the element to replacenewval
- the new value for the element
true
if a replacement occurred.
UnsupportedOperationException
- if the list iterator does not allow
for the set operation
ClassCastException
- if newval is of a type which cannot be added
to the list
IllegalArgumentException
- if some other aspect of newval stops
it being added to the listpublic static void reverse(List<?> l)
l
- the list to reverse
UnsupportedOperationException
- if l.listIterator() does not
support the set operationpublic static <T> Comparator<T> reverseOrder(Comparator<T> c)
reverseOrder()
. The return value
of this method is Serializable, if the specified Comparator is
either Serializable or null.
c
- the comparator to invert
Comparable
,
Serializable
public static <T> Comparator<T> reverseOrder()
Comparable
,
Serializable
public static void rotate(List<?> list, int distance)
i
was formerly at index
(i - distance) mod list.size()
. The list size is unchanged.
For example, suppose a list contains [t, a, n, k, s]
. After
either Collections.rotate(l, 4)
or
Collections.rotate(l, -1)
, the new contents are
[s, t, a, n, k]
. This can be applied to sublists to rotate
just a portion of the list. For example, to move element a
forward two positions in the original example, use
Collections.rotate(l.subList(1, 3+1), -1)
, which will
result in [t, n, k, a, s]
.
If the list is small or implements RandomAccess
, the
implementation exchanges the first element to its destination, then the
displaced element, and so on until a circuit has been completed. The
process is repeated if needed on the second element, and so forth, until
all elements have been swapped. For large non-random lists, the
implementation breaks the list into two sublists at index
-distance mod size
, calls reverse(List)
on the
pieces, then reverses the overall list.
list
- the list to rotatedistance
- the distance to rotate by; unrestricted in value
UnsupportedOperationException
- if the list does not support setpublic static void shuffle(List<?> l)
This algorithm would result in a perfectly fair shuffle (that is, each element would have an equal chance of ending up in any position) if r were a perfect source of randomness. In practice the results are merely very close to perfect.
This method operates in linear time. To do this on large lists which do
not implement RandomAccess
, a temporary array is used to acheive
this speed, since it would be quadratic access otherwise.
l
- the list to shuffle
UnsupportedOperationException
- if l.listIterator() does not
support the set operationpublic static void shuffle(List<?> l, Random r)
This algorithm would result in a perfectly fair shuffle (that is, each element would have an equal chance of ending up in any position) if r were a perfect source of randomness. In practise (eg if r = new Random()) the results are merely very close to perfect.
This method operates in linear time. To do this on large lists which do
not implement RandomAccess
, a temporary array is used to acheive
this speed, since it would be quadratic access otherwise.
l
- the list to shuffler
- the source of randomness to use for the shuffle
UnsupportedOperationException
- if l.listIterator() does not
support the set operationpublic static int frequency(Collection<?> c, Object o)
true
when
compared with the object using the equals
method.
c
- the collection to scan for occurrences of the object.o
- the object to locate occurrances of within the collection.
NullPointerException
- if the collection is null
.public static <T> boolean addAll(Collection<? super T> c, T... a)
addAll
method of the Collection
.
However, this is a variable argument method which allows the new elements
to be specified individually or in array form, as opposed to the list
required by the collection's addAll
method. This has
benefits in both simplicity (multiple elements can be added without
having to be wrapped inside a grouping structure) and efficiency
(as a redundant list doesn't have to be created to add an individual
set of elements or an array).
c
- the collection to which the elements should be added.a
- the elements to be added to the collection.
UnsupportedOperationException
- if the collection does not support
addition.
NullPointerException
- if one or more elements in a are null,
and the collection does not allow null
elements. This exception is also thrown
if either c
or a
are null.
IllegalArgumentException
- if the collection won't allow an element
to be added for some other reason.public static boolean disjoint(Collection<?> c1, Collection<?> c2)
c1
- the first collection to compare.c2
- the second collection to compare.
NullPointerException
- if either collection is null.public static <T> Set<T> singleton(T o)
o
- the single element
Serializable
public static <T> List<T> singletonList(T o)
o
- the single element
Serializable
,
RandomAccess
public static <K,V> Map<K,V> singletonMap(K key, V value)
key
- the single keyvalue
- the single value
Serializable
public static <T extends Comparable<? super T>> void sort(List<T> l)
l
- the List to sort (null
not permitted)
ClassCastException
- if some items are not mutually comparable
UnsupportedOperationException
- if the List is not modifiable
NullPointerException
- if the list is null
, or contains
some element that is null
.Arrays.sort(Object[])
public static <T> void sort(List<T> l, Comparator<? super T> c)
l
- the List to sort (null
not permitted)c
- the Comparator specifying the ordering for the elements, or
null
for natural ordering
ClassCastException
- if c will not compare some pair of items
UnsupportedOperationException
- if the List is not modifiable
NullPointerException
- if the List is null
or
null
is compared by natural ordering (only possible
when c is null
)Arrays.sort(Object[], Comparator)
public static void swap(List<?> l, int i, int j)
l
- the list to work oni
- the first index to swapj
- the second index
UnsupportedOperationException
- if list.set is not supported
IndexOutOfBoundsException
- if either i or j is < 0 or >=
list.size()public static <T> Collection<T> synchronizedCollection(Collection<T> c)
Collection c = Collections.synchronizedCollection(new Collection(...)); ... synchronized (c) { Iterator i = c.iterator(); while (i.hasNext()) foo(i.next()); }
Since the collection might be a List or a Set, and those have incompatible equals and hashCode requirements, this relies on Object's implementation rather than passing those calls on to the wrapped collection. The returned Collection implements Serializable, but can only be serialized if the collection it wraps is likewise Serializable.
c
- the collection to wrap
Serializable
public static <T> List<T> synchronizedList(List<T> l)
List l = Collections.synchronizedList(new List(...)); ... synchronized (l) { Iterator i = l.iterator(); while (i.hasNext()) foo(i.next()); }
The returned List implements Serializable, but can only be serialized if the list it wraps is likewise Serializable. In addition, if the wrapped list implements RandomAccess, this does too.
l
- the list to wrap
Serializable
,
RandomAccess
public static <K,V> Map<K,V> synchronizedMap(Map<K,V> m)
Map m = Collections.synchronizedMap(new Map(...)); ... Set s = m.keySet(); // safe outside a synchronized block synchronized (m) // synch on m, not s { Iterator i = s.iterator(); while (i.hasNext()) foo(i.next()); }
The returned Map implements Serializable, but can only be serialized if the map it wraps is likewise Serializable.
m
- the map to wrap
Serializable
public static <T> Set<T> synchronizedSet(Set<T> s)
Set s = Collections.synchronizedSet(new Set(...)); ... synchronized (s) { Iterator i = s.iterator(); while (i.hasNext()) foo(i.next()); }
The returned Set implements Serializable, but can only be serialized if the set it wraps is likewise Serializable.
s
- the set to wrap
Serializable
public static <K,V> SortedMap<K,V> synchronizedSortedMap(SortedMap<K,V> m)
SortedMap m = Collections.synchronizedSortedMap(new SortedMap(...)); ... Set s = m.keySet(); // safe outside a synchronized block SortedMap m2 = m.headMap(foo); // safe outside a synchronized block Set s2 = m2.keySet(); // safe outside a synchronized block synchronized (m) // synch on m, not m2, s or s2 { Iterator i = s.iterator(); while (i.hasNext()) foo(i.next()); i = s2.iterator(); while (i.hasNext()) bar(i.next()); }
The returned SortedMap implements Serializable, but can only be serialized if the map it wraps is likewise Serializable.
m
- the sorted map to wrap
Serializable
public static <T> SortedSet<T> synchronizedSortedSet(SortedSet<T> s)
SortedSet s = Collections.synchronizedSortedSet(new SortedSet(...)); ... SortedSet s2 = s.headSet(foo); // safe outside a synchronized block synchronized (s) // synch on s, not s2 { Iterator i = s2.iterator(); while (i.hasNext()) foo(i.next()); }
The returned SortedSet implements Serializable, but can only be serialized if the set it wraps is likewise Serializable.
s
- the sorted set to wrap
Serializable
public static <T> Collection<T> unmodifiableCollection(Collection<? extends T> c)
UnsupportedOperationException
. Although this view
prevents changes to the structure of the collection and its elements, the values
referenced by the objects in the collection can still be modified.
Since the collection might be a List or a Set, and those have incompatible equals and hashCode requirements, this relies on Object's implementation rather than passing those calls on to the wrapped collection. The returned Collection implements Serializable, but can only be serialized if the collection it wraps is likewise Serializable.
c
- the collection to wrap
Serializable
public static <T> List<T> unmodifiableList(List<? extends T> l)
UnsupportedOperationException
.
Although this view prevents changes to the structure of the list and
its elements, the values referenced by the objects in the list can
still be modified.
The returned List implements Serializable, but can only be serialized if the list it wraps is likewise Serializable. In addition, if the wrapped list implements RandomAccess, this does too.
l
- the list to wrap
Serializable
,
RandomAccess
public static <K,V> Map<K,V> unmodifiableMap(Map<? extends K,? extends V> m)
UnsupportedOperationException
.
Although this view prevents changes to the structure of the map and its
entries, the values referenced by the objects in the map can still be
modified.
The returned Map implements Serializable, but can only be serialized if the map it wraps is likewise Serializable.
m
- the map to wrap
Serializable
public static <T> Set<T> unmodifiableSet(Set<? extends T> s)
UnsupportedOperationException
.
Although this view prevents changes to the structure of the set and its
entries, the values referenced by the objects in the set can still be
modified.
The returned Set implements Serializable, but can only be serialized if the set it wraps is likewise Serializable.
s
- the set to wrap
Serializable
public static <K,V> SortedMap<K,V> unmodifiableSortedMap(SortedMap<K,? extends V> m)
UnsupportedOperationException
.
Although this view prevents changes to the structure of the map and its
entries, the values referenced by the objects in the map can still be
modified.
The returned SortedMap implements Serializable, but can only be serialized if the map it wraps is likewise Serializable.
m
- the map to wrap
Serializable
public static <T> SortedSet<T> unmodifiableSortedSet(SortedSet<T> s)
UnsupportedOperationException
.
Although this view prevents changes to the structure of the set and its
entries, the values referenced by the objects in the set can still be
modified.
The returns SortedSet implements Serializable, but can only be serialized if the set it wraps is likewise Serializable.
s
- the set to wrap
Serializable
public static <E> Collection<E> checkedCollection(Collection<E> c, Class<E> type)
Returns a dynamically typesafe view of the given collection,
where any modification is first checked to ensure that the type
of the new data is appropriate. Although the addition of
generics and parametrically-typed collections prevents an
incorrect type of element being added to a collection at
compile-time, via static type checking, this can be overridden by
casting. In contrast, wrapping the collection within a
dynamically-typesafe wrapper, using this and associated methods,
ClassCastException
caused by erroneous casting, or
for protecting collections from corruption by external libraries.
Since the collection might be a List or a Set, and those have incompatible equals and hashCode requirements, this relies on Object's implementation rather than passing those calls on to the wrapped collection. The returned Collection implements Serializable, but can only be serialized if the collection it wraps is likewise Serializable.
c
- the collection to wrap in a dynamically typesafe wrappertype
- the type of elements the collection should hold.
Serializable
public static <E> List<E> checkedList(List<E> l, Class<E> type)
Returns a dynamically typesafe view of the given list,
where any modification is first checked to ensure that the type
of the new data is appropriate. Although the addition of
generics and parametrically-typed collections prevents an
incorrect type of element being added to a collection at
compile-time, via static type checking, this can be overridden by
casting. In contrast, wrapping the collection within a
dynamically-typesafe wrapper, using this and associated methods,
ClassCastException
caused by erroneous casting, or
for protecting collections from corruption by external libraries.
The returned List implements Serializable, but can only be serialized if the list it wraps is likewise Serializable. In addition, if the wrapped list implements RandomAccess, this does too.
l
- the list to wraptype
- the type of the elements within the checked list.
Serializable
,
RandomAccess
public static <K,V> Map<K,V> checkedMap(Map<K,V> m, Class<K> keyType, Class<V> valueType)
Returns a dynamically typesafe view of the given map,
where any modification is first checked to ensure that the type
of the new data is appropriate. Although the addition of
generics and parametrically-typed collections prevents an
incorrect type of element being added to a collection at
compile-time, via static type checking, this can be overridden by
casting. In contrast, wrapping the collection within a
dynamically-typesafe wrapper, using this and associated methods,
ClassCastException
caused by erroneous casting, or
for protecting collections from corruption by external libraries.
The returned Map implements Serializable, but can only be serialized if the map it wraps is likewise Serializable.
m
- the map to wrapkeyType
- the dynamic type of the map's keys.valueType
- the dynamic type of the map's values.
Serializable
public static <E> Set<E> checkedSet(Set<E> s, Class<E> type)
Returns a dynamically typesafe view of the given set,
where any modification is first checked to ensure that the type
of the new data is appropriate. Although the addition of
generics and parametrically-typed collections prevents an
incorrect type of element being added to a collection at
compile-time, via static type checking, this can be overridden by
casting. In contrast, wrapping the collection within a
dynamically-typesafe wrapper, using this and associated methods,
ClassCastException
caused by erroneous casting, or
for protecting collections from corruption by external libraries.
The returned Set implements Serializable, but can only be serialized if the set it wraps is likewise Serializable.
s
- the set to wrap.type
- the type of the elements within the checked list.
Serializable
public static <K,V> SortedMap<K,V> checkedSortedMap(SortedMap<K,V> m, Class<K> keyType, Class<V> valueType)
Returns a dynamically typesafe view of the given sorted map,
where any modification is first checked to ensure that the type
of the new data is appropriate. Although the addition of
generics and parametrically-typed collections prevents an
incorrect type of element being added to a collection at
compile-time, via static type checking, this can be overridden by
casting. In contrast, wrapping the collection within a
dynamically-typesafe wrapper, using this and associated methods,
ClassCastException
caused by erroneous casting, or
for protecting collections from corruption by external libraries.
The returned SortedMap implements Serializable, but can only be serialized if the map it wraps is likewise Serializable.
m
- the map to wrap.keyType
- the dynamic type of the map's keys.valueType
- the dynamic type of the map's values.
Serializable
public static <E> SortedSet<E> checkedSortedSet(SortedSet<E> s, Class<E> type)
Returns a dynamically typesafe view of the given sorted set,
where any modification is first checked to ensure that the type
of the new data is appropriate. Although the addition of
generics and parametrically-typed collections prevents an
incorrect type of element being added to a collection at
compile-time, via static type checking, this can be overridden by
casting. In contrast, wrapping the collection within a
dynamically-typesafe wrapper, using this and associated methods,
ClassCastException
caused by erroneous casting, or
for protecting collections from corruption by external libraries.
The returned SortedSet implements Serializable, but can only be serialized if the set it wraps is likewise Serializable.
s
- the set to wrap.type
- the type of the set's elements.
Serializable
public static <T> java.util.Queue<T> asLifoQueue(java.util.Deque<T> deque)
Deque
as a stack or LIFO (Last-In-First-Out)
Queue
. Each call to the LIFO queue corresponds to one
equivalent method call to the underlying deque, with the exception
of Collection.addAll(Collection)
, which is emulated by a series
of Deque#push(E)
calls.
deque
- the deque to convert to a LIFO queue.
public static <E> Set<E> newSetFromMap(Map<E,Boolean> map)
Set.addAll(Collection)
which is emulated by a series of
calls to put
.
map
- the map to convert to a set.
IllegalArgumentException
- if the map is not empty.
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |