Example usage for com.google.common.collect Lists newArrayListWithCapacity

List of usage examples for com.google.common.collect Lists newArrayListWithCapacity

Introduction

In this page you can find the example usage for com.google.common.collect Lists newArrayListWithCapacity.

Prototype

@GwtCompatible(serializable = true)
public static <E> ArrayList<E> newArrayListWithCapacity(int initialArraySize) 

Source Link

Document

Creates an ArrayList instance backed by an array with the specified initial size; simply delegates to ArrayList#ArrayList(int) .

Usage

From source file:com.android.ide.common.res2.DuplicateDataException.java

static <I extends DataItem> Message[] createMessages(@NonNull Collection<Collection<I>> duplicateDataItemSets) {
    List<Message> messages = Lists.newArrayListWithCapacity(duplicateDataItemSets.size());
    for (Collection<I> duplicateItems : duplicateDataItemSets) {
        ImmutableList.Builder<SourceFilePosition> positions = ImmutableList.builder();
        for (I item : duplicateItems) {
            if (!item.isRemoved()) {
                positions.add(getPosition(item));
            }/*from  ww  w.  j a  va2 s .  com*/
        }
        messages.add(
                new Message(Message.Kind.ERROR, DUPLICATE_RESOURCES, DUPLICATE_RESOURCES, positions.build()));
    }
    return Iterables.toArray(messages, Message.class);
}

From source file:com.spotify.folsom.ketama.KetamaMemcacheClient.java

private static Collection<RawMemcacheClient> clientsOnly(final Collection<AddressAndClient> addressAndClients) {

    int size = addressAndClients.size();
    final List<RawMemcacheClient> clients = Lists.newArrayListWithCapacity(size);
    for (final AddressAndClient client : addressAndClients) {
        clients.add(client.getClient());
    }/*w ww . j av  a2 s .c  o m*/
    return clients;
}

From source file:com.google.gerrit.server.query.account.AccountPredicates.java

static Predicate<AccountState> defaultPredicate(String query) {
    // Adapt the capacity of this list when adding more default predicates.
    List<Predicate<AccountState>> preds = Lists.newArrayListWithCapacity(3);
    Integer id = Ints.tryParse(query);
    if (id != null) {
        preds.add(id(new Account.Id(id)));
    }//  www  .java 2 s . co  m
    preds.add(equalsName(query));
    preds.add(username(query));
    // Adapt the capacity of the "predicates" list when adding more default
    // predicates.
    return Predicate.or(preds);
}

From source file:com.yahoo.yqlplus.engine.internal.java.backends.java.Chooser.java

public Iterable<List<T>> chooseN(int n, List<T> input) {
    if (n == input.size()) {
        final List<List<T>> result = Lists.newArrayListWithCapacity(1);
        final List<T> copy = Lists.newArrayList(input);
        result.add(copy);//  w ww.ja  va  2 s  . com
        return result;
    } else if (n == 1) {
        return Iterables.transform(input, new Function<T, List<T>>() {

            @Override
            public List<T> apply(T input) {
                final List<T> res = Lists.newArrayListWithCapacity(1);
                res.add(input);
                return res;
            }
        });
    } else if (n > input.size()) {
        return ImmutableList.of();
    } else {
        // should use an exact sized list, or better yet don't materialize the list
        List<List<T>> output = Lists.newArrayList();
        chooseN(n, 0, Lists.<T>newArrayListWithCapacity(n), input, output);
        return output;
    }
}

From source file:com.attribyte.essem.model.Metric.java

/**
 * Keeps only metrics with the specified type.
 * @param metrics The metrics.//from   w w w  .j av a 2 s  .c  o  m
 * @param types The types to keep.
 * @return The filtered list of metrics.
 */
public static List<Metric> keep(final List<Metric> metrics, final EnumSet<Type> types) {
    if (metrics == null || metrics.size() == 0) {
        return metrics;
    } else {
        List<Metric> filtered = Lists.newArrayListWithCapacity(metrics.size());
        for (Metric metric : metrics) {
            if (types.contains(metric.type)) {
                filtered.add(metric);
            }
        }
        return filtered;
    }
}

