/** * Creates an {@code ArrayList} instance to hold {@code estimatedSize} elements, <i>plus</i> an * unspecified amount of padding; you almost certainly mean to call {@link * #newArrayListWithCapacity} (see that method for further advice on usage). * * <p><b>Note:</b> This method will soon be deprecated. Even in the rare case that you do want * some amount of padding, it's best if you choose your desired amount explicitly. * * @param estimatedSize an estimate of the eventual {@link List#size()} of the new list * @return a new, empty {@code ArrayList}, sized appropriately to hold the estimated number of * elements * @throws IllegalArgumentException if {@code estimatedSize} is negative */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayListWithExpectedSize(int estimatedSize) { return new ArrayList<>(computeArrayListCapacity(estimatedSize)); }
/** * Creates a <i>mutable</i> {@code ArrayList} instance containing the given elements. * * <p><b>Note:</b> essentially the only reason to use this method is when you will need to add or * remove elements later. Otherwise, for non-null elements use {@link ImmutableList#of()} (for * varargs) or {@link ImmutableList#copyOf(Object[])} (for an array) instead. If any elements * might be null, or you need support for {@link List#set(int, Object)}, use {@link * Arrays#asList}. * * <p>Note that even when you do need the ability to add or remove, this method provides only a * tiny bit of syntactic sugar for {@code newArrayList(}{@link Arrays#asList asList}{@code * (...))}, or for creating an empty list then calling {@link Collections#addAll}. This method is * not actually very useful and will likely be deprecated in the future. */ @SafeVarargs @CanIgnoreReturnValue // TODO(kak): Remove this @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayList(E... elements) { checkNotNull(elements); // for GWT // Avoid integer overflow when a large array is passed in int capacity = computeArrayListCapacity(elements.length); ArrayList<E> list = new ArrayList<>(capacity); Collections.addAll(list, elements); return list; }
/** * Creates an {@code ArrayList} instance to hold {@code estimatedSize} elements, <i>plus</i> an * unspecified amount of padding; you almost certainly mean to call {@link * #newArrayListWithCapacity} (see that method for further advice on usage). * * <p><b>Note:</b> This method will soon be deprecated. Even in the rare case that you do want * some amount of padding, it's best if you choose your desired amount explicitly. * * @param estimatedSize an estimate of the eventual {@link List#size()} of the new list * @return a new, empty {@code ArrayList}, sized appropriately to hold the estimated number of * elements * @throws IllegalArgumentException if {@code estimatedSize} is negative */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayListWithExpectedSize(int estimatedSize) { return new ArrayList<>(computeArrayListCapacity(estimatedSize)); }
public void testComputeArrayListCapacity() { assertEquals(5, Lists.computeArrayListCapacity(0)); assertEquals(13, Lists.computeArrayListCapacity(8)); assertEquals(89, Lists.computeArrayListCapacity(77)); assertEquals(22000005, Lists.computeArrayListCapacity(20000000)); assertEquals(Integer.MAX_VALUE, Lists.computeArrayListCapacity(Integer.MAX_VALUE - 1000)); }
/** * Creates an {@code ArrayList} instance to hold {@code estimatedSize} elements, <i>plus</i> an * unspecified amount of padding; you almost certainly mean to call {@link * #newArrayListWithCapacity} (see that method for further advice on usage). * * <p><b>Note:</b> This method will soon be deprecated. Even in the rare case that you do want * some amount of padding, it's best if you choose your desired amount explicitly. * * @param estimatedSize an estimate of the eventual {@link List#size()} of the new list * @return a new, empty {@code ArrayList}, sized appropriately to hold the estimated number of * elements * @throws IllegalArgumentException if {@code estimatedSize} is negative */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayListWithExpectedSize(int estimatedSize) { return new ArrayList<>(computeArrayListCapacity(estimatedSize)); }
/** * Creates a <i>mutable</i> {@code ArrayList} instance containing the given elements. * * <p><b>Note:</b> essentially the only reason to use this method is when you will need to add or * remove elements later. Otherwise, for non-null elements use {@link ImmutableList#of()} (for * varargs) or {@link ImmutableList#copyOf(Object[])} (for an array) instead. If any elements * might be null, or you need support for {@link List#set(int, Object)}, use {@link * Arrays#asList}. * * <p>Note that even when you do need the ability to add or remove, this method provides only a * tiny bit of syntactic sugar for {@code newArrayList(}{@link Arrays#asList asList}{@code * (...))}, or for creating an empty list then calling {@link Collections#addAll}. This method is * not actually very useful and will likely be deprecated in the future. */ @SafeVarargs @CanIgnoreReturnValue // TODO(kak): Remove this @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayList(E... elements) { checkNotNull(elements); // for GWT // Avoid integer overflow when a large array is passed in int capacity = computeArrayListCapacity(elements.length); ArrayList<E> list = new ArrayList<>(capacity); Collections.addAll(list, elements); return list; }
/** * Creates a <i>mutable</i> {@code ArrayList} instance containing the given elements. * * <p><b>Note:</b> essentially the only reason to use this method is when you will need to add or * remove elements later. Otherwise, for non-null elements use {@link ImmutableList#of()} (for * varargs) or {@link ImmutableList#copyOf(Object[])} (for an array) instead. If any elements * might be null, or you need support for {@link List#set(int, Object)}, use {@link * Arrays#asList}. * * <p>Note that even when you do need the ability to add or remove, this method provides only a * tiny bit of syntactic sugar for {@code newArrayList(}{@link Arrays#asList asList}{@code * (...))}, or for creating an empty list then calling {@link Collections#addAll}. This method is * not actually very useful and will likely be deprecated in the future. */ @SafeVarargs @CanIgnoreReturnValue // TODO(kak): Remove this @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayList(E... elements) { checkNotNull(elements); // for GWT // Avoid integer overflow when a large array is passed in int capacity = computeArrayListCapacity(elements.length); ArrayList<E> list = new ArrayList<>(capacity); Collections.addAll(list, elements); return list; }
/** * Creates an {@code ArrayList} instance with the given expected size. * * @param expectedSize the expected size of the list * @return a newly-created, initially empty {@code ArrayList} with enough * capacity for the given expected size * @throws IllegalArgumentException if the specified expected size is negative */ public static <E> ArrayList<E> newArrayListWithExpectedSize(int expectedSize) { return new ArrayList<E>(computeArrayListCapacity(expectedSize)); }
/** * Creates a <i>mutable</i> {@code ArrayList} instance containing the given * elements. * * <p><b>Note:</b> if mutability is not required and the elements are * non-null, use {@link ImmutableList#of(Object[])} instead. * * @param elements the elements that the list should contain, in order * @return a new {@code ArrayList} containing those elements */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayList(E... elements) { checkNotNull(elements); // for GWT // Avoid integer overflow when a large array is passed in int capacity = computeArrayListCapacity(elements.length); ArrayList<E> list = new ArrayList<E>(capacity); Collections.addAll(list, elements); return list; }
/** * Creates an {@code ArrayList} instance to hold {@code estimatedSize} elements, <i>plus</i> an * unspecified amount of padding; you almost certainly mean to call {@link * #newArrayListWithCapacity} (see that method for further advice on usage). * * <p><b>Note:</b> This method will soon be deprecated. Even in the rare case that you do want * some amount of padding, it's best if you choose your desired amount explicitly. * * @param estimatedSize an estimate of the eventual {@link List#size()} of the new list * @return a new, empty {@code ArrayList}, sized appropriately to hold the estimated number of * elements * @throws IllegalArgumentException if {@code estimatedSize} is negative */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayListWithExpectedSize(int estimatedSize) { return new ArrayList<>(computeArrayListCapacity(estimatedSize)); }
/** * Creates an {@code ArrayList} instance to hold {@code estimatedSize} elements, <i>plus</i> an * unspecified amount of padding; you almost certainly mean to call {@link * #newArrayListWithCapacity} (see that method for further advice on usage). * * <p><b>Note:</b> This method will soon be deprecated. Even in the rare case that you do want * some amount of padding, it's best if you choose your desired amount explicitly. * * @param estimatedSize an estimate of the eventual {@link List#size()} of the new list * @return a new, empty {@code ArrayList}, sized appropriately to hold the estimated number of * elements * @throws IllegalArgumentException if {@code estimatedSize} is negative */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayListWithExpectedSize(int estimatedSize) { return new ArrayList<>(computeArrayListCapacity(estimatedSize)); }
/** * Creates a <i>mutable</i> {@code ArrayList} instance containing the given * elements. * * <p><b>Note:</b> if mutability is not required and the elements are * non-null, use {@link ImmutableList#of(Object[])} instead. * * @param elements the elements that the list should contain, in order * @return a new {@code ArrayList} containing those elements */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayList(E... elements) { checkNotNull(elements); // for GWT // Avoid integer overflow when a large array is passed in int capacity = computeArrayListCapacity(elements.length); ArrayList<E> list = new ArrayList<E>(capacity); Collections.addAll(list, elements); return list; }
/** * Creates a <i>mutable</i> {@code ArrayList} instance containing the given * elements. * * <p><b>Note:</b> if mutability is not required and the elements are * non-null, use {@link ImmutableList#of(Object[])} instead. * * @param elements the elements that the list should contain, in order * @return a new {@code ArrayList} containing those elements */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayList(E... elements) { checkNotNull(elements); // for GWT // Avoid integer overflow when a large array is passed in int capacity = computeArrayListCapacity(elements.length); ArrayList<E> list = new ArrayList<E>(capacity); Collections.addAll(list, elements); return list; }
/** * Creates an {@code ArrayList} instance sized appropriately to hold an * <i>estimated</i> number of elements without resizing. A small amount of * padding is added in case the estimate is low. * * <p><b>Note:</b> If you know the <i>exact</i> number of elements the list * will hold, or prefer to calculate your own amount of padding, refer to * {@link #newArrayListWithCapacity(int)}. * * @param estimatedSize an estimate of the eventual {@link List#size()} of * the new list * @return a new, empty {@code ArrayList}, sized appropriately to hold the * estimated number of elements * @throws IllegalArgumentException if {@code estimatedSize} is negative */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayListWithExpectedSize( int estimatedSize) { return new ArrayList<E>(computeArrayListCapacity(estimatedSize)); }
/** * Creates an {@code ArrayList} instance to hold {@code estimatedSize} * elements, <i>plus</i> an unspecified amount of padding; you almost * certainly mean to call {@link #newArrayListWithCapacity} (see that method * for further advice on usage). * * <p><b>Note:</b> This method will soon be deprecated. Even in the rare case * that you do want some amount of padding, it's best if you choose your * desired amount explicitly. * * @param estimatedSize an estimate of the eventual {@link List#size()} of * the new list * @return a new, empty {@code ArrayList}, sized appropriately to hold the * estimated number of elements * @throws IllegalArgumentException if {@code estimatedSize} is negative */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayListWithExpectedSize(int estimatedSize) { return new ArrayList<E>(computeArrayListCapacity(estimatedSize)); }
/** * Creates an {@code ArrayList} instance sized appropriately to hold an * <i>estimated</i> number of elements without resizing. A small amount of * padding is added in case the estimate is low. * * <p><b>Note:</b> If you know the <i>exact</i> number of elements the list * will hold, or prefer to calculate your own amount of padding, refer to * {@link #newArrayListWithCapacity(int)}. * * @param estimatedSize an estimate of the eventual {@link List#size()} of * the new list * @return a new, empty {@code ArrayList}, sized appropriately to hold the * estimated number of elements * @throws IllegalArgumentException if {@code estimatedSize} is negative */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayListWithExpectedSize( int estimatedSize) { return new ArrayList<E>(computeArrayListCapacity(estimatedSize)); }
/** * Creates an {@code ArrayList} instance sized appropriately to hold an * <i>estimated</i> number of elements without resizing. A small amount of * padding is added in case the estimate is low. * * <p><b>Note:</b> If you know the <i>exact</i> number of elements the list * will hold, or prefer to calculate your own amount of padding, refer to * {@link #newArrayListWithCapacity(int)}. * * @param estimatedSize an estimate of the eventual {@link List#size()} of * the new list * @return a new, empty {@code ArrayList}, sized appropriately to hold the * estimated number of elements * @throws IllegalArgumentException if {@code estimatedSize} is negative */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayListWithExpectedSize( int estimatedSize) { return new ArrayList<E>(computeArrayListCapacity(estimatedSize)); }
/** * Creates an {@code ArrayList} instance sized appropriately to hold an * <i>estimated</i> number of elements without resizing. A small amount of * padding is added in case the estimate is low. * * <p><b>Note:</b> If you know the <i>exact</i> number of elements the list * will hold, or prefer to calculate your own amount of padding, refer to * {@link #newArrayListWithCapacity(int)}. * * @param estimatedSize an estimate of the eventual {@link List#size()} of * the new list * @return a new, empty {@code ArrayList}, sized appropriately to hold the * estimated number of elements * @throws IllegalArgumentException if {@code estimatedSize} is negative */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayListWithExpectedSize( int estimatedSize) { return new ArrayList<E>(computeArrayListCapacity(estimatedSize)); }
/** * Creates a <i>mutable</i> {@code ArrayList} instance containing the given * elements. * * <p><b>Note:</b> if mutability is not required and the elements are * non-null, use an overload of {@link ImmutableList#of()} (for varargs) or * {@link ImmutableList#copyOf(Object[])} (for an array) instead. * * @param elements the elements that the list should contain, in order * @return a new {@code ArrayList} containing those elements */ @GwtCompatible(serializable = true) public static <E> ArrayList<E> newArrayList(E... elements) { checkNotNull(elements); // for GWT // Avoid integer overflow when a large array is passed in int capacity = computeArrayListCapacity(elements.length); ArrayList<E> list = new ArrayList<E>(capacity); Collections.addAll(list, elements); return list; }
public void testComputeArrayListCapacity() { assertEquals(5, Lists.computeArrayListCapacity(0)); assertEquals(13, Lists.computeArrayListCapacity(8)); assertEquals(89, Lists.computeArrayListCapacity(77)); assertEquals(22000005, Lists.computeArrayListCapacity(20000000)); assertEquals(Integer.MAX_VALUE, Lists.computeArrayListCapacity(Integer.MAX_VALUE - 1000)); }