|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.google.common.collect.Lists
@GwtCompatible 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 List<Character> |
charactersOf(CharSequence sequence)
Returns a view of the specified CharSequence as a List<Character> , viewing sequence as a sequence of Unicode code
units. |
|
static ImmutableList<Character> |
charactersOf(String string)
Returns a view of the specified string as an immutable list of Character values. |
|
static
|
newArrayList()
Creates a mutable, empty ArrayList instance. |
|
static
|
newArrayList(E... elements)
Creates a mutable ArrayList instance containing the given
elements. |
|
static
|
newArrayList(Iterable<? extends E> elements)
Creates a mutable ArrayList instance containing the given
elements. |
|
static
|
newArrayList(Iterator<? extends E> elements)
Creates a mutable ArrayList instance containing the given
elements. |
|
static
|
newArrayListWithCapacity(int initialArraySize)
Creates an ArrayList instance backed by an array of the
exact size specified; equivalent to
ArrayList.ArrayList(int) . |
|
static
|
newArrayListWithExpectedSize(int estimatedSize)
Creates an ArrayList instance sized appropriately to hold an
estimated number of elements without resizing. |
|
static
|
newLinkedList()
Creates an empty LinkedList instance. |
|
static
|
newLinkedList(Iterable<? extends E> elements)
Creates a LinkedList instance containing the given elements. |
|
static
|
partition(List<T> list,
int size)
Returns consecutive sublists of a list, each of the same size (the final list may be smaller). |
|
static
|
reverse(List<T> list)
Returns a reversed view of the specified list. |
|
static
|
transform(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 |
---|
@GwtCompatible(serializable=true) public static <E> ArrayList<E> newArrayList()
ArrayList
instance.
Note: if mutability is not required, use ImmutableList.of()
instead.
ArrayList
@GwtCompatible(serializable=true) public static <E> ArrayList<E> newArrayList(E... elements)
ArrayList
instance containing the given
elements.
Note: if mutability is not required and the elements are
non-null, use an overload of ImmutableList.of()
(for varargs) or
ImmutableList.copyOf(Object[])
(for an array) instead.
elements
- the elements that the list should contain, in order
ArrayList
containing those elements@GwtCompatible(serializable=true) public static <E> ArrayList<E> newArrayList(Iterable<? extends E> elements)
ArrayList
instance containing the given
elements.
Note: if mutability is not required and the elements are
non-null, use ImmutableList.copyOf(Iterator)
instead.
elements
- the elements that the list should contain, in order
ArrayList
containing those elements@GwtCompatible(serializable=true) public static <E> ArrayList<E> newArrayList(Iterator<? extends E> elements)
ArrayList
instance containing the given
elements.
Note: if mutability is not required and the elements are
non-null, use ImmutableList.copyOf(Iterator)
instead.
elements
- the elements that the list should contain, in order
ArrayList
containing those elements@GwtCompatible(serializable=true) public static <E> ArrayList<E> newArrayListWithCapacity(int initialArraySize)
ArrayList
instance backed by an array of the
exact size specified; equivalent to
ArrayList.ArrayList(int)
.
Note: if you know the exact size your list will be, consider
using a fixed-size list (Arrays.asList(Object[])
) or an ImmutableList
instead of a growable ArrayList
.
Note: If you have only an estimate of the eventual size of
the list, consider padding this estimate by a suitable amount, or simply
use newArrayListWithExpectedSize(int)
instead.
initialArraySize
- the exact size of the initial backing array for
the returned array list (ArrayList
documentation calls this
value the "capacity")
ArrayList
which is guaranteed not to resize
itself unless its size reaches initialArraySize + 1
IllegalArgumentException
- if initialArraySize
is negative@GwtCompatible(serializable=true) public static <E> ArrayList<E> newArrayListWithExpectedSize(int estimatedSize)
ArrayList
instance sized appropriately to hold an
estimated number of elements without resizing. A small amount of
padding is added in case the estimate is low.
Note: If you know the exact number of elements the list
will hold, or prefer to calculate your own amount of padding, refer to
newArrayListWithCapacity(int)
.
estimatedSize
- an estimate of the eventual List.size()
of
the new list
ArrayList
, sized appropriately to hold the
estimated number of elements
IllegalArgumentException
- if estimatedSize
is negative@GwtCompatible(serializable=true) public static <E> LinkedList<E> newLinkedList()
LinkedList
instance.
Note: if you need an immutable empty List
, use
Collections.emptyList()
instead.
LinkedList
@GwtCompatible(serializable=true) public static <E> LinkedList<E> newLinkedList(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> 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> 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> List<T> transform(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.
If only a Collection
or Iterable
input is available, use
Collections2.transform(java.util.Collection
or Iterables.transform(java.lang.Iterable
.
public static <T> List<List<T>> partition(List<T> list, int size)
[a, b, c, d, e]
with a partition
size of 3 yields [[a, b, c], [d, e]]
-- an outer list containing
two inner lists of three and two elements, all in the original order.
The outer list is unmodifiable, but reflects the latest state of the
source list. The inner lists are sublist views of the original list,
produced on demand using List.subList(int, int)
, and are subject
to all the usual caveats about modification as explained in that API.
list
- the list to return consecutive sublists ofsize
- the desired size of each sublist (the last may be
smaller)
IllegalArgumentException
- if partitionSize
is nonpositive@Beta public static ImmutableList<Character> charactersOf(String string)
Character
values.
@Beta public static List<Character> charactersOf(CharSequence sequence)
CharSequence
as a List<Character>
, viewing sequence
as a sequence of Unicode code
units. The view does not support any modification operations, but reflects
any changes to the underlying character sequence.
sequence
- the character sequence to view as a List
of
characters
List<Character>
view of the character sequencepublic static <T> List<T> reverse(List<T> list)
Lists.reverse(Arrays.asList(1, 2, 3))
returns a list containing 3,
2, 1
. The returned list is backed by this list, so changes in the returned
list are reflected in this list, and vice-versa. The returned list supports
all of the optional list operations supported by this list.
The returned list is random-access if the specified list is random access.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |