Class CollectionUtils
- java.lang.Object
-
- org.apache.jackrabbit.oak.commons.collections.CollectionUtils
-
public class CollectionUtils extends Object
Utility methods for collections conversions.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <K,V>
@NotNull Map<K,V>newHashMap(int capacity)
Creates a new, empty HashMap with expected capacity.static <K> @NotNull Set<K>
newHashSet(int capacity)
Creates a new, empty HashSet with expected capacity.static <K> @NotNull Set<K>
newLinkedHashSet(int capacity)
Creates a new, empty LinkedHashSet with expected capacity.static <T> @NotNull Iterable<T>
toIterable(@NotNull Iterator<T> iterator)
Convert anIterator
to anIterable
.static <T> @NotNull Set<T>
toLinkedSet(@NotNull Iterable<T> iterable)
Convert an iterable to aLinkedHashSet
.static <T> @NotNull List<T>
toList(@NotNull Iterable<T> iterable)
Convert an iterable to a list.static <T> @NotNull List<T>
toList(Iterator<T> iterator)
Convert an iterator to a list.static <T> @NotNull Set<T>
toSet(@NotNull Iterable<T> iterable)
Convert an iterable to a set.static <T> @NotNull Set<T>
toSet(@NotNull Iterator<T> iterator)
Convert an iterator to a set.static <T> @NotNull Set<T>
toSet(@NotNull T... elements)
Convert a vararg list of items to a set.static <T> @NotNull Stream<T>
toStream(@NotNull Iterable<T> iterable)
static <T> @NotNull Stream<T>
toStream(@NotNull Iterator<T> iterator)
static <T extends Comparable>
@NotNull TreeSet<T>toTreeSet(@NotNull Iterable<? extends T> iterable)
Convert an iterable to aTreeSet
.
-
-
-
Method Detail
-
toList
@NotNull public static <T> @NotNull List<T> toList(@NotNull @NotNull Iterable<T> iterable)
Convert an iterable to a list. The returning list is mutable and supports all optional operations.- Type Parameters:
T
- the type of the elements- Parameters:
iterable
- the iterable to convert- Returns:
- the list
-
toList
@NotNull public static <T> @NotNull List<T> toList(Iterator<T> iterator)
Convert an iterator to a list. The returning list is mutable and supports all optional operations.- Type Parameters:
T
- the type of the elements- Parameters:
iterator
- the iterator to convert- Returns:
- the list
-
toSet
@NotNull public static <T> @NotNull Set<T> toSet(@NotNull @NotNull Iterable<T> iterable)
Convert an iterable to a set. The returning set is mutable and supports all optional operations.- Type Parameters:
T
- the type of the elements- Parameters:
iterable
- the iterable to convert- Returns:
- the set
-
toLinkedSet
@NotNull public static <T> @NotNull Set<T> toLinkedSet(@NotNull @NotNull Iterable<T> iterable)
Convert an iterable to aLinkedHashSet
. The returning set is mutable and supports all optional operations.- Type Parameters:
T
- the type of the elements- Parameters:
iterable
- the iterable to convert- Returns:
- the linkedHashSet
-
toTreeSet
@NotNull public static <T extends Comparable> @NotNull TreeSet<T> toTreeSet(@NotNull @NotNull Iterable<? extends T> iterable)
Convert an iterable to aTreeSet
. The returning set is mutable and supports all optional operations.- Type Parameters:
T
- the type of the elements- Parameters:
iterable
- the iterable to convert- Returns:
- the treeSet
-
toSet
@NotNull public static <T> @NotNull Set<T> toSet(@NotNull @NotNull Iterator<T> iterator)
Convert an iterator to a set. The returning set is mutable and supports all optional operations.- Type Parameters:
T
- the type of the elements- Parameters:
iterator
- the iterator to convert- Returns:
- the set
-
toSet
@SafeVarargs @NotNull public static <T> @NotNull Set<T> toSet(@NotNull @NotNull T... elements)
Convert a vararg list of items to a set. The returning set is mutable and supports all optional operations.- Type Parameters:
T
- the type of the elements- Parameters:
elements
- elements to convert- Returns:
- the set
-
newHashSet
@NotNull public static <K> @NotNull Set<K> newHashSet(int capacity)
Creates a new, empty HashSet with expected capacity.The returned set is large enough to add expected no. of elements without resizing.
- Parameters:
capacity
- the expected number of elements- Throws:
IllegalArgumentException
- if capacity is negative- See Also:
newHashMap(int)
,newLinkedHashSet(int)
-
newLinkedHashSet
@NotNull public static <K> @NotNull Set<K> newLinkedHashSet(int capacity)
Creates a new, empty LinkedHashSet with expected capacity.The returned set is large enough to add expected no. of elements without resizing.
- Parameters:
capacity
- the expected number of elements- Throws:
IllegalArgumentException
- if capacity is negative- See Also:
newHashMap(int)
,newHashSet(int)
-
newHashMap
@NotNull public static <K,V> @NotNull Map<K,V> newHashMap(int capacity)
Creates a new, empty HashMap with expected capacity.The returned map uses the default load factor of 0.75, and its capacity is large enough to add expected number of elements without resizing.
- Parameters:
capacity
- the expected number of elements- Throws:
IllegalArgumentException
- if capacity is negative- See Also:
newHashSet(int)
,newLinkedHashSet(int)
-
toIterable
@NotNull public static <T> @NotNull Iterable<T> toIterable(@NotNull @NotNull Iterator<T> iterator)
Convert anIterator
to anIterable
.This method is not thread-safe
- Parameters:
iterator
- iterator to convert- Returns:
- a single-use iterable for the iterator (representing the remaining elements in the iterator)
- Throws:
IllegalStateException
- when Iterable.iterator() is called more than once
-
toStream
@NotNull public static <T> @NotNull Stream<T> toStream(@NotNull @NotNull Iterable<T> iterable)
- Parameters:
iterable
- iterable to convert- Returns:
- the stream
-
-