Methods Summary |
---|
public static boolean | addAll(java.util.Collection c, T a)Adds all the specified elements to the specified collection.
boolean modified = false;
for (int i = 0; i < a.length; i++) {
modified |= c.add(a[i]);
}
return modified;
|
public static int | binarySearch(java.util.List list, T object)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.
if (list == null) {
throw new NullPointerException();
}
if (list.isEmpty()) {
return -1;
}
Comparable<T> key = (Comparable<T>) object;
if (!(list instanceof RandomAccess)) {
ListIterator<T> it = (ListIterator<T>) list.listIterator();
while (it.hasNext()) {
int result;
if ((result = key.compareTo(it.next())) <= 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 = key.compareTo((T) list.get(mid))) > 0) {
low = mid + 1;
} else if (result == 0) {
return mid;
} else {
high = mid - 1;
}
}
return -mid - (result < 0 ? 1 : 2);
|
public static int | binarySearch(java.util.List list, T object, java.util.Comparator comparator)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.
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(object, it.next())) <= 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(object, list.get(mid))) > 0) {
low = mid + 1;
} else if (result == 0) {
return mid;
} else {
high = mid - 1;
}
}
return -mid - (result < 0 ? 1 : 2);
|
static E | checkType(E obj, java.lang.Class type)Checks if specified object is instance of specified class. Used for a
dynamically typesafe view of the collections.
if (!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;
|
public static java.util.Collection | checkedCollection(java.util.Collection c, java.lang.Class 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.
return new CheckedCollection<E>(c, type);
|
public static java.util.List | checkedList(java.util.List list, java.lang.Class 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.
if (list instanceof RandomAccess) {
return new CheckedRandomAccessList<E>(list, type);
}
return new CheckedList<E>(list, type);
|
public static java.util.Map | checkedMap(java.util.Map m, java.lang.Class keyType, java.lang.Class 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.
return new CheckedMap<K, V>(m, keyType, valueType);
|
public static java.util.Set | checkedSet(java.util.Set s, java.lang.Class 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.
return new CheckedSet<E>(s, type);
|
public static java.util.SortedMap | checkedSortedMap(java.util.SortedMap m, java.lang.Class keyType, java.lang.Class 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.
return new CheckedSortedMap<K, V>(m, keyType, valueType);
|
public static java.util.SortedSet | checkedSortedSet(java.util.SortedSet s, java.lang.Class 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.
return new CheckedSortedSet<E>(s, type);
|
public static void | copy(java.util.List destination, java.util.List source)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.
if (destination.size() < source.size()) {
throw new ArrayIndexOutOfBoundsException();
}
Iterator<? extends T> srcIt = source.iterator();
ListIterator<? super T> destIt = destination.listIterator();
while (srcIt.hasNext()) {
try {
destIt.next();
} catch (NoSuchElementException e) {
throw new ArrayIndexOutOfBoundsException();
}
destIt.set(srcIt.next());
}
|
public static boolean | disjoint(java.util.Collection c1, java.util.Collection c2)Returns whether the specified collections have no elements in common.
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;
|
public static final java.util.List | emptyList()Returns a type-safe empty, immutable {@link List}.
return EMPTY_LIST;
|
public static final java.util.Map | emptyMap()Returns a type-safe empty, immutable {@link Map}.
return EMPTY_MAP;
|
public static final java.util.Set | emptySet()Returns a type-safe empty, immutable {@link Set}.
return EMPTY_SET;
|
public static java.util.Enumeration | enumeration(java.util.Collection collection)Returns an {@code Enumeration} on the specified 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();
}
};
|
public static void | fill(java.util.List list, T object)Fills the specified List with the specified element.
ListIterator<? super T> it = list.listIterator();
while (it.hasNext()) {
it.next();
it.set(object);
}
|
public static int | frequency(java.util.Collection c, java.lang.Object o)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.
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;
|
public static int | indexOfSubList(java.util.List list, java.util.List sublist)Searches the {@code list} for {@code sublist} and returns the beginning
index of the first occurrence.
-1 is returned if the {@code sublist} does not exist in {@code list}.
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(java.util.List list, java.util.List sublist)Searches the {@code list} for {@code sublist} and returns the beginning
index of the last occurrence.
-1 is returned if the {@code sublist} does not exist in {@code list}.
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 java.util.ArrayList | list(java.util.Enumeration enumeration)Returns an {@code ArrayList} with all the elements in the
{@code enumeration}. The elements in the returned ArrayList are in the
same order as in the {@code enumeration}.
ArrayList<T> list = new ArrayList<T>();
while (enumeration.hasMoreElements()) {
list.add(enumeration.nextElement());
}
return list;
|
public static T | max(java.util.Collection collection)Searches the specified Collection for the maximum element.
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;
|
public static T | max(java.util.Collection collection, java.util.Comparator comparator)Searches the specified Collection for the maximum element using the
specified Comparator.
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;
|
public static T | min(java.util.Collection collection, java.util.Comparator comparator)Searches the specified Collection for the minimum element using the
specified Comparator.
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;
|
public static T | min(java.util.Collection collection)Searches the specified Collection for the minimum element.
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;
|
public static java.util.List | nCopies(int length, T object)Returns a List containing the specified number of the specified element.
The list cannot be modified. The list is serializable.
return new CopiesList<T>(length, object);
|
public static boolean | replaceAll(java.util.List list, T obj, T obj2)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}.
int index;
boolean found = false;
while ((index = list.indexOf(obj)) > -1) {
found = true;
list.set(index, obj2);
}
return found;
|
public static void | reverse(java.util.List list)Modifies the specified {@code List} by reversing the order of the
elements.
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);
}
|
public static java.util.Comparator | reverseOrder()A Comparator which reverses the natural order of the elements. The
{@code Comparator} that's returned is {@link Serializable}.
return new ReverseComparator<T>();
|
public static java.util.Comparator | reverseOrder(java.util.Comparator c)Returns a {@link Comparator} that reverses the order of the
{@code Comparator} passed. If the {@code Comparator} passed is
{@code null}, then this method is equivalent to {@link #reverseOrder()}.
The {@code Comparator} that's returned is {@link Serializable} if the
{@code Comparator} passed is serializable or {@code null}.
if (c == null) {
return reverseOrder();
}
return new ReverseComparatorWithComparator<T>(c);
|
public static void | rotate(java.util.List lst, int dist)Rotates the elements in List {@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]
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);
}
|
public static void | shuffle(java.util.List list)Moves every element of the List to a random new position in the list.
shuffle(list, new Random());
|
public static void | shuffle(java.util.List list, java.util.Random random)Moves every element of the List to a random new position in the list
using the specified random number generator.
if (!(list instanceof RandomAccess)) {
Object[] array = list.toArray();
for (int i = array.length - 1; i > 0; i--) {
int index = random.nextInt() % (i + 1);
if (index < 0) {
index = -index;
}
Object temp = array[i];
array[i] = array[index];
array[index] = temp;
}
int i = 0;
ListIterator<Object> it = (ListIterator<Object>) list
.listIterator();
while (it.hasNext()) {
it.next();
it.set(array[i++]);
}
} else {
List<Object> rawList = (List<Object>) list;
for (int i = rawList.size() - 1; i > 0; i--) {
int index = random.nextInt() % (i + 1);
if (index < 0) {
index = -index;
}
rawList.set(index, rawList.set(i, rawList.get(index)));
}
}
|
public static java.util.Set | singleton(E object)Returns a Set containing the specified element. The set cannot be
modified. The set is serializable.
return new SingletonSet<E>(object);
|
public static java.util.List | singletonList(E object)Returns a List containing the specified element. The list cannot be
modified. The list is serializable.
return new SingletonList<E>(object);
|
public static java.util.Map | singletonMap(K key, V value)Returns a Map containing the specified key and value. The map cannot be
modified. The map is serializable.
return new SingletonMap<K, V>(key, value);
|
public static void | sort(java.util.List list)Sorts the specified List in ascending natural order. The algorithm is
stable which means equal elements don't get reordered.
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++]);
}
|
public static void | sort(java.util.List list, java.util.Comparator comparator)Sorts the specified List using the specified Comparator. The algorithm is
stable which means equal elements don't get reordered.
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++]);
}
|
public static void | swap(java.util.List list, int index1, int index2)Swaps the elements of List {@code list} at indices {@code index1} and
{@code index2}.
if (list == null) {
throw new NullPointerException();
}
if (index1 == index2) {
return;
}
List<Object> rawList = (List<Object>) list;
rawList.set(index2, rawList.set(index1, rawList.get(index2)));
|
public static java.util.Collection | synchronizedCollection(java.util.Collection collection)Returns a wrapper on the specified Collection which synchronizes all
access to the Collection.
if (collection == null) {
throw new NullPointerException();
}
return new SynchronizedCollection<T>(collection);
|
public static java.util.List | synchronizedList(java.util.List list)Returns a wrapper on the specified List which synchronizes all access to
the List.
if (list == null) {
throw new NullPointerException();
}
if (list instanceof RandomAccess) {
return new SynchronizedRandomAccessList<T>(list);
}
return new SynchronizedList<T>(list);
|
public static java.util.Map | synchronizedMap(java.util.Map map)Returns a wrapper on the specified Map which synchronizes all access to
the Map.
if (map == null) {
throw new NullPointerException();
}
return new SynchronizedMap<K, V>(map);
|
public static java.util.Set | synchronizedSet(java.util.Set set)Returns a wrapper on the specified Set which synchronizes all access to
the Set.
if (set == null) {
throw new NullPointerException();
}
return new SynchronizedSet<E>(set);
|
public static java.util.SortedMap | synchronizedSortedMap(java.util.SortedMap map)Returns a wrapper on the specified SortedMap which synchronizes all
access to the SortedMap.
if (map == null) {
throw new NullPointerException();
}
return new SynchronizedSortedMap<K, V>(map);
|
public static java.util.SortedSet | synchronizedSortedSet(java.util.SortedSet set)Returns a wrapper on the specified SortedSet which synchronizes all
access to the SortedSet.
if (set == null) {
throw new NullPointerException();
}
return new SynchronizedSortedSet<E>(set);
|
public static java.util.Collection | unmodifiableCollection(java.util.Collection collection)Returns a wrapper on the specified Collection which throws an
{@code UnsupportedOperationException} whenever an attempt is made to
modify the Collection.
if (collection == null) {
throw new NullPointerException();
}
return new UnmodifiableCollection<E>((Collection<E>) collection);
|
public static java.util.List | unmodifiableList(java.util.List list)Returns a wrapper on the specified List which throws an
{@code UnsupportedOperationException} whenever an attempt is made to
modify the 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);
|
public static java.util.Map | unmodifiableMap(java.util.Map map)Returns a wrapper on the specified Map which throws an
{@code UnsupportedOperationException} whenever an attempt is made to
modify the Map.
if (map == null) {
throw new NullPointerException();
}
return new UnmodifiableMap<K, V>((Map<K, V>) map);
|
public static java.util.Set | unmodifiableSet(java.util.Set set)Returns a wrapper on the specified Set which throws an
{@code UnsupportedOperationException} whenever an attempt is made to
modify the Set.
if (set == null) {
throw new NullPointerException();
}
return new UnmodifiableSet<E>((Set<E>) set);
|
public static java.util.SortedMap | unmodifiableSortedMap(java.util.SortedMap map)Returns a wrapper on the specified SortedMap which throws an
{@code UnsupportedOperationException} whenever an attempt is made to
modify the SortedMap.
if (map == null) {
throw new NullPointerException();
}
return new UnmodifiableSortedMap<K, V>((SortedMap<K, V>) map);
|
public static java.util.SortedSet | unmodifiableSortedSet(java.util.SortedSet set)Returns a wrapper on the specified SortedSet which throws an
{@code UnsupportedOperationException} whenever an attempt is made to
modify the SortedSet.
if (set == null) {
throw new NullPointerException();
}
return new UnmodifiableSortedSet<E>(set);
|