| Method from java.util.Collections Detail: |
public static boolean addAll(Collection<? super T> c,
T a) {
boolean modified = false;
for (int i = 0; i < a.length; i++) {
modified |= c.add(a[i]);
}
return modified;
}
Adds all the specified elements to the specified collection. |
public static Queue<T> asLifoQueue(Deque<T> deque) {
return new AsLIFOQueue< T >(deque);
}
Answers a LIFO Queue as a view of a Deque. Methods in the returned Queue
need to be re-written to implement the LIFO feature. |
public static int binarySearch(List<Comparable> list,
T object) {
if (list == null) {
throw new NullPointerException();
}
if (list.isEmpty()) {
return -1;
}
if (!(list instanceof RandomAccess)) {
ListIterator< ? extends Comparable< ? super T > > it = list.listIterator();
while (it.hasNext()) {
int result;
if ((result = -it.next().compareTo(object)) < = 0) {
if (result == 0) {
return it.previousIndex();
}
return -it.previousIndex() - 1;
}
}
return -list.size() - 1;
}
int low = 0, mid = list.size(), high = mid - 1, result = -1;
while (low < = high) {
mid = (low + high) > > 1;
if ((result = -list.get(mid).compareTo(object)) > 0) {
low = mid + 1;
} else if (result == 0) {
return mid;
} else {
high = mid - 1;
}
}
return -mid - (result < 0 ? 1 : 2);
}
Performs a binary search for the specified element in the specified
sorted list. The list needs to be already sorted in natural sorting
order. Searching in an unsorted array has an undefined result. It's also
undefined which element is found if there are multiple occurrences of the
same element. |
public static int binarySearch(List<? extends T> list,
T object,
Comparator<? super T> comparator) {
if (comparator == null) {
return Collections.binarySearch(
(List< ? extends Comparable< ? super T > >) list, object);
}
if (!(list instanceof RandomAccess)) {
ListIterator< ? extends T > it = list.listIterator();
while (it.hasNext()) {
int result;
if ((result = -comparator.compare(it.next(), object)) < = 0) {
if (result == 0) {
return it.previousIndex();
}
return -it.previousIndex() - 1;
}
}
return -list.size() - 1;
}
int low = 0, mid = list.size(), high = mid - 1, result = -1;
while (low < = high) {
mid = (low + high) > > 1;
if ((result = -comparator.compare(list.get(mid),object)) > 0) {
low = mid + 1;
} else if (result == 0) {
return mid;
} else {
high = mid - 1;
}
}
return -mid - (result < 0 ? 1 : 2);
}
Performs a binary search for the specified element in the specified
sorted list using the specified comparator. The list needs to be already
sorted according to the comparator passed. Searching in an unsorted array
has an undefined result. It's also undefined which element is found if
there are multiple occurrences of the same element. |
static E checkType(E obj,
Class<? extends E> type) {
if (obj != null && !type.isInstance(obj)) {
// luni.05=Attempt to insert {0} element into collection with
// element type {1}
throw new ClassCastException(Messages.getString(
"luni.05", obj.getClass(), type)); //$NON-NLS-1$
}
return obj;
}
Checks if specified object is instance of specified class. Used for a
dynamically typesafe view of the collections. |
public static Collection<E> checkedCollection(Collection<E> c,
Class<E> type) {
return new CheckedCollection< E >(c, type);
}
Returns a dynamically typesafe view of the specified collection. Trying
to insert an element of the wrong type into this collection throws a
{@code ClassCastException}. At creation time the types in {@code c} are
not checked for correct type. |
public static List<E> checkedList(List<E> list,
Class<E> type) {
if (list instanceof RandomAccess) {
return new CheckedRandomAccessList< E >(list, type);
}
return new CheckedList< E >(list, type);
}
Returns a dynamically typesafe view of the specified list. Trying to
insert an element of the wrong type into this list throws a
{@code ClassCastException}. At creation time the types in {@code list}
are not checked for correct type. |
public static Map<K, V> checkedMap(Map<K, V> m,
Class<K> keyType,
Class<V> valueType) {
return new CheckedMap< K, V >(m, keyType, valueType);
}
Returns a dynamically typesafe view of the specified map. Trying to
insert an element of the wrong type into this map throws a
{@code ClassCastException}. At creation time the types in {@code m} are
not checked for correct type. |
public static Set<E> checkedSet(Set<E> s,
Class<E> type) {
return new CheckedSet< E >(s, type);
}
Returns a dynamically typesafe view of the specified set. Trying to
insert an element of the wrong type into this set throws a
{@code ClassCastException}. At creation time the types in {@code s} are
not checked for correct type. |
public static SortedMap<K, V> checkedSortedMap(SortedMap<K, V> m,
Class<K> keyType,
Class<V> valueType) {
return new CheckedSortedMap< K, V >(m, keyType, valueType);
}
Returns a dynamically typesafe view of the specified sorted map. Trying
to insert an element of the wrong type into this sorted map throws a
{@code ClassCastException}. At creation time the types in {@code m} are
not checked for correct type. |
public static SortedSet<E> checkedSortedSet(SortedSet<E> s,
Class<E> type) {
return new CheckedSortedSet< E >(s, type);
}
Returns a dynamically typesafe view of the specified sorted set. Trying
to insert an element of the wrong type into this sorted set throws a
{@code ClassCastException}. At creation time the types in {@code s} are
not checked for correct type. |
public static void copy(List<? super T> destination,
List<? extends T> source) {
if (destination.size() < source.size()) {
// luni.38=Source size {0} does not fit into destination
throw new ArrayIndexOutOfBoundsException(Messages.getString("luni.38", source.size())); //$NON-NLS-1$
}
Iterator< ? extends T > srcIt = source.iterator();
ListIterator< ? super T > destIt = destination.listIterator();
while (srcIt.hasNext()) {
try {
destIt.next();
} catch (NoSuchElementException e) {
// luni.38=Source size {0} does not fit into destination
throw new ArrayIndexOutOfBoundsException(Messages.getString("luni.38", source.size())); //$NON-NLS-1$
}
destIt.set(srcIt.next());
}
}
Copies the elements from the source list to the destination list. At the
end both lists will have the same objects at the same index. If the
destination array is larger than the source list, the elements in the
destination list with {@code index >= source.size()} will be unchanged. |
public static boolean disjoint(Collection<?> c1,
Collection<?> c2) {
if ((c1 instanceof Set) && !(c2 instanceof Set)
|| (c2.size()) > c1.size()) {
Collection< ? > tmp = c1;
c1 = c2;
c2 = tmp;
}
Iterator< ? > it = c1.iterator();
while (it.hasNext()) {
if (c2.contains(it.next())) {
return false;
}
}
return true;
}
Returns whether the specified collections have no elements in common. |
public static final List<T> emptyList() {
return EMPTY_LIST;
}
Returns a type-safe empty, immutable List . |
public static final Map<K, V> emptyMap() {
return EMPTY_MAP;
}
Returns a type-safe empty, immutable Map . |
public static final Set<T> emptySet() {
return EMPTY_SET;
}
Returns a type-safe empty, immutable Set . |
public static Enumeration<T> enumeration(Collection<T> collection) {
final Collection< T > c = collection;
return new Enumeration< T >() {
Iterator< T > it = c.iterator();
public boolean hasMoreElements() {
return it.hasNext();
}
public T nextElement() {
return it.next();
}
};
}
Returns an {@code Enumeration} on the specified collection. |
public static void fill(List<? super T> list,
T object) {
ListIterator< ? super T > it = list.listIterator();
while (it.hasNext()) {
it.next();
it.set(object);
}
}
Fills the specified list with the specified element. |
public static int frequency(Collection<?> c,
Object o) {
if (c == null) {
throw new NullPointerException();
}
if (c.isEmpty()) {
return 0;
}
int result = 0;
Iterator< ? > itr = c.iterator();
while (itr.hasNext()) {
Object e = itr.next();
if (o == null ? e == null : o.equals(e)) {
result++;
}
}
return result;
}
Returns the number of elements in the {@code Collection} that match the
{@code Object} passed. If the {@code Object} is {@code null}, then the
number of {@code null} elements is returned. |
public static int indexOfSubList(List<?> list,
List<?> sublist) {
int size = list.size();
int sublistSize = sublist.size();
if (sublistSize > size) {
return -1;
}
if (sublistSize == 0) {
return 0;
}
// find the first element of sublist in the list to get a head start
Object firstObj = sublist.get(0);
int index = list.indexOf(firstObj);
if (index == -1) {
return -1;
}
while (index < size && (size - index >= sublistSize)) {
ListIterator< ? > listIt = list.listIterator(index);
if ((firstObj == null) ? listIt.next() == null : firstObj
.equals(listIt.next())) {
// iterate through the elements in sublist to see
// if they are included in the same order in the list
ListIterator< ? > sublistIt = sublist.listIterator(1);
boolean difFound = false;
while (sublistIt.hasNext()) {
Object element = sublistIt.next();
if (!listIt.hasNext()) {
return -1;
}
if ((element == null) ? listIt.next() != null : !element
.equals(listIt.next())) {
difFound = true;
break;
}
}
// All elements of sublist are found in main list
// starting from index.
if (!difFound) {
return index;
}
}
// This was not the sequence we were looking for,
// continue search for the firstObj in main list
// at the position after index.
index++;
}
return -1;
}
|
public static int lastIndexOfSubList(List<?> list,
List<?> sublist) {
int sublistSize = sublist.size();
int size = list.size();
if (sublistSize > size) {
return -1;
}
if (sublistSize == 0) {
return size;
}
// find the last element of sublist in the list to get a head start
Object lastObj = sublist.get(sublistSize - 1);
int index = list.lastIndexOf(lastObj);
while ((index > -1) && (index + 1 >= sublistSize)) {
ListIterator< ? > listIt = list.listIterator(index + 1);
if ((lastObj == null) ? listIt.previous() == null : lastObj
.equals(listIt.previous())) {
// iterate through the elements in sublist to see
// if they are included in the same order in the list
ListIterator< ? > sublistIt = sublist
.listIterator(sublistSize - 1);
boolean difFound = false;
while (sublistIt.hasPrevious()) {
Object element = sublistIt.previous();
if (!listIt.hasPrevious()) {
return -1;
}
if ((element == null) ? listIt.previous() != null
: !element.equals(listIt.previous())) {
difFound = true;
break;
}
}
// All elements of sublist are found in main list
// starting from listIt.nextIndex().
if (!difFound) {
return listIt.nextIndex();
}
}
// This was not the sequence we were looking for,
// continue search for the lastObj in main list
// at the position before index.
index--;
}
return -1;
}
|
public static ArrayList<T> list(Enumeration<T> enumeration) {
ArrayList< T > list = new ArrayList< T >();
while (enumeration.hasMoreElements()) {
list.add(enumeration.nextElement());
}
return list;
}
Returns an {@code ArrayList} with all the elements in the {@code
enumeration}. The elements in the returned {@code ArrayList} are in the
same order as in the {@code enumeration}. |
public static T max(Collection<? extends T> collection) {
Iterator< ? extends T > it = collection.iterator();
T max = it.next();
while (it.hasNext()) {
T next = it.next();
if (max.compareTo(next) < 0) {
max = next;
}
}
return max;
}
Searches the specified collection for the maximum element. |
public static T max(Collection<? extends T> collection,
Comparator<? super T> comparator) {
if (comparator == null) {
@SuppressWarnings("unchecked") // null comparator? T is comparable
T result = (T) max((Collection< Comparable >) collection);
return result;
}
Iterator< ? extends T > it = collection.iterator();
T max = it.next();
while (it.hasNext()) {
T next = it.next();
if (comparator.compare(max, next) < 0) {
max = next;
}
}
return max;
}
Searches the specified collection for the maximum element using the
specified comparator. |
public static T min(Collection<? extends T> collection) {
Iterator< ? extends T > it = collection.iterator();
T min = it.next();
while (it.hasNext()) {
T next = it.next();
if (min.compareTo(next) > 0) {
min = next;
}
}
return min;
}
Searches the specified collection for the minimum element. |
public static T min(Collection<? extends T> collection,
Comparator<? super T> comparator) {
if (comparator == null) {
@SuppressWarnings("unchecked") // null comparator? T is comparable
T result = (T) min((Collection< Comparable >) collection);
return result;
}
Iterator< ? extends T > it = collection.iterator();
T min = it.next();
while (it.hasNext()) {
T next = it.next();
if (comparator.compare(min, next) > 0) {
min = next;
}
}
return min;
}
Searches the specified collection for the minimum element using the
specified comparator. |
public static List<T> nCopies(int length,
T object) {
return new CopiesList< T >(length, object);
}
Returns a list containing the specified number of the specified element.
The list cannot be modified. The list is serializable. |
public static Set<E> newSetFromMap(Map<E, Boolean> map) {
if (map.isEmpty()) {
return new SetFromMap< E >(map);
}
throw new IllegalArgumentException();
}
Answers a set backed by a map. And the map must be empty when this method
is called. |
public static boolean replaceAll(List<T> list,
T obj,
T obj2) {
int index;
boolean found = false;
while ((index = list.indexOf(obj)) > -1) {
found = true;
list.set(index, obj2);
}
return found;
}
Replaces all occurrences of Object {@code obj} in {@code list} with
{@code newObj}. If the {@code obj} is {@code null}, then all
occurrences of {@code null} are replaced with {@code newObj}. |
public static void reverse(List<?> list) {
int size = list.size();
ListIterator< Object > front = (ListIterator< Object >) list.listIterator();
ListIterator< Object > back = (ListIterator< Object >) list
.listIterator(size);
for (int i = 0; i < size / 2; i++) {
Object frontNext = front.next();
Object backPrev = back.previous();
front.set(backPrev);
back.set(frontNext);
}
}
Modifies the specified {@code List} by reversing the order of the
elements. |
public static Comparator<T> reverseOrder() {
return (Comparator) ReverseComparator.INSTANCE;
}
A comparator which reverses the natural order of the elements. The
{@code Comparator} that's returned is Serializable . |
public static Comparator<T> reverseOrder(Comparator<T> c) {
if (c == null) {
return reverseOrder();
}
if (c instanceof ReverseComparatorWithComparator) {
return ((ReverseComparatorWithComparator< T >) c).comparator;
}
return new ReverseComparatorWithComparator< T >(c);
}
Returns a Comparator that reverses the order of the
{@code Comparator} passed. If the {@code Comparator} passed is
{@code null}, then this method is equivalent to #reverseOrder() .
The {@code Comparator} that's returned is Serializable if the
{@code Comparator} passed is serializable or {@code null}. |
public static void rotate(List<?> lst,
int dist) {
List< Object > list = (List< Object >) lst;
int size = list.size();
// Can't sensibly rotate an empty collection
if (size == 0) {
return;
}
// normalize the distance
int normdist;
if (dist > 0) {
normdist = dist % size;
} else {
normdist = size - ((dist % size) * (-1));
}
if (normdist == 0 || normdist == size) {
return;
}
if (list instanceof RandomAccess) {
// make sure each element gets juggled
// with the element in the position it is supposed to go to
Object temp = list.get(0);
int index = 0, beginIndex = 0;
for (int i = 0; i < size; i++) {
index = (index + normdist) % size;
temp = list.set(index, temp);
if (index == beginIndex) {
index = ++beginIndex;
temp = list.get(beginIndex);
}
}
} else {
int divideIndex = (size - normdist) % size;
List< Object > sublist1 = list.subList(0, divideIndex);
List< Object > sublist2 = list.subList(divideIndex, size);
reverse(sublist1);
reverse(sublist2);
reverse(list);
}
}
Rotates the elements in {@code list} by the distance {@code dist}
e.g. for a given list with elements [1, 2, 3, 4, 5, 6, 7, 8, 9, 0],
calling rotate(list, 3) or rotate(list, -7) would modify the list to look
like this: [8, 9, 0, 1, 2, 3, 4, 5, 6, 7] |
public static void shuffle(List<?> list) {
shuffle(list, new Random());
}
Moves every element of the list to a random new position in the list. |
public static void shuffle(List<?> list,
Random random) {
@SuppressWarnings("unchecked") // we won't put foreign objects in
final List< Object > objectList = (List< Object >) list;
if (list instanceof RandomAccess) {
for (int i = objectList.size() - 1; i > 0; i--) {
int index = random.nextInt(i + 1);
objectList.set(index, objectList.set(i, objectList.get(index)));
}
} else {
Object[] array = objectList.toArray();
for (int i = array.length - 1; i > 0; i--) {
int index = random.nextInt(i + 1);
Object temp = array[i];
array[i] = array[index];
array[index] = temp;
}
int i = 0;
ListIterator< Object > it = objectList.listIterator();
while (it.hasNext()) {
it.next();
it.set(array[i++]);
}
}
}
Moves every element of the list to a random new position in the list
using the specified random number generator. |
public static Set<E> singleton(E object) {
return new SingletonSet< E >(object);
}
Returns a set containing the specified element. The set cannot be
modified. The set is serializable. |
public static List<E> singletonList(E object) {
return new SingletonList< E >(object);
}
Returns a list containing the specified element. The list cannot be
modified. The list is serializable. |
public static Map<K, V> singletonMap(K key,
V value) {
return new SingletonMap< K, V >(key, value);
}
Returns a Map containing the specified key and value. The map cannot be
modified. The map is serializable. |
public static void sort(List<T> list) {
Object[] array = list.toArray();
Arrays.sort(array);
int i = 0;
ListIterator< T > it = list.listIterator();
while (it.hasNext()) {
it.next();
it.set((T) array[i++]);
}
}
Sorts the specified list in ascending natural order. The algorithm is
stable which means equal elements don't get reordered. |
public static void sort(List<T> list,
Comparator<? super T> comparator) {
T[] array = list.toArray((T[]) new Object[list.size()]);
Arrays.sort(array, comparator);
int i = 0;
ListIterator< T > it = list.listIterator();
while (it.hasNext()) {
it.next();
it.set(array[i++]);
}
}
Sorts the specified list using the specified comparator. The algorithm is
stable which means equal elements don't get reordered. |
public static void swap(List<?> list,
int index1,
int index2) {
if (list == null) {
throw new NullPointerException();
}
final int size = list.size();
if (index1 < 0 || index1 >= size || index2 < 0 || index2 >= size) {
throw new IndexOutOfBoundsException();
}
if (index1 == index2) {
return;
}
List< Object > rawList = (List< Object >) list;
rawList.set(index2, rawList.set(index1, rawList.get(index2)));
}
Swaps the elements of list {@code list} at indices {@code index1} and
{@code index2}. |
public static Collection<T> synchronizedCollection(Collection<T> collection) {
if (collection == null) {
throw new NullPointerException();
}
return new SynchronizedCollection< T >(collection);
}
Returns a wrapper on the specified collection which synchronizes all
access to the collection. |
public static List<T> synchronizedList(List<T> list) {
if (list == null) {
throw new NullPointerException();
}
if (list instanceof RandomAccess) {
return new SynchronizedRandomAccessList< T >(list);
}
return new SynchronizedList< T >(list);
}
Returns a wrapper on the specified List which synchronizes all access to
the List. |
public static Map<K, V> synchronizedMap(Map<K, V> map) {
if (map == null) {
throw new NullPointerException();
}
return new SynchronizedMap< K, V >(map);
}
Returns a wrapper on the specified map which synchronizes all access to
the map. |
public static Set<E> synchronizedSet(Set<E> set) {
if (set == null) {
throw new NullPointerException();
}
return new SynchronizedSet< E >(set);
}
Returns a wrapper on the specified set which synchronizes all access to
the set. |
public static SortedMap<K, V> synchronizedSortedMap(SortedMap<K, V> map) {
if (map == null) {
throw new NullPointerException();
}
return new SynchronizedSortedMap< K, V >(map);
}
Returns a wrapper on the specified sorted map which synchronizes all
access to the sorted map. |
public static SortedSet<E> synchronizedSortedSet(SortedSet<E> set) {
if (set == null) {
throw new NullPointerException();
}
return new SynchronizedSortedSet< E >(set);
}
Returns a wrapper on the specified sorted set which synchronizes all
access to the sorted set. |
public static Collection<E> unmodifiableCollection(Collection<? extends E> collection) {
if (collection == null) {
throw new NullPointerException();
}
return new UnmodifiableCollection< E >((Collection< E >) collection);
}
Returns a wrapper on the specified collection which throws an
{@code UnsupportedOperationException} whenever an attempt is made to
modify the collection. |
public static List<E> unmodifiableList(List<? extends E> list) {
if (list == null) {
throw new NullPointerException();
}
if (list instanceof RandomAccess) {
return new UnmodifiableRandomAccessList< E >((List< E >) list);
}
return new UnmodifiableList< E >((List< E >) list);
}
Returns a wrapper on the specified list which throws an
{@code UnsupportedOperationException} whenever an attempt is made to
modify the list. |
public static Map<K, V> unmodifiableMap(Map<? extends K, ? extends V> map) {
if (map == null) {
throw new NullPointerException();
}
return new UnmodifiableMap< K, V >((Map< K, V >) map);
}
Returns a wrapper on the specified map which throws an
{@code UnsupportedOperationException} whenever an attempt is made to
modify the map. |
public static Set<E> unmodifiableSet(Set<? extends E> set) {
if (set == null) {
throw new NullPointerException();
}
return new UnmodifiableSet< E >((Set< E >) set);
}
Returns a wrapper on the specified set which throws an
{@code UnsupportedOperationException} whenever an attempt is made to
modify the set. |
public static SortedMap<K, V> unmodifiableSortedMap(SortedMap<K, ? extends V> map) {
if (map == null) {
throw new NullPointerException();
}
return new UnmodifiableSortedMap< K, V >((SortedMap< K, V >) map);
}
Returns a wrapper on the specified sorted map which throws an
{@code UnsupportedOperationException} whenever an attempt is made to
modify the sorted map. |
public static SortedSet<E> unmodifiableSortedSet(SortedSet<E> set) {
if (set == null) {
throw new NullPointerException();
}
return new UnmodifiableSortedSet< E >(set);
}
Returns a wrapper on the specified sorted set which throws an
{@code UnsupportedOperationException} whenever an attempt is made to
modify the sorted set. |