Package com.gengoai.collection
Class Lists
- java.lang.Object
-
- com.gengoai.collection.Lists
-
public final class Lists extends Object
Convenience methods for creating lists and manipulating collections resulting in lists.
- Author:
- David B. Bracewell
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T> ArrayList<T>
arrayListOf(@NonNull T... elements)
Creates an array list of the supplied elementsstatic <T> ArrayList<T>
asArrayList(@NonNull Iterable<? extends T> iterable)
Creates an array list of the supplied elementsstatic <T> ArrayList<T>
asArrayList(@NonNull Iterator<? extends T> iterator)
Creates an array list of the supplied elementsstatic <T> ArrayList<T>
asArrayList(@NonNull Stream<? extends T> stream)
Creates an array list of the supplied elementsstatic <T> List<T>
asConcurrentList(@NonNull Iterable<? extends T> iterable)
Creates a copy on write array list of the supplied elementsstatic <T> List<T>
asConcurrentList(@NonNull Iterator<? extends T> iterator)
Creates a copy on write array list of the supplied elementsstatic <T> List<T>
asConcurrentList(@NonNull Stream<? extends T> stream)
Creates a copy on write array list of the supplied elementsstatic <T> LinkedList<T>
asLinkedList(@NonNull Iterable<? extends T> iterable)
Creates a linked list of the supplied elementsstatic <T> LinkedList<T>
asLinkedList(@NonNull Iterator<? extends T> iterator)
Creates a linked list of the supplied elementsstatic <T> List<T>
asLinkedList(Stream<? extends T> stream)
Creates a linked list of the supplied elementsstatic <T> Iterable<Tuple>
combinations(@NonNull List<T> list, int size)
Generates all combinations of the given size from the items in the list.static <T> List<T>
concurrentListOf(@NonNull T... elements)
Creates a copy on write array list of the supplied elementsstatic <E> List<E>
difference(@NonNull Collection<? extends E> collection1, @NonNull Collection<? extends E> collection2)
Retains all items in collection1 that are not in collection2 and returns them as a list.static <T> List<T>
ensureSize(@NonNull List<T> list, int desiredSize, T defaultValue)
Ensures that the size of the given list is at least the supplied desired size.static <T> LinkedList<T>
linkedListOf(@NonNull T... elements)
Creates a linked list of the supplied elementsstatic <T> List<List<T>>
partition(@NonNull List<T> list, int partitionSize)
Partitions a list into multiple lists of partition size (last list may have a size less than partition size).static <E> List<E>
sampleWithReplacement(@NonNull List<? extends E> list, int N)
Samples the given list for N items allowing replacementstatic <I,O>
List<O>transform(@NonNull List<? extends I> list, @NonNull SerializableFunction<? super I,? extends O> converter)
Transforms the given list with the given functionstatic <E> List<E>
union(@NonNull Collection<? extends E> collection1, @NonNull Collection<? extends E> collection2)
Retains all items in collection1 and collection2 and returns them as a list.
-
-
-
Method Detail
-
arrayListOf
@SafeVarargs public static <T> ArrayList<T> arrayListOf(@NonNull @NonNull T... elements)
Creates an array list of the supplied elements- Type Parameters:
T
- the component type of the set- Parameters:
elements
- the elements to add to the set- Returns:
- the new array list containing the given elements
-
asArrayList
public static <T> ArrayList<T> asArrayList(@NonNull @NonNull Stream<? extends T> stream)
Creates an array list of the supplied elements- Type Parameters:
T
- the component type of the set- Parameters:
stream
- the elements to add to the set- Returns:
- the new array list containing the given elements
-
asArrayList
public static <T> ArrayList<T> asArrayList(@NonNull @NonNull Iterator<? extends T> iterator)
Creates an array list of the supplied elements- Type Parameters:
T
- the component type of the set- Parameters:
iterator
- the elements to add to the set- Returns:
- the new array list containing the given elements
-
asArrayList
public static <T> ArrayList<T> asArrayList(@NonNull @NonNull Iterable<? extends T> iterable)
Creates an array list of the supplied elements- Type Parameters:
T
- the component type of the set- Parameters:
iterable
- the elements to add to the set- Returns:
- the new array list containing the given elements
-
asConcurrentList
public static <T> List<T> asConcurrentList(@NonNull @NonNull Stream<? extends T> stream)
Creates a copy on write array list of the supplied elements- Type Parameters:
T
- the component type of the set- Parameters:
stream
- the elements to add to the set- Returns:
- the new copy on write array list containing the given elements
-
asConcurrentList
public static <T> List<T> asConcurrentList(@NonNull @NonNull Iterator<? extends T> iterator)
Creates a copy on write array list of the supplied elements- Type Parameters:
T
- the component type of the set- Parameters:
iterator
- the elements to add to the set- Returns:
- the new copy on write array list containing the given elements
-
asConcurrentList
public static <T> List<T> asConcurrentList(@NonNull @NonNull Iterable<? extends T> iterable)
Creates a copy on write array list of the supplied elements- Type Parameters:
T
- the component type of the set- Parameters:
iterable
- the elements to add to the set- Returns:
- the new copy on write array list containing the given elements
-
asLinkedList
public static <T> List<T> asLinkedList(Stream<? extends T> stream)
Creates a linked list of the supplied elements- Type Parameters:
T
- the component type of the set- Parameters:
stream
- the elements to add to the set- Returns:
- the new linked list containing the given elements
-
asLinkedList
public static <T> LinkedList<T> asLinkedList(@NonNull @NonNull Iterator<? extends T> iterator)
Creates a linked list of the supplied elements- Type Parameters:
T
- the component type of the set- Parameters:
iterator
- the elements to add to the set- Returns:
- the new linked list containing the given elements
-
asLinkedList
public static <T> LinkedList<T> asLinkedList(@NonNull @NonNull Iterable<? extends T> iterable)
Creates a linked list of the supplied elements- Type Parameters:
T
- the component type of the set- Parameters:
iterable
- the elements to add to the set- Returns:
- the new linked list containing the given elements
-
combinations
public static <T> Iterable<Tuple> combinations(@NonNull @NonNull List<T> list, int size)
Generates all combinations of the given size from the items in the list. For example, given the list[1, 2, 3]
and size2
we would generate the following combinations:[ [1,2], [1,3], [2,3] ]
- Type Parameters:
T
- the type parameter- Parameters:
list
- the list of itemssize
- the number of items in the combination.- Returns:
- the iterable
-
concurrentListOf
@SafeVarargs public static <T> List<T> concurrentListOf(@NonNull @NonNull T... elements)
Creates a copy on write array list of the supplied elements- Type Parameters:
T
- the component type of the set- Parameters:
elements
- the elements to add to the set- Returns:
- the new copy on write array list containing the given elements
-
difference
public static <E> List<E> difference(@NonNull @NonNull Collection<? extends E> collection1, @NonNull @NonNull Collection<? extends E> collection2)
Retains all items in collection1 that are not in collection2 and returns them as a list.
- Type Parameters:
E
- the component type of the collections- Parameters:
collection1
- the first collection of itemscollection2
- the second collection of items- Returns:
- A list of the collection1 - collection2
-
ensureSize
public static <T> List<T> ensureSize(@NonNull @NonNull List<T> list, int desiredSize, T defaultValue)
Ensures that the size of the given list is at least the supplied desired size. If the list size is smaller, it will add the given default value to the end of the list until
list.size() >= desiredSize
- Type Parameters:
T
- the component type of the list- Parameters:
list
- the list whose size is being checkeddesiredSize
- the desired size of the listdefaultValue
- the default value to add to the list to reach the desired size- Returns:
- the list passed in with size greater than or equal to the desired size
-
linkedListOf
@SafeVarargs public static <T> LinkedList<T> linkedListOf(@NonNull @NonNull T... elements)
Creates a linked list of the supplied elements- Type Parameters:
T
- the component type of the set- Parameters:
elements
- the elements to add to the set- Returns:
- the new linked list containing the given elements
-
partition
public static <T> List<List<T>> partition(@NonNull @NonNull List<T> list, int partitionSize)
Partitions a list into multiple lists of partition size (last list may have a size less than partition size). This method usessubList
which means that each partition is a view into the underlying list.- Type Parameters:
T
- the list component type- Parameters:
list
- the list to partitionpartitionSize
- the partition size- Returns:
- A list of partitioned lists
-
sampleWithReplacement
public static <E> List<E> sampleWithReplacement(@NonNull @NonNull List<? extends E> list, int N)
Samples the given list for N items allowing replacement- Type Parameters:
E
- the list type parameter- Parameters:
list
- the list of elements to sampleN
- the number of elements to sample- Returns:
- the list of sampled elements
-
transform
public static <I,O> List<O> transform(@NonNull @NonNull List<? extends I> list, @NonNull @NonNull SerializableFunction<? super I,? extends O> converter)
Transforms the given list with the given function- Type Parameters:
I
- the input list type parameterO
- the transformed list type parameter- Parameters:
list
- the input listconverter
- the function to convert the input elements to the output elements- Returns:
- the transformed list (lazy transformation)
-
union
public static <E> List<E> union(@NonNull @NonNull Collection<? extends E> collection1, @NonNull @NonNull Collection<? extends E> collection2)
Retains all items in collection1 and collection2 and returns them as a list.
- Type Parameters:
E
- the component type of the collections- Parameters:
collection1
- the first collection of itemscollection2
- the second collection of items- Returns:
- A list of the collection1 + collection2
-
-