public final class ListUtils
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
ListUtils.Replacement<T> |
Modifier and Type | Method and Description |
---|---|
static <T> boolean |
addIfNotPresent(java.util.List<? super T> list,
int index,
T element)
Adds an element to a list if the list does not already contain it.
|
static <T> boolean |
addIfNotPresent(java.util.List<? super T> list,
T element)
Adds an element to a list if the list does not already contain it.
|
static <T> int |
firstIndexOf(java.util.List<? extends T> list,
java.util.function.Predicate<? super T> predicate)
Gets the first index of an element matching a predicate.
|
static <T> java.util.stream.Collector<T,?,java.util.List<T>> |
joining(java.util.function.Supplier<? extends T> separator) |
static <T> java.util.stream.Collector<T,?,java.util.List<T>> |
joining(java.util.function.Supplier<? extends T> prefix,
java.util.function.Supplier<? extends T> separator,
java.util.function.Supplier<? extends T> suffix)
Creates a collector for interleaving items with a constant separator item, along with prepending and appending
items to bookend the resulting list.
|
static <T> ListUtils.Replacement<T> |
replaceIn(java.util.List<T> list)
Replaces the first instance of an element in a list with another.
|
static <T> java.util.stream.Collector<T,?,com.google.common.collect.ImmutableList<T>> |
toImmutableList()
Creates a new collector for immutable lists.
|
public static <T> int firstIndexOf(java.util.List<? extends T> list, java.util.function.Predicate<? super T> predicate)
T
- the type of elements in the listlist
- the list to searchpredicate
- the predicate to usejava.lang.NullPointerException
- if either list or predicate is nullpublic static <T> boolean addIfNotPresent(java.util.List<? super T> list, T element)
T
- the type of values in the listlist
- the list to add toelement
- the element to addpublic static <T> boolean addIfNotPresent(java.util.List<? super T> list, int index, T element)
T
- the type of values in the listlist
- the list to add toindex
- the index in the list to add the element toelement
- the element to addpublic static <T> java.util.stream.Collector<T,?,com.google.common.collect.ImmutableList<T>> toImmutableList()
For example:
ImmutableList<T> list = values.stream()
.filter(...)
.map(...)
.collect(toImmutableList());
T
- the type of elements to collectpublic static <T> java.util.stream.Collector<T,?,java.util.List<T>> joining(java.util.function.Supplier<? extends T> separator)
public static <T> java.util.stream.Collector<T,?,java.util.List<T>> joining(java.util.function.Supplier<? extends T> prefix, java.util.function.Supplier<? extends T> separator, java.util.function.Supplier<? extends T> suffix)
null
result, no element will be added
for that part of the collection. For example, if prefix
supplies null
, then no element will be
prepended to the list, as opposed to prepending null
.
Each supplier must return consistent, equivalent values. For example, prefix
cannot
return null
on one invocation and "foo"
on another.
public static <T> ListUtils.Replacement<T> replaceIn(java.util.List<T> list)
T
- the type of elements in the listlist
- the list to replace the item in