Package org.eclipse.cdt.core.parser.util
Class CollectionUtils
- java.lang.Object
-
- org.eclipse.cdt.core.parser.util.CollectionUtils
-
public final class CollectionUtils extends Object
Useful utility methods for dealing with Collections.- Restriction:
- This class is not intended to be instantiated by clients.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> Collection<T>
filter(Collection<T> collection, IUnaryPredicate<T> predicate)
Filter the elements of a collection down to just the ones that match the given predicate.static <T> T
findFirstAndRemove(List<?> list, Class<T> clazz)
Finds the first object in the heterogeneous list that is an instance of the given class, removes it from the list, and returns it.static <T> Iterable<T>
iterable(Iterator<T> iter)
Creates an Iterable instance that just returns the given Iterator from its iterator() method.static <T,U>
List<U>listMapGet(Map<T,List<U>> m, T t)
Returns a List corresponding to a T in a Map>. static <T,U extends Collection<T>>
Umerge(U c1, U c2)
Combines two collections into one.static <T> Iterable<T>
reverseIterable(List<T> list)
Allows a foreach loop to iterate backwards over a list from the end to the start.static <T> Iterator<T>
reverseIterator(List<T> list)
Returns an iterator that iterates backwards over the given list.
-
-
-
Method Detail
-
reverseIterator
public static <T> Iterator<T> reverseIterator(List<T> list)
Returns an iterator that iterates backwards over the given list. The remove() method is not implemented and will throw UnsupportedOperationException. The returned iterator does not support the remove() method.- Throws:
NullPointerException
- if list isnull
-
reverseIterable
public static <T> Iterable<T> reverseIterable(List<T> list)
Allows a foreach loop to iterate backwards over a list from the end to the start.Example use:
for (Object o : reverseIterable(list)) { ... }
- Throws:
NullPointerException
- if list is null
-
iterable
public static <T> Iterable<T> iterable(Iterator<T> iter)
Creates an Iterable instance that just returns the given Iterator from its iterator() method. This is useful for using an iterator in a foreach loop directly.Example use:
for (Object o : iterable(iterator)) { ... }
- Throws:
NullPointerException
- if list isnull
-
findFirstAndRemove
public static <T> T findFirstAndRemove(List<?> list, Class<T> clazz)
Finds the first object in the heterogeneous list that is an instance of the given class, removes it from the list, and returns it. If there is not object in the list of the given type the list is left unmodified and null is returned.- Throws:
NullPointerException
- if list or clazz is nullUnsupportedOperationException
- if the list's Iterator does not support the remove() method
-
merge
public static <T,U extends Collection<T>> U merge(U c1, U c2)
Combines two collections into one.- Parameters:
c1
- The first collection. May be modified as a result of the call. May benull
.c2
- The second collection. May benull
.- Returns:
- A collection containing elements from both input collections,
or
null
if both,c1
andc2
arenull
. - Since:
- 5.4
-
listMapGet
public static <T,U> List<U> listMapGet(Map<T,List<U>> m, T t)
Returns a List corresponding to a T in a Map>. If the mapping doesn't exist, creates it with an empty list as the initial value. - Since:
- 5.6
-
filter
public static <T> Collection<T> filter(Collection<T> collection, IUnaryPredicate<T> predicate)
Filter the elements of a collection down to just the ones that match the given predicate.- Since:
- 5.6
-
-