Example usage for com.google.common.collect Maps newHashMapWithExpectedSize

List of usage examples for com.google.common.collect Maps newHashMapWithExpectedSize

Introduction

In this page you can find the example usage for com.google.common.collect Maps newHashMapWithExpectedSize.

Prototype

public static <K, V> HashMap<K, V> newHashMapWithExpectedSize(int expectedSize) 

Source Link

Document

Creates a HashMap instance, with a high enough "initial capacity" that it should hold expectedSize elements without growth.

Usage

From source file:com.cloudera.oryx.kmeans.serving.generation.VectorFactory.java

public static VectorFactory create(MiningSchema schema, LocalTransformations transforms,
        List<ClusteringField> fields) {
    Map<FieldName, DerivedField> derived = Maps
            .newHashMapWithExpectedSize(transforms.getDerivedFields().size());
    for (DerivedField df : transforms.getDerivedFields()) {
        derived.put(df.getName(), df);/* w  w  w.jav a  2s  .co m*/
    }

    Multimap<FieldName, NumericUpdate> numeric = HashMultimap.create();
    Map<FieldName, Map<String, Update>> categorical = Maps.newHashMap();
    for (int j = 0; j < fields.size(); j++) {
        ClusteringField cf = fields.get(j);
        FieldName fn = cf.getField();
        if (derived.containsKey(fn)) {
            DerivedField df = derived.get(fn);
            Expression e = df.getExpression();
            if (e instanceof NormDiscrete) {
                NormDiscrete nd = (NormDiscrete) e;
                Map<String, Update> m = categorical.get(nd.getField());
                if (m == null) {
                    m = Maps.newHashMap();
                    categorical.put(nd.getField(), m);
                }
                m.put(nd.getValue(), new NumericUpdate(ONE, j, cf.getFieldWeight()));
            } else if (e instanceof Apply) {
                Apply apply = (Apply) e;
                if (!"ln".equals(apply.getFunction())) {
                    throw new UnsupportedOperationException(
                            "Unsupported function type: " + apply.getFunction());
                }
                FieldName f = ((FieldRef) apply.getExpressions().get(0)).getField();
                numeric.put(f, new NumericUpdate(LOG_VALUE, j, cf.getFieldWeight()));
            } else if (e instanceof NormContinuous) {
                NormContinuous nc = (NormContinuous) e;
                FieldName f = nc.getField();
                LinearNorm l1 = nc.getLinearNorms().get(0);
                LinearNorm l2 = nc.getLinearNorms().get(1);
                InterpolateFunction ifunc = new InterpolateFunction(l1.getOrig(), l1.getNorm(), l2.getOrig(),
                        l2.getNorm());
                numeric.put(f, new NumericUpdate(ifunc, j, cf.getFieldWeight()));
            } else {
                throw new UnsupportedOperationException("Unsupported expression type: " + e);
            }
        } else {
            numeric.put(fn, new NumericUpdate(VALUE, j, cf.getFieldWeight()));
        }
    }

    boolean sparse = 2 * schema.getMiningFields().size() <= fields.size();
    List<Set<Update>> updates = Lists.newArrayListWithExpectedSize(schema.getMiningFields().size());
    for (MiningField mf : schema.getMiningFields()) {
        FieldName fn = mf.getName();
        if (numeric.containsKey(fn)) {
            updates.add(ImmutableSet.<Update>copyOf(numeric.get(fn)));
        } else if (categorical.containsKey(fn)) {
            CategoricalUpdate u = new CategoricalUpdate(categorical.get(fn));
            updates.add(ImmutableSet.<Update>of(u));
        }
    }
    return new VectorFactory(sparse, fields.size(), updates);
}

From source file:org.apache.phoenix.end2end.StatsCollectorAbstractIT.java

@BeforeClass
public static void doSetup() throws Exception {
    Map<String, String> props = Maps.newHashMapWithExpectedSize(3);
    // Must update config before starting server
    props.put(QueryServices.STATS_GUIDEPOST_WIDTH_BYTES_ATTRIB, Long.toString(20));
    props.put(QueryServices.EXPLAIN_CHUNK_COUNT_ATTRIB, Boolean.TRUE.toString());
    setUpTestDriver(new ReadOnlyProps(props.entrySet().iterator()));
}

From source file:com.palantir.atlasdb.table.generation.ColumnValues.java

