本篇文章为你整理了Lists (Guava: Google Core Libraries for Java 21.0 API)()的详细内容,包含有 Lists (Guava: Google Core Libraries for Java 21.0 API),希望能帮助你了解 Lists (Guava: Google Core Libraries for Java 21.0 API)。
static E List E
asList(Efirst,
E[]rest)
Returns an unmodifiable list containing the specified first element and
backed by the specified array of additional elements.
static E List E
asList(Efirst,
Esecond,
E[]rest)
Returns an unmodifiable list containing the specified first and second
element, and backed by the specified array of additional elements.
static E ArrayList E
newArrayListWithExpectedSize(intestimatedSize)
Creates an ArrayList instance to hold estimatedSize
elements, plus an unspecified amount of padding; you almost
certainly mean to call newArrayListWithCapacity(int) (see that method
for further advice on usage).
newArrayListWithCapacity
@GwtCompatible(serializable=true)
public static E ArrayList E newArrayListWithCapacity(intinitialArraySize)
Creates an ArrayList instance backed by an array with the specified
initial size; simply delegates to ArrayList.ArrayList(int).
Note for Java 7 and later: this method is now unnecessary and
should be treated as deprecated. Instead, use new ArrayList (int) directly, taking
advantage of the new "diamond" syntax.
(Unlike here, there is no risk of overload ambiguity, since the ArrayList constructors very wisely did not accept varargs.)
Parameters:
initialArraySize - the exact size of the initial backing array for
the returned array list (ArrayList documentation calls this
value the "capacity")
Returns:
a new, empty ArrayList which is guaranteed not to resize
itself unless its size reaches initialArraySize + 1
Throws:
IllegalArgumentException - if initialArraySize is negative
newArrayListWithExpectedSize
@GwtCompatible(serializable=true)
public static E ArrayList E newArrayListWithExpectedSize(intestimatedSize)
Creates an ArrayList instance to hold estimatedSize
elements, plus an unspecified amount of padding; you almost
certainly mean to call newArrayListWithCapacity(int) (see that method
for further advice on usage).
Note: This method will soon be deprecated. Even in the rare case
that you do want some amount of padding, its best if you choose your
desired amount explicitly.
Parameters:
estimatedSize - an estimate of the eventual List.size() of
the new list
Returns:
a new, empty ArrayList, sized appropriately to hold the
estimated number of elements
Throws:
IllegalArgumentException - if estimatedSize is negative
newLinkedList
@GwtCompatible(serializable=true)
public static E LinkedList E newLinkedList()
Creates a mutable, empty LinkedList instance (for Java 6 and
earlier).
Note: if you wont be adding any elements to the list, use ImmutableList.of() instead.
Performance note: ArrayList and ArrayDeque consistently outperform LinkedList except in
certain rare and specific situations. Unless you have spent a lot of time
benchmarking your specific needs, use one of those instead.
Note for Java 7 and later: this method is now unnecessary and
should be treated as deprecated. Instead, use the LinkedList
constructor directly, taking advantage
of the new "diamond" syntax.
asList
public static E List E asList(@Nullable
Efirst,
E[]rest)
Returns an unmodifiable list containing the specified first element and
backed by the specified array of additional elements. Changes to the 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.
Parameters:
first - the first element
rest - an array of additional elements, possibly empty
Returns:
an unmodifiable list containing the specified elements
asList
public static E List E asList(@Nullable
Efirst,
@Nullable
Esecond,
E[]rest)
Returns an unmodifiable list containing the specified first and second
element, and backed by the specified array of additional elements. Changes
to the 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.
Parameters:
first - the first element
second - the second element
rest - an array of additional elements, possibly empty
Returns:
an unmodifiable list containing the specified elements
The result is guaranteed to be in the "traditional", lexicographical
order for Cartesian products that you would get from nesting for loops:
for (B b0 : lists.get(0)) {
for (B b1 : lists.get(1)) {
ImmutableList B tuple = ImmutableList.of(b0, b1, ...);
// operate on tuple
}
Note that if any input list is empty, the Cartesian product will also be
empty. If no lists at all are provided (an empty list), the resulting
Cartesian product has one element, an empty list (counter-intuitive, but
mathematically consistent).
Performance notes: while the cartesian product of lists of size
m, n, p is a list of size m x n x p, its actual memory
consumption is much smaller. When the cartesian product is constructed, the
input lists are merely copied. Only as the resulting list is iterated are
the individual lists created, and these are not retained after iteration.
Type Parameters:
B - any common base class shared by all axes (often just Object)
Parameters:
lists - the lists to choose elements from, in the order that
the elements chosen from those lists should appear in the resulting
lists
Returns:
the Cartesian product, as an immutable list containing immutable
lists
Throws:
IllegalArgumentException - if the size of the cartesian product would
be greater than Integer.MAX_VALUE
NullPointerException - if lists, any one of the lists,
or any element of a provided list is null
Since:
The result is guaranteed to be in the "traditional", lexicographical
order for Cartesian products that you would get from nesting for loops:
for (B b0 : lists.get(0)) {
for (B b1 : lists.get(1)) {
ImmutableList B tuple = ImmutableList.of(b0, b1, ...);
// operate on tuple
}
Note that if any input list is empty, the Cartesian product will also be
empty. If no lists at all are provided (an empty list), the resulting
Cartesian product has one element, an empty list (counter-intuitive, but
mathematically consistent).
Performance notes: while the cartesian product of lists of size
m, n, p is a list of size m x n x p, its actual memory
consumption is much smaller. When the cartesian product is constructed, the
input lists are merely copied. Only as the resulting list is iterated are
the individual lists created, and these are not retained after iteration.
Type Parameters:
B - any common base class shared by all axes (often just Object)
Parameters:
lists - the lists to choose elements from, in the order that
the elements chosen from those lists should appear in the resulting
lists
Returns:
the Cartesian product, as an immutable list containing immutable
lists
Throws:
IllegalArgumentException - if the size of the cartesian product would
be greater than Integer.MAX_VALUE
NullPointerException - if lists, any one of the
lists, or any element of a provided list is null
Since:
transform
public static F,T List T transform(List F fromList,
Function ? super F,? extends T function)
Returns a list that applies 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 doesnt 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 is threadsafe if the supplied list and
function are.
If only a Collection or Iterable input is available, use
Collections2.transform(java.util.Collection F , com.google.common.base.Function ? super F, T ) or Iterables.transform(java.lang.Iterable F , com.google.common.base.Function ? super F, ? extends T ).
Note: serializing the returned list is implemented by serializing
fromList, its contents, and function -- not by
serializing the transformed values. This can lead to surprising behavior,
so serializing the returned list is not recommended. Instead,
copy the list using ImmutableList.copyOf(Collection) (for example),
then serialize the copy. Other methods similar to this do not implement
serialization at all for this reason.
Java 8 users: many use cases for this method are better addressed
by Stream.map(java.util.function.Function ? super T, ? extends R ). This method is not being
deprecated, but we gently encourage you to migrate to streams.
partition
public static T List List T partition(List T list,
intsize)
Returns consecutive sublists of a list,
each of the same size (the final list may be smaller). For example,
partitioning a list containing [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.
Parameters:
list - the list to return consecutive sublists of
size - the desired size of each sublist (the last may be
smaller)
Returns:
a list of consecutive sublists
Throws:
IllegalArgumentException - if partitionSize is nonpositive
charactersOf
@Beta
public staticList Character charactersOf(CharSequencesequence)
Returns a view of the specified 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.
Parameters:
sequence - the character sequence to view as a List of
characters
Returns:
an List Character view of the character sequence
Since:
reverse
public static T List T reverse(List T list)
Returns a reversed view of the specified list. For example, 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.
Since:
以上就是Lists (Guava: Google Core Libraries for Java 21.0 API)()的详细内容,想要了解更多 Lists (Guava: Google Core Libraries for Java 21.0 API)的内容,请持续关注盛行IT软件开发工作室。
郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。