From source file:org.apache.hadoop.hbase.util.ByteRangeTool.java

public static ArrayList<ByteRange> fromArrays(Collection<byte[]> arrays) {
    if (arrays == null) {
        return new ArrayList<ByteRange>(0);
    }/*w  w w  . j a v  a2s  .co m*/
    ArrayList<ByteRange> ranges = Lists.newArrayListWithCapacity(arrays.size());
    for (byte[] array : arrays) {
        ranges.add(new ByteRange(array));
    }
    return ranges;
}

From source file:com.github.steveash.jg2p.Word.java

public static Word fromNormalString(String normalString) {
    List<String> chars = Lists.newArrayListWithCapacity(normalString.length());
    for (int i = 0; i < normalString.length(); i++) {
        chars.add(normalString.substring(i, i + 1));
    }// w ww  . j av a 2 s  . c  o  m
    return new Word(chars);
}

From source file:com.cinchapi.common.collect.Collectives.java

/**
 * Intelligently merged collectives {@code a} and {@code b}.
 * /*from w ww  . j a  v a2  s. c om*/
 * @param a
 * @param b
 * @return the merged collection
 */
public static Collection<Object> merge(Collection<Object> a, Collection<Object> b) {
    int i = 0;
    int asize = a.size();
    int bsize = b.size();
    Collection<Object> merged = Lists.newArrayListWithCapacity(Math.max(asize, bsize));
    for (; i < Math.min(asize, bsize); ++i) {
        Object ai = Iterables.get(a, i);
        Object bi = Iterables.get(b, i);
        Object merger = merge(ai, bi);
        if (merger != null) {
            merged.add(merger);
        } else {
            if (ai != null) {
                merged.add(ai);
            }
            if (bi != null) {
                merged.add(bi);
            }
        }
    }
    for (; i < a.size(); ++i) {
        merged.add(Iterables.get(a, i));
    }
    for (; i < b.size(); ++i) {
        merged.add(Iterables.get(b, i));
    }
    return merged;
}

From source file:org.novelang.common.tree.TreeTools.java

/**
 * Returns a copy of a {@code Tree} with the {@code newChild} added as first child.
 *
 * @param tree a non-null object that may implement {@link StorageTypeProvider}.
 * @param newChild a non-null object.//from   w ww.j a v  a  2  s .c  o  m
 * @return a non-null object.
 */
public static <T extends Tree<T>> T addFirst(final T tree, final T newChild) {
    final List<T> newChildList = Lists.newArrayListWithCapacity(tree.getChildCount() + 1);
    newChildList.add(Preconditions.checkNotNull(newChild));
    for (int i = 0; i < tree.getChildCount(); i++) {
        newChildList.add(tree.getChildAt(i));
    }
    return tree.adopt(newChildList);
}

From source file:dmh.kuebiko.util.Pair.java

/**
 * Static factory for pair lists. Builds a list of pairs, where the first
 * two items in the data set are the first and second items in the first 
 * pair, and so on.//from   www .j a va2  s  .  c  o m
 * @param data The data that will comprise the pairs. Must contain an even 
 *             number of items.
 * @return An immutable list of pairs build from the passed data set.
 */
public static <T> List<Pair<T, T>> list(T... data) {
    if (data.length % 2 != 0) {
        throw new IllegalArgumentException("Pair list factory requires an even number of arguments.");
    }

    List<Pair<T, T>> pairList = Lists.newArrayListWithCapacity(data.length / 2);
    T pairFirst = null;
    for (T datum : data) {
        if (pairFirst == null) {
            pairFirst = datum;
        } else {
            pairList.add(Pair.of(pairFirst, datum));
            pairFirst = null;
        }
    }
    return Collections.unmodifiableList(pairList);
}