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

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

Introduction

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

Prototype

@GwtCompatible(serializable = true)
public static <E> ArrayList<E> newArrayListWithExpectedSize(int estimatedSize) 

Source Link

Document

Creates an ArrayList instance to hold estimatedSize elements, plus an unspecified amount of padding; you almost certainly mean to call #newArrayListWithCapacity (see that method for further advice on usage).

Usage

From source file:com.mgmtp.jfunk.common.random.RandomCollection.java

/**
 * Creates a new instance with the elements of specified list.
 *///from ww  w.  j a  v  a 2  s .c o m
public RandomCollection(final MathRandom random, final Collection<E> elements) {
    this.random = random;
    this.originalElements = Lists.newArrayList(elements);
    this.priorityElements = Lists.newArrayList(elements);
    Collections.shuffle(priorityElements);
    this.currentElements = Lists.newArrayListWithExpectedSize(2 * elements.size());
    this.currentElements.addAll(elements);
}

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

/**
 * Parses the file and returns a list of {@link ResourceItem} objects.
 * @return a list of resources./*  w ww.ja  v a  2 s.co m*/
 *
 * @throws MergingException if a merging exception happens
 */
@NonNull
List<ResourceItem> parseFile() throws MergingException {
    Document document = parseDocument(mFile);

    // get the root node
    Node rootNode = document.getDocumentElement();
    if (rootNode == null) {
        return Collections.emptyList();
    }
    NodeList nodes = rootNode.getChildNodes();

    final int count = nodes.getLength();
    // list containing the result
    List<ResourceItem> resources = Lists.newArrayListWithExpectedSize(count);
    // Multimap to detect dups
    Map<ResourceType, Set<String>> map = Maps.newEnumMap(ResourceType.class);

    for (int i = 0, n = nodes.getLength(); i < n; i++) {
        Node node = nodes.item(i);

        if (node.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }

        ResourceItem resource = getResource(node, mFile);
        if (resource != null) {
            // check this is not a dup
            checkDuplicate(resource, map, mFile);

            resources.add(resource);

            if (resource.getType() == ResourceType.DECLARE_STYLEABLE) {
                // Need to also create ATTR items for its children
                addStyleableItems(node, resources, map, mFile);
            }
        }
    }

    return resources;
}

From source file:com.cloudera.oryx.rdf.common.rule.NumericDecision.java

static List<Decision> numericDecisionsFromExamples(int featureNumber, Iterable<Example> examples,
        int suggestedMaxSplitCandidates) {
    Multiset<Float> sortedFeatureValueCounts = TreeMultiset.create();
    StorelessUnivariateStatistic mean = new Mean();
    int numExamples = 0;
    for (Example example : examples) {
        NumericFeature feature = (NumericFeature) example.getFeature(featureNumber);
        if (feature == null) {
            continue;
        }/*from w ww  .  j  a v a 2s  .  c o m*/
        numExamples++;
        float value = feature.getValue();
        sortedFeatureValueCounts.add(value, 1);
        mean.increment(value);
    }

    // Make decisions from split points that divide up input into roughly equal amounts of examples
    List<Decision> decisions = Lists.newArrayListWithExpectedSize(suggestedMaxSplitCandidates);
    int approxExamplesPerSplit = FastMath.max(1, numExamples / suggestedMaxSplitCandidates);
    int examplesInSplit = 0;
    float lastValue = Float.NaN;
    // This will iterate in order of value by nature of TreeMap
    for (Multiset.Entry<Float> entry : sortedFeatureValueCounts.entrySet()) {
        float value = entry.getElement();
        if (examplesInSplit >= approxExamplesPerSplit) {
            decisions.add(
                    new NumericDecision(featureNumber, (value + lastValue) / 2.0f, (float) mean.getResult()));
            examplesInSplit = 0;
        }
        examplesInSplit += entry.getCount();
        lastValue = value;
    }

    // The vital condition here is that if decision n decides an example is positive, then all subsequent
    // decisions in the list will also find it positive. So we need to order from highest threshold to lowest
    Collections.reverse(decisions);
    return decisions;
}

From source file:org.apache.kylin.metrics.MetricsManager.java

private static void setSourceReporterBindProps(
        Map<ActiveReservoir, List<Pair<String, Properties>>> sourceReporterBindProperties) {
    sourceReporterBindProps = Maps.newHashMapWithExpectedSize(sourceReporterBindProperties.size());
    for (ActiveReservoir activeReservoir : sourceReporterBindProperties.keySet()) {
        List<Pair<Class<? extends ActiveReservoirReporter>, Properties>> values = Lists
                .newArrayListWithExpectedSize(sourceReporterBindProperties.get(activeReservoir).size());
        sourceReporterBindProps.put(activeReservoir, values);
        for (Pair<String, Properties> entry : sourceReporterBindProperties.get(activeReservoir)) {
            try {
                Class clz = Class.forName(entry.getKey());
                if (ActiveReservoirReporter.class.isAssignableFrom(clz)) {
                    values.add(new Pair(clz, entry.getValue()));
                } else {
                    logger.warn("The class " + clz + " is not a sub class of " + ActiveReservoir.class);
                }/* ww w. j  av a  2  s .  c o m*/
            } catch (ClassNotFoundException e) {
                logger.warn("Cannot find class " + entry.getKey());
            }
        }
    }
}

From source file:com.android.tools.idea.rendering.ModuleResourceRepository.java

