|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.google.common.collect.Lists
public final class Lists
Static utility methods pertaining to List instances. Also see this
class's counterparts Sets and Maps.
| Method Summary | ||
|---|---|---|
static
|
asList(E first,
E[] rest)
Returns an unmodifiable list containing the specified first element and backed by the specified array of additional elements. |
|
static
|
asList(E first,
E second,
E[] rest)
Returns an unmodifiable list containing the specified first and second element, and backed by the specified array of additional elements. |
|
static
|
immutableList()
Returns an immutable List instance with no elements. |
|
static
|
immutableList(E... elements)
Returns an immutable List instance containing the given elements. |
|
static
|
immutableList(E element)
Returns an immutable List instance with a single element. |
|
static
|
immutableList(java.lang.Iterable<? extends E> iterable)
Returns an immutable List instance containing the given elements. |
|
static
|
immutableList(java.util.Iterator<? extends E> iterator)
Returns an immutable List instance containing the given elements. |
|
static
|
newArrayList()
Creates an empty ArrayList instance. |
|
static
|
newArrayList(E... elements)
Creates an ArrayList instance containing the given elements. |
|
static
|
newArrayList(java.lang.Iterable<? extends E> elements)
Creates an ArrayList instance containing the given elements. |
|
static
|
newArrayList(java.util.Iterator<? extends E> elements)
Creates an ArrayList instance containing the given elements. |
|
static
|
newArrayListWithCapacity(int initialCapacity)
Creates an ArrayList instance with the given initial capacity. |
|
static
|
newArrayListWithExpectedSize(int expectedSize)
Creates an ArrayList instance with the given expected size. |
|
static
|
newLinkedList()
Creates an empty LinkedList instance. |
|
static
|
newLinkedList(E... elements)
Creates a LinkedList instance containing the given elements. |
|
static
|
newLinkedList(java.lang.Iterable<? extends E> elements)
Creates a LinkedList instance containing the given elements. |
|
static
|
newLinkedList(java.util.Iterator<? extends E> elements)
Creates a LinkedList instance containing the given elements. |
|
static
|
sortedCopy(java.lang.Iterable<E> iterable)
Returns a copy of the given iterable sorted by the natural ordering of its elements. |
|
static
|
sortedCopy(java.lang.Iterable<E> iterable,
java.util.Comparator<? super E> comparator)
Returns a copy of the given iterable sorted by an explicit comparator. |
|
static
|
transform(java.util.List<F> fromList,
Function<? super F,? extends T> function)
Returns a list that applies function to each element of fromList. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
public static <E> java.util.List<E> immutableList(E... elements)
List instance containing the given elements.
Note: due to a bug in javac 1.5.0_06, we cannot support the following:
List<Base> list = Lists.immutableList(sub1, sub2);
where sub1 and sub2 are references to subtypes of Base, not of Base itself. To get around this, you must use:
List<Base> list = Lists.<Base>immutableList(sub1, sub2);
elements - the elements that the list should contain, in order. If an
array is given, its contents may later be altered without affecting the
List returned by this method
List instance containing those elementspublic static <E> java.util.List<E> immutableList(java.lang.Iterable<? extends E> iterable)
List instance containing the given elements.
Note that if the input is an immutable list, the input itself may be
returned.
iterable - the elements that the list should contain, in order
List instance containing those elementspublic static <E> java.util.List<E> immutableList(java.util.Iterator<? extends E> iterator)
List instance containing the given elements.
iterator - the elements that the list should contain, in order
List instance containing those elementspublic static <E> java.util.List<E> immutableList()
List instance with no elements.
Collections.emptyList()
public static <E> java.util.List<E> immutableList(@Nullable
E element)
List instance with a single element.
element - the lone element to be in the returned list
Collections.singletonList(T)public static <E> java.util.ArrayList<E> newArrayList()
ArrayList instance.
Note: if you need an immutable empty list, use Collections.emptyList() instead.
ArrayListpublic static <E> java.util.ArrayList<E> newArrayList(E... elements)
ArrayList instance containing the given elements.
Note: if you need an immutable List, use ImmutableList
instead.
Note: due to a bug in javac 1.5.0_06, we cannot support the following:
List<Base> list = Lists.newArrayList(sub1, sub2);
where sub1 and sub2 are references to subtypes of Base, not of Base itself. To get around this, you must use:
List<Base> list = Lists.<Base>newArrayList(sub1, sub2);
elements - the elements that the list should contain, in order
ArrayList containing those elementspublic static <E> java.util.ArrayList<E> newArrayList(java.lang.Iterable<? extends E> elements)
ArrayList instance containing the given elements.
elements - the elements that the list should contain, in order
ArrayList containing those elementspublic static <E> java.util.ArrayList<E> newArrayList(java.util.Iterator<? extends E> elements)
ArrayList instance containing the given elements.
elements - the elements that the list should contain, in order
ArrayList containing those elementspublic static <E> java.util.ArrayList<E> newArrayListWithCapacity(int initialCapacity)
ArrayList instance with the given initial capacity.
initialCapacity - the initial capacity of the list
ArrayList with the given
initial capacity
java.lang.IllegalArgumentException - if the specified initial capacity is
negativepublic static <E> java.util.ArrayList<E> newArrayListWithExpectedSize(int expectedSize)
ArrayList instance with the given expected size.
expectedSize - the expected size of the list
ArrayList with enough
capacity for the given expected size
java.lang.IllegalArgumentException - if the specified expected size is negativepublic static <E> java.util.LinkedList<E> newLinkedList()
LinkedList instance.
Note: if you need an immutable empty List, use
Collections.emptyList() instead.
LinkedListpublic static <E> java.util.LinkedList<E> newLinkedList(E... elements)
LinkedList instance containing the given elements.
See the caveat in newArrayList(Object...).
elements - the elements that the list should contain, in order
LinkedList containing those elementspublic static <E> java.util.LinkedList<E> newLinkedList(java.lang.Iterable<? extends E> elements)
LinkedList instance containing the given elements.
elements - the elements that the list should contain, in order
LinkedList containing those elementspublic static <E> java.util.LinkedList<E> newLinkedList(java.util.Iterator<? extends E> elements)
LinkedList instance containing the given elements.
elements - the elements that the list should contain, in order
LinkedList containing those elementspublic static <E extends java.lang.Comparable> java.util.List<E> sortedCopy(java.lang.Iterable<E> iterable)
RandomAccess.
Unlike Sets.newTreeSet(Iterable), this method does not collapse
equal elements, and the resulting collection does not maintain its own sort
order.
iterable - the elements to be copied and sorted
java.lang.ClassCastException - if iterable contains elements that are
not mutually comparable
public static <E> java.util.List<E> sortedCopy(java.lang.Iterable<E> iterable,
java.util.Comparator<? super E> comparator)
RandomAccess.
Unlike Sets.newTreeSet(Comparator, Iterable), this method does
not collapse elements that the comparator treats as equal, and the
resulting collection does not maintain its own sort order.
iterable - the elements to be copied and sortedcomparator - a comparator capable of sorting the given elements
public static <E> java.util.List<E> asList(@Nullable
E first,
E[] rest)
rest array will be reflected in the returned list. Unlike Arrays.asList(T...), the returned list is unmodifiable.
This is useful when a varargs method needs to use a signature such as
(Foo firstFoo, Foo... moreFoos), in order to avoid overload
ambiguity or to enforce a minimum argument count.
The returned list is serializable and implements RandomAccess.
first - the first elementrest - an array of additional elements, possibly empty
public static <E> java.util.List<E> asList(@Nullable
E first,
@Nullable
E second,
E[] rest)
rest array will be reflected in the returned list. Unlike
Arrays.asList(T...), the returned list is unmodifiable.
This is useful when a varargs method needs to use a signature such as
(Foo firstFoo, Foo secondFoo, Foo... moreFoos), in order to avoid
overload ambiguity or to enforce a minimum argument count.
The returned list is serializable and implements RandomAccess.
first - the first elementsecond - the second elementrest - an array of additional elements, possibly empty
public static <F,T> java.util.List<T> transform(java.util.List<F> fromList,
Function<? super F,? extends T> function)
function to each element of fromList. The returned list is a transformed view of fromList;
changes to fromList will be reflected in the returned list and vice
versa.
Since functions are not reversible, the transform is one-way and new
items cannot be stored in the returned list. The add,
addAll and set methods are unsupported in the returned
list.
The function is applied lazily, invoked when needed. This is necessary
for the returned list to be a view, but it means that the function will be
applied many times for bulk operations like List.contains(java.lang.Object) and
List.hashCode(). For this to perform well, function should be
fast. To avoid lazy evaluation when the returned list doesn't need to be a
view, copy the returned list into a new list of your choosing.
If fromList implements RandomAccess, so will the
returned list. The returned list always implements Serializable,
but serialization will succeed only when fromList and
function are serializable. The returned list is threadsafe if the
supplied list and function are.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||