public class Collections4D
extends java.lang.Object
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
Modifier and Type | Field and Description |
---|---|
static java.util.List |
EMPTY_LIST
An immutable, serializable, empty List, which implements RandomAccess.
|
static java.util.Map |
EMPTY_MAP
An immutable, serializable, empty Map.
|
static java.util.Set |
EMPTY_SET
An immutable, serializable, empty Set.
|
Modifier and Type | Method and Description |
---|---|
static <T> boolean |
addAll(java.util.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 <T> java.util.Queue<T> |
asLifoQueue(java.util.Deque<T> deque)
Returns a view of a
Deque as a stack or LIFO (Last-In-First-Out)
Queue . |
static <T> int |
binarySearch(java.util.List<? extends java.lang.Comparable<? super T>> l,
T key)
Perform a binary search of a List for a key, using the natural ordering of
the elements.
|
static <T> int |
binarySearch(java.util.List<? extends T> l,
T key,
java.util.Comparator<? super T> c)
Perform a binary search of a List for a key, using a supplied Comparator.
|
static <E> java.util.Collection<E> |
checkedCollection(java.util.Collection<E> c,
java.lang.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 <E> java.util.List<E> |
checkedList(java.util.List<E> l,
java.lang.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 <K,V> java.util.Map<K,V> |
checkedMap(java.util.Map<K,V> m,
java.lang.Class<K> keyType,
java.lang.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 <E> java.util.Set<E> |
checkedSet(java.util.Set<E> s,
java.lang.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 <K,V> java.util.SortedMap<K,V> |
checkedSortedMap(java.util.SortedMap<K,V> m,
java.lang.Class<K> keyType,
java.lang.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 <E> java.util.SortedSet<E> |
checkedSortedSet(java.util.SortedSet<E> s,
java.lang.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 <T> void |
copy(java.util.List<? super T> dest,
java.util.List<? extends T> source)
Copy one list to another.
|
static boolean |
disjoint(java.util.Collection<?> c1,
java.util.Collection<?> c2)
Returns true if the two specified collections have no elements in
common.
|
static <T> java.util.List<T> |
emptyList()
Returns an immutable, serializable parameterized empty list.
|
static <K,V> java.util.Map<K,V> |
emptyMap()
Returns an immutable, serializable parameterized empty map.
|
static <T> java.util.Set<T> |
emptySet()
Returns an immutable, serializable parameterized empty set.
|
static <T> java.util.Enumeration<T> |
enumeration(java.util.Collection<T> c)
Returns an Enumeration over a collection.
|
static <T> void |
fill(java.util.List<? super T> l,
T val)
Replace every element of a list with a given value.
|
static int |
frequency(java.util.Collection<?> c,
java.lang.Object o)
Returns the frequency of the specified object within the supplied
collection.
|
static int |
indexOfSubList(java.util.List<?> source,
java.util.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(java.util.List<?> source,
java.util.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 <T> java.util.ArrayList<T> |
list(java.util.Enumeration<T> e)
Returns an ArrayList holding the elements visited by a given
Enumeration.
|
static <T extends java.lang.Object & java.lang.Comparable<? super T>> |
max(java.util.Collection<? extends T> c)
Find the maximum element in a Collection, according to the natural
ordering of the elements.
|
static <T> T |
max(java.util.Collection<? extends T> c,
java.util.Comparator<? super T> order)
Find the maximum element in a Collection, according to a specified
Comparator.
|
static <T extends java.lang.Object & java.lang.Comparable<? super T>> |
min(java.util.Collection<? extends T> c)
Find the minimum element in a Collection, according to the natural
ordering of the elements.
|
static <T> T |
min(java.util.Collection<? extends T> c,
java.util.Comparator<? super T> order)
Find the minimum element in a Collection, according to a specified
Comparator.
|
static <T> java.util.List<T> |
nCopies(int n,
T o)
Creates an immutable list consisting of the same object repeated n times.
|
static <E> java.util.Set<E> |
newSetFromMap(java.util.Map<E,java.lang.Boolean> map)
Returns a set backed by the supplied map.
|
static <T> boolean |
replaceAll(java.util.List<T> list,
T oldval,
T newval)
Replace all instances of one object with another in the specified list.
|
static void |
reverse(java.util.List<?> l)
Reverse a given list.
|
static <T> java.util.Comparator<T> |
reverseOrder()
Get a comparator that implements the reverse of natural ordering.
|
static <T> java.util.Comparator<T> |
reverseOrder(java.util.Comparator<T> c)
Get a comparator that implements the reverse of the ordering
specified by the given Comparator.
|
static void |
rotate(java.util.List<?> list,
int distance)
Rotate the elements in a list by a specified distance.
|
static void |
shuffle(java.util.List<?> l)
Shuffle a list according to a default source of randomness.
|
static void |
shuffle(java.util.List<?> l,
java.util.Random r)
Shuffle a list according to a given source of randomness.
|
static <T> java.util.Set<T> |
singleton(T o)
Obtain an immutable Set consisting of a single element.
|
static <T> java.util.List<T> |
singletonList(T o)
Obtain an immutable List consisting of a single element.
|
static <K,V> java.util.Map<K,V> |
singletonMap(K key,
V value)
Obtain an immutable Map consisting of a single key-value pair.
|
static <T extends java.lang.Comparable<? super T>> |
sort(java.util.List<T> l)
Sort a list according to the natural ordering of its elements.
|
static <T> void |
sort(java.util.List<T> l,
java.util.Comparator<? super T> c)
Sort a list according to a specified Comparator.
|
static void |
swap(java.util.List<?> l,
int i,
int j)
Swaps the elements at the specified positions within the list.
|
static <T> java.util.Collection<T> |
synchronizedCollection(java.util.Collection<T> c)
Returns a synchronized (thread-safe) collection wrapper backed by the
given collection.
|
static <T> java.util.List<T> |
synchronizedList(java.util.List<T> l)
Returns a synchronized (thread-safe) list wrapper backed by the
given list.
|
static <K,V> java.util.Map<K,V> |
synchronizedMap(java.util.Map<K,V> m)
Returns a synchronized (thread-safe) map wrapper backed by the given
map.
|
static <T> java.util.Set<T> |
synchronizedSet(java.util.Set<T> s)
Returns a synchronized (thread-safe) set wrapper backed by the given
set.
|
static <K,V> java.util.SortedMap<K,V> |
synchronizedSortedMap(java.util.SortedMap<K,V> m)
Returns a synchronized (thread-safe) sorted map wrapper backed by the
given map.
|
static <T> java.util.SortedSet<T> |
synchronizedSortedSet(java.util.SortedSet<T> s)
Returns a synchronized (thread-safe) sorted set wrapper backed by the
given set.
|
static <T> java.util.Collection<T> |
unmodifiableCollection(java.util.Collection<? extends T> c)
Returns an unmodifiable view of the given collection.
|
static <T> java.util.List<T> |
unmodifiableList(java.util.List<? extends T> l)
Returns an unmodifiable view of the given list.
|
static <K,V> java.util.Map<K,V> |
unmodifiableMap(java.util.Map<? extends K,? extends V> m)
Returns an unmodifiable view of the given map.
|
static <T> java.util.Set<T> |
unmodifiableSet(java.util.Set<? extends T> s)
Returns an unmodifiable view of the given set.
|
static <K,V> java.util.SortedMap<K,V> |
unmodifiableSortedMap(java.util.SortedMap<K,? extends V> m)
Returns an unmodifiable view of the given sorted map.
|
static <T> java.util.SortedSet<T> |
unmodifiableSortedSet(java.util.SortedSet<T> s)
Returns an unmodifiable view of the given sorted set.
|
public static final java.util.Set EMPTY_SET
public static final java.util.List EMPTY_LIST
RandomAccess
public static final java.util.Map EMPTY_MAP
public static final <T> java.util.Set<T> emptySet()
EMPTY_SET
, the set returned by
this method is type-safe.public static final <T> java.util.List<T> emptyList()
EMPTY_LIST
, the list returned by
this method is type-safe.public static final <K,V> java.util.Map<K,V> emptyMap()
EMPTY_MAP
, the map returned by
this method is type-safe.public static <T> int binarySearch(java.util.List<? extends java.lang.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 forjava.lang.ClassCastException
- if key could not be compared with one of the
elements of ljava.lang.NullPointerException
- if a null element has compareTo calledsort(List)
public static <T> int binarySearch(java.util.List<? extends T> l, T key, java.util.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 sortedjava.lang.ClassCastException
- if key could not be compared with one of the
elements of ljava.lang.NullPointerException
- if a null element is compared with natural
ordering (only possible when c is null)sort(List, Comparator)
public static <T> void copy(java.util.List<? super T> dest, java.util.List<? extends T> source)
dest
- the destination listsource
- the source listjava.lang.IndexOutOfBoundsException
- if the destination list is shorter
than the source list (the destination will be unmodified)java.lang.UnsupportedOperationException
- if dest.listIterator() does not
support the set operationpublic static <T> java.util.Enumeration<T> enumeration(java.util.Collection<T> c)
c
- the Collection to iterate overpublic static <T> void fill(java.util.List<? super T> l, T val)
l
- the list to fill.val
- the object to vill the list with.java.lang.UnsupportedOperationException
- if l.listIterator() does not
support the set operation.public static int indexOfSubList(java.util.List<?> source, java.util.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 forpublic static int lastIndexOfSubList(java.util.List<?> source, java.util.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 forpublic static <T> java.util.ArrayList<T> list(java.util.Enumeration<T> e)
e
- the enumeration to put in a listArrayList
public static <T extends java.lang.Object & java.lang.Comparable<? super T>> T max(java.util.Collection<? extends T> c)
c
- the Collection to find the maximum element ofjava.util.NoSuchElementException
- if c is emptyjava.lang.ClassCastException
- if elements in c are not mutually comparablejava.lang.NullPointerException
- if null.compareTo is calledpublic static <T> T max(java.util.Collection<? extends T> c, java.util.Comparator<? super T> order)
c
- the Collection to find the maximum element oforder
- the Comparator to order the elements by, or null for natural
orderingjava.util.NoSuchElementException
- if c is emptyjava.lang.ClassCastException
- if elements in c are not mutually comparablejava.lang.NullPointerException
- if null is compared by natural ordering
(only possible when order is null)public static <T extends java.lang.Object & java.lang.Comparable<? super T>> T min(java.util.Collection<? extends T> c)
c
- the Collection to find the minimum element ofjava.util.NoSuchElementException
- if c is emptyjava.lang.ClassCastException
- if elements in c are not mutually comparablejava.lang.NullPointerException
- if null.compareTo is calledpublic static <T> T min(java.util.Collection<? extends T> c, java.util.Comparator<? super T> order)
c
- the Collection to find the minimum element oforder
- the Comparator to order the elements by, or null for natural
orderingjava.util.NoSuchElementException
- if c is emptyjava.lang.ClassCastException
- if elements in c are not mutually comparablejava.lang.NullPointerException
- if null is compared by natural ordering
(only possible when order is null)public static <T> java.util.List<T> nCopies(int n, T o)
n
- the number of times to repeat the objecto
- the object to repeatjava.lang.IllegalArgumentException
- if n < 0List.addAll(Collection)
,
RandomAccess
public static <T> boolean replaceAll(java.util.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 elementtrue
if a replacement occurred.java.lang.UnsupportedOperationException
- if the list iterator does not allow
for the set operationjava.lang.ClassCastException
- if newval is of a type which cannot be added
to the listjava.lang.IllegalArgumentException
- if some other aspect of newval stops
it being added to the listpublic static void reverse(java.util.List<?> l)
l
- the list to reversejava.lang.UnsupportedOperationException
- if l.listIterator() does not
support the set operationpublic static <T> java.util.Comparator<T> reverseOrder(java.util.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 invertComparable
public static <T> java.util.Comparator<T> reverseOrder()
Comparable
public static void rotate(java.util.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 valuejava.lang.UnsupportedOperationException
- if the list does not support setpublic static void shuffle(java.util.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 shufflejava.lang.UnsupportedOperationException
- if l.listIterator() does not
support the set operationpublic static void shuffle(java.util.List<?> l, java.util.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 shufflejava.lang.UnsupportedOperationException
- if l.listIterator() does not
support the set operationpublic static int frequency(java.util.Collection<?> c, java.lang.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.java.lang.NullPointerException
- if the collection is null
.public static <T> boolean addAll(java.util.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.java.lang.UnsupportedOperationException
- if the collection does not support
addition.java.lang.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.java.lang.IllegalArgumentException
- if the collection won't allow an element
to be added for some other reason.public static boolean disjoint(java.util.Collection<?> c1, java.util.Collection<?> c2)
c1
- the first collection to compare.c2
- the second collection to compare.java.lang.NullPointerException
- if either collection is null.public static <T> java.util.Set<T> singleton(T o)
o
- the single elementpublic static <T> java.util.List<T> singletonList(T o)
o
- the single elementRandomAccess
public static <K,V> java.util.Map<K,V> singletonMap(K key, V value)
key
- the single keyvalue
- the single valuepublic static <T extends java.lang.Comparable<? super T>> void sort(java.util.List<T> l)
l
- the List to sort (null
not permitted)java.lang.ClassCastException
- if some items are not mutually comparablejava.lang.UnsupportedOperationException
- if the List is not modifiablejava.lang.NullPointerException
- if the list is null
, or contains
some element that is null
.Arrays.sort(Object[])
public static <T> void sort(java.util.List<T> l, java.util.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 orderingjava.lang.ClassCastException
- if c will not compare some pair of itemsjava.lang.UnsupportedOperationException
- if the List is not modifiablejava.lang.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(java.util.List<?> l, int i, int j)
l
- the list to work oni
- the first index to swapj
- the second indexjava.lang.UnsupportedOperationException
- if list.set is not supportedjava.lang.IndexOutOfBoundsException
- if either i or j is < 0 or >=
list.size()public static <T> java.util.Collection<T> synchronizedCollection(java.util.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 wrappublic static <T> java.util.List<T> synchronizedList(java.util.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 wrapRandomAccess
public static <K,V> java.util.Map<K,V> synchronizedMap(java.util.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 wrappublic static <T> java.util.Set<T> synchronizedSet(java.util.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 wrappublic static <K,V> java.util.SortedMap<K,V> synchronizedSortedMap(java.util.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 wrappublic static <T> java.util.SortedSet<T> synchronizedSortedSet(java.util.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 wrappublic static <T> java.util.Collection<T> unmodifiableCollection(java.util.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 wrappublic static <T> java.util.List<T> unmodifiableList(java.util.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 wrapRandomAccess
public static <K,V> java.util.Map<K,V> unmodifiableMap(java.util.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 wrappublic static <T> java.util.Set<T> unmodifiableSet(java.util.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 wrappublic static <K,V> java.util.SortedMap<K,V> unmodifiableSortedMap(java.util.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 wrappublic static <T> java.util.SortedSet<T> unmodifiableSortedSet(java.util.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 wrappublic static <E> java.util.Collection<E> checkedCollection(java.util.Collection<E> c, java.lang.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.public static <E> java.util.List<E> checkedList(java.util.List<E> l, java.lang.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.RandomAccess
public static <K,V> java.util.Map<K,V> checkedMap(java.util.Map<K,V> m, java.lang.Class<K> keyType, java.lang.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.public static <E> java.util.Set<E> checkedSet(java.util.Set<E> s, java.lang.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.public static <K,V> java.util.SortedMap<K,V> checkedSortedMap(java.util.SortedMap<K,V> m, java.lang.Class<K> keyType, java.lang.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.public static <E> java.util.SortedSet<E> checkedSortedSet(java.util.SortedSet<E> s, java.lang.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.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> java.util.Set<E> newSetFromMap(java.util.Map<E,java.lang.Boolean> map)
Set.addAll(Collection)
which is emulated by a series of
calls to put
.map
- the map to convert to a set.java.lang.IllegalArgumentException
- if the map is not empty.