|
|||||||||
| 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
Provides static methods for creating List instances easily, and other
utility methods for working with lists. You can replace code like:
List<String> list = new ArrayList<String>();
Collections.addAll(list, "foo", "bar", "baz");
with just:
List<String> list = newArrayList("foo", "bar", "baz");
You can also create an empty List, or populate your new List using any array, Iterator or Iterable.
Supported today are: ArrayList and LinkedList.
See also this class's counterparts Sets and Maps.
WARNING: These factories do not support the full variety of tuning parameters available in the collection constructors. Use them only for collections which will always remain small, or for which the cost of future growth operations is not a concern.
| 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()
Variant of immutableList for zero arguments. |
|
static
|
immutableList(Collection<? extends E> collection)
Returns an immutable List instance containing the given elements. |
|
static
|
immutableList(E... elements)
Returns an immutable List instance containing the given elements. |
|
static
|
immutableList(E element)
Variant of immutableList for one argument. |
|
static
|
immutableList(Iterable<? extends E> iterable)
Returns an immutable List instance containing the given elements. |
|
static
|
immutableList(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 a resizable ArrayList instance containing the given
elements. |
|
static
|
newArrayList(Iterable<? extends E> elements)
Creates an ArrayList instance containing the given elements. |
|
static
|
newArrayList(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(Iterable<? extends E> elements)
Creates a LinkedList instance containing the given elements. |
|
static
|
newLinkedList(Iterator<? extends E> elements)
Creates a LinkedList instance containing the given elements. |
|
static
|
sortedCopy(Iterable<E> iterable)
Returns a copy of the given iterable sorted by the natural ordering of its elements. |
|
static
|
sortedCopy(Iterable<E> iterable,
Comparator<? super E> comparator)
Returns a copy of the given iterable sorted by an explicit comparator. |
|
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 |
|---|
public static <E> List<E> immutableList(E... 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> List<E> immutableList(Iterable<? extends E> iterable)
List instance containing the given elements.
Note that if the input is an immutable list, then the input itself
may be returned.
iterable - the elements that the list should contain, in order
List instance containing those elementspublic static <E> List<E> immutableList(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> List<E> immutableList(Collection<? extends E> collection)
List instance containing the given elements.
Note that if the input is an immutable list, then the input itself
may be returned.
collection - the elements that the list should contain, in order
List instance containing those elementspublic static <E> List<E> immutableList()
immutableList for zero arguments.
Collections.emptyList()
public static <E> List<E> immutableList(@Nullable
E element)
immutableList for one argument.
element - the lone element to be in the returned list
Collections.singletonList(T)public static <E> ArrayList<E> newArrayList()
ArrayList instance.
Note: if you only need an immutable empty List, use Collections.emptyList() instead.
ArrayListpublic static <E> ArrayList<E> newArrayList(E... elements)
ArrayList instance containing the given
elements.
Note: if it is an immutable List you seek, you should use #immutableList.
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> ArrayList<E> newArrayList(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> ArrayList<E> newArrayList(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> 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
IllegalArgumentException - if the specified initial capacity is
negativepublic static <E> 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
IllegalArgumentException - if the specified expected size is negativepublic static <E> LinkedList<E> newLinkedList()
LinkedList instance.
Note: if you only need an immutable empty List,
use Collections.emptyList() instead.
LinkedListpublic static <E> LinkedList<E> newLinkedList(E... elements)
LinkedList instance containing the given elements.
Please see the caveat in newArrayList(Object...).
elements - the elements that the list should contain, in order
LinkedList containing those elementspublic 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> LinkedList<E> newLinkedList(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 Comparable> List<E> sortedCopy(Iterable<E> iterable)
Unlike Sets.newTreeSet(Iterable), this method does not collapse
elements that compare as zero, and the resulting collection does not
maintain its own sort order. If you have no preference on these issues,
these two alternatives are equivalent, so you can choose for performance
factors.
iterable - the elements to be copied and sorted
ClassCastException - if iterable contains elements which are
not mutually comparable
public static <E> List<E> sortedCopy(Iterable<E> iterable,
Comparator<? super E> comparator)
Unlike Sets.newTreeSet(Comparator, Iterable), this method does
not collapse elements that compare as zero, and the resulting collection
does not maintain its own sort order. If you have no preference on these
issues, these two alternatives are equivalent, so you can choose for
performance factors.
iterable - the elements to be copied and sortedcomparator - a comparator capable of sorting the given elements
public 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
(FoofirstFoo,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
(FoofirstFoo,FoosecondFoo,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 <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,
similar to Iterators.transform(java.util.Iterator, com.google.common.base.Function super F, ? extends T>) : changes to fromList will be
reflected in the returned list and vice versa.
Functions are not reversible, so the transform is one-way and new items
cannot be added to the returned list. The add, addAll and
set methods are unsupported in the returned list.
As with Iterators.transform(java.util.Iterator, the function is applied lazily.
This is necessary for returned list to be a view, but also 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. If you want to avoid lazy evaluation and you
don't need the returned list to be a view, you can dump the returned list
into a new list of your choosing. Alternatively, you can use a memoizing
("canonicalizing") function.
If fromList implements RandomAccess, so will the
returned list. TODO: provide similar support for Serializable.
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||