public static <T extends Persistable, V extends ColumnValue<?>> Map<Cell, byte[]> toCellValues(
        Multimap<T, V> map, long duration, TimeUnit durationTimeUnit) {
    Map<Cell, byte[]> ret = Maps.newHashMapWithExpectedSize(map.size());
    for (Entry<T, Collection<V>> e : map.asMap().entrySet()) {
        byte[] rowName = e.getKey().persistToBytes();
        for (V val : e.getValue()) {
            ret.put(Cell.create(rowName, val.persistColumnName(), duration, durationTimeUnit),
                    val.persistValue());
        }//  w  ww .j  a  v a  2 s  .  c o  m
    }
    return ret;
}

From source file:com.opengamma.examples.simulated.livedata.SyntheticIdResolver.java

@Override
public Map<ExternalIdBundle, ExternalId> resolve(final Collection<ExternalIdBundle> ids) {
    final Map<ExternalIdBundle, ExternalId> result = Maps.newHashMapWithExpectedSize(ids.size());
    for (ExternalIdBundle id : ids) {
        result.put(id, resolve(id));//from  www. j a va  2 s.c  o m
    }
    return result;
}

From source file:co.cask.cdap.ui.ConfigurationJsonTool.java

public static void exportToJson(Configuration configuration, Appendable output) {
    // Set the log-level of the Configuration logger to ERROR. We don't want warning of deprecated key.
    Level oldLevel = setConfigurationLogLevel(Level.ERROR);

    Map<String, String> map = Maps.newHashMapWithExpectedSize(configuration.size());
    for (Map.Entry<String, String> entry : configuration) {
        map.put(entry.getKey(), entry.getValue());
    }/* www .j a v a 2 s  .  c o m*/
    new GsonBuilder().setPrettyPrinting().create().toJson(map, output);

    // Restore the log level
    setConfigurationLogLevel(oldLevel);
}

From source file:org.sosy_lab.cpachecker.core.algorithm.pdr.transition.Reindexer.java

public static <F extends Formula> F invertIndices(F pOldFormula, SSAMap pFormulaSSAMap, int pLowIndex,
        FormulaManagerView pFormulaManager) {
    Set<String> allVariables = pFormulaSSAMap.allVariables();
    Map<String, IntUnaryOperator> substitution = Maps.newHashMapWithExpectedSize(allVariables.size());
    Set<String> formulaVariables = pFormulaManager.extractVariableNames(pOldFormula);
    for (String variableName : allVariables) {
        int highIndex = pFormulaSSAMap.getIndex(variableName);
        String highVariableName = pFormulaManager
                .instantiate(Collections.singleton(variableName), pFormulaSSAMap).iterator().next();
        final int realHighIndex;
        if (formulaVariables.contains(highVariableName)) {
            realHighIndex = highIndex;//from  w  w w  .ja v  a2  s.  c om
        } else {
            realHighIndex = highIndex - 1;
        }
        substitution.put(variableName, index -> realHighIndex - index + pLowIndex);
    }
    return reindex(pOldFormula, pFormulaSSAMap, (var, i) -> substitution.get(var).applyAsInt(i),
            pFormulaManager);
}

From source file:net.automatalib.incremental.mealy.tree.dynamic.Node.java

Node(int expectedSize) {
    this.outEdges = Maps.newHashMapWithExpectedSize(expectedSize);
}

From source file:uk.ac.ebi.mdk.hsql.handler.MoleculeCache.java

public MoleculeCache(int capacity) {
    cache = Maps.newHashMapWithExpectedSize(capacity);
}

From source file:com.yahoo.yqlplus.engine.internal.plan.types.base.IntegerSwitchSequence.java

@Override
public void generate(CodeEmitter code) {
    Map<Integer, Label> labelMap = Maps.newHashMapWithExpectedSize(sequenceMap.size());
    for (Integer key : sequenceMap.keySet()) {
        labelMap.put(key, new Label());
    }//from   w  ww  . ja v a2 s  .c o m
    Label done = new Label();
    Label defaultCase = defaultSequence == BytecodeSequence.NOOP ? done : new Label();
    code.emitIntegerSwitch(labelMap, defaultCase);
    MethodVisitor mv = code.getMethodVisitor();
    for (Map.Entry<Integer, BytecodeSequence> e : sequenceMap.entrySet()) {
        mv.visitLabel(labelMap.get(e.getKey()));
        code.exec(e.getValue());
        mv.visitJumpInsn(Opcodes.GOTO, done);
    }
    if (defaultCase != done) {
        mv.visitLabel(defaultCase);
        code.exec(defaultSequence);
    }
    mv.visitLabel(done);

}

From source file:org.eclipse.emf.ecore.xcore.scoping.types.CachingTypeScope.java

public CachingTypeScope(AbstractXcoreScope parent) {
    this.parent = parent;
    this.cache = Maps.newHashMapWithExpectedSize(50);
}