/**
 * Creates a new resource repository for the given module, <b>not</b> including its dependent modules.
 *
 * @param facet the facet for the module
 * @return the resource repository/*from  w w w . j  a v a  2 s. co  m*/
 */
@NotNull
public static LocalResourceRepository create(@NotNull final AndroidFacet facet) {
    boolean gradleProject = facet.isGradleProject();
    if (!gradleProject) {
        // Always just a single resource folder: simple
        VirtualFile primaryResourceDir = facet.getPrimaryResourceDir();
        if (primaryResourceDir == null) {
            return new EmptyRepository();
        }
        return ResourceFolderRegistry.get(facet, primaryResourceDir);
    }

    ResourceFolderManager folderManager = facet.getResourceFolderManager();
    List<VirtualFile> resourceDirectories = folderManager.getFolders();
    List<LocalResourceRepository> resources = Lists.newArrayListWithExpectedSize(resourceDirectories.size());
    for (VirtualFile resourceDirectory : resourceDirectories) {
        ResourceFolderRepository repository = ResourceFolderRegistry.get(facet, resourceDirectory);
        resources.add(repository);
    }

    DynamicResourceValueRepository dynamicResources = DynamicResourceValueRepository.create(facet);
    resources.add(dynamicResources);

    // We create a ModuleResourceRepository even if resources.isEmpty(), because we may
    // dynamically add children to it later (in updateRoots)
    final ModuleResourceRepository repository = new ModuleResourceRepository(facet, resources);

    // If the model is not yet ready, we may get an incomplete set of resource
    // directories, so in that case update the repository when the model is available.

    folderManager.addListener(new ResourceFolderManager.ResourceFolderListener() {
        @Override
        public void resourceFoldersChanged(@NotNull AndroidFacet facet, @NotNull List<VirtualFile> folders,
                @NotNull Collection<VirtualFile> added, @NotNull Collection<VirtualFile> removed) {
            repository.updateRoots();
        }
    });

    return repository;
}

From source file:com.google.wave.splash.wprime.Index.java

/**
 * Perform a search.// w  w w .java 2s . c o m
 *
 * @param token the token to search for.
 * @return the collection of matching digests.
 */
public Collection<Digest> search(String token) {
    token = token.toLowerCase();

    // return everything in cache.
    if (isAllQuery(token)) {
        return corpus.values();
    }

    Collection<String> resultIds = tokenToWaves.get(token);
    List<Digest> results = Lists.newArrayListWithExpectedSize(resultIds.size());
    for (String resultId : resultIds) {
        results.add(corpus.get(resultId));
    }

    return results;
}

From source file:com.google.devtools.build.lib.util.CompactStringIndexer.java

public CompactStringIndexer(int expectedCapacity) {
    Preconditions.checkArgument(expectedCapacity > 0);
    nodes = Lists.newArrayListWithExpectedSize(expectedCapacity);
    rootId = NOT_FOUND;
}

From source file:org.axdt.as3.ui.util.ToggleAxdtNatureAction.java

private List<IProject> getSelectedProjects(IStructuredSelection sel) {
    List<IProject> result = Lists.newArrayListWithExpectedSize(sel.size());
    for (Object elem : sel.toArray()) {
        IProject project = null;//  w w  w. j  a va2 s.  c  om
        if (elem instanceof IFile)
            project = ((IFile) elem).getProject();
        else if (elem instanceof IProject)
            project = (IProject) elem;
        if (project != null && !result.contains(project))
            result.add(project);
    }
    return result;
}

From source file:org.apache.bookkeeper.statelib.impl.rocksdb.checkpoint.RocksdbRestoreTask.java

private List<String> getFilesToCopy(String checkpointId, File checkpointedDir, CheckpointMetadata metadata)
        throws IOException {
    if (!checkpointedDir.exists()) {
        Files.createDirectories(Paths.get(checkpointedDir.getAbsolutePath()));
    }/*from   w  w  w .ja  v a  2 s . c o m*/

    List<String> filesToCopy = Lists.newArrayListWithExpectedSize(metadata.getFilesCount());
    for (String fileName : metadata.getFilesList()) {
        File localFile = new File(checkpointedDir, fileName);
        if (!localFile.exists()) {
            filesToCopy.add(fileName);
            continue;
        }

        String srcFile;
        if (RocksUtils.isSstFile(localFile)) {
            srcFile = RocksUtils.getDestSstPath(dbPrefix, localFile);
        } else {
            srcFile = RocksUtils.getDestPath(dbPrefix, checkpointId, localFile);
        }

        long srcFileLength = checkpointStore.getFileLength(srcFile);
        long localFileLength = localFile.length();
        if (srcFileLength != localFileLength) {
            filesToCopy.add(fileName);
        }
    }

    return filesToCopy;
}

From source file:com.cloudera.exhibit.sql.FrameTable.java

@Override
public RelDataType getRowType(RelDataTypeFactory typeFactory) {
    List<String> names = Lists.newArrayListWithExpectedSize(descriptor.size());
    List<RelDataType> relTypes = Lists.newArrayListWithExpectedSize(descriptor.size());
    for (int i = 0; i < descriptor.size(); i++) {
        ObsDescriptor.Field f = descriptor.get(i);
        names.add(f.name.toUpperCase());
        relTypes.add(typeFactory.createJavaType(TYPE_CLASSES.get(f.type)));
    }/*from   w  w w .  j  ava  2s  .c  o m*/
    return typeFactory.createStructType(relTypes, names);
}