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:org.apache.mahout.knn.tools.TrainNewsGroupsKMeansLogisticRegression.java

public static void main(String[] args) throws IOException, ParseException {
    Options options = new Options();
    options.addOption("i", "input", true,
            "Path to the input folder containing the training set's" + " sequence files.");
    options.addOption("o", "output", true, "Base path to the output file. The name will be "
            + "appended with a suffix for each type of training.");
    options.addOption("a", "actual", false, "If set, runs the training with the actual cluster "
            + "assignments and outputs the model to the output path with a -actual suffix.");
    options.addOption("b", "ballkmeans", false, "If set, runs the training with the ball k-means "
            + "cluster assignments and outputs the model to the output path with a -ballkmeans suffix.");
    options.addOption("s", "streamingkmeans", false,
            "If set, runs the training with the "
                    + "streaming k-means cluster assignments and outputs the model to the output path with a "
                    + "-streamingkmeans suffix.");
    options.addOption("c", "centroids", true, "Path to the centroids seqfile");

    CommandLine cmd = (new PosixParser()).parse(options, args);

    String inputPath = cmd.getOptionValue("input");
    Preconditions.checkNotNull(inputPath);

    String outputBase = cmd.getOptionValue("output");
    Preconditions.checkNotNull(outputBase);

    String centroidsPath = cmd.getOptionValue("centroids");
    Preconditions.checkNotNull(centroidsPath);

    Configuration conf = new Configuration();
    SequenceFileDirIterable<Text, VectorWritable> inputIterable = new SequenceFileDirIterable<Text, VectorWritable>(
            new Path(inputPath), PathType.LIST, conf);

    PrintStream clusterIdOut = new PrintStream(new FileOutputStream("cluster-ids.csv"));
    clusterIdOut.printf("clusterName, clusterId\n");
    int clusterId = 0;
    Map<String, Integer> clusterNamesToIds = Maps.newHashMapWithExpectedSize(NUM_CLASSES);
    for (Pair<Text, VectorWritable> pair : inputIterable) {
        String clusterName = pair.getFirst().toString();
        if (!clusterNamesToIds.containsKey(clusterName)) {
            clusterIdOut.printf("%s, %d\n", clusterName, clusterId);
            clusterNamesToIds.put(clusterName, clusterId++);
        }//from ww  w . ja  va  2  s .  c  o m
    }
    clusterIdOut.close();

    if (cmd.hasOption("actual")) {
        System.out.printf("\nActual clusters models\n");
        System.out.printf("----------------------\n");
        long start = System.currentTimeMillis();
        trainActual(inputIterable, outputBase, clusterNamesToIds);
        long end = System.currentTimeMillis();
        System.out.printf("Trained models for actual clusters. Took %d ms\n", end - start);
    }

    if (cmd.hasOption("ballkmeans") || cmd.hasOption("streamingkmeans")) {
        SequenceFileValueIterable<CentroidWritable> centroidIterable = new SequenceFileValueIterable<CentroidWritable>(
                new Path(centroidsPath), conf);
        List<Centroid> centroids = Lists
                .newArrayList(CreateCentroids.getCentroidsFromCentroidWritableIterable(centroidIterable));

        if (cmd.hasOption("ballkmeans")) {
            System.out.printf("\nBall k-means clusters models\n");
            System.out.printf("----------------------------\n");
            long start = System.currentTimeMillis();
            trainComputed(inputIterable, outputBase, "ballkmeans", clusterNamesToIds,
                    new Pair<Integer, Iterable<Centroid>>(NUM_FEATURES_BKM, centroids));
            long end = System.currentTimeMillis();
            System.out.printf("Trained models for ballkmeans clusters. Took %d ms\n", end - start);
        }

        if (cmd.hasOption("streamingkmeans")) {
            System.out.printf("\nStreaming k-means clusters models\n");
            System.out.printf("---------------------------------\n");
            long start = System.currentTimeMillis();
            trainComputed(inputIterable, outputBase, "streamingkmeans", clusterNamesToIds,
                    new Pair<Integer, Iterable<Centroid>>(centroids.size(), centroids));
            long end = System.currentTimeMillis();
            System.out.printf("Trained models for streamingkmeans clusters. Took %d ms\n", end - start);
        }
    }
}

From source file:net.myrrix.online.eval.ParameterOptimizer.java

public static void main(String[] args) throws Exception {
    if (args.length < 4) {
        System.err.println(/*from   ww  w.j av  a 2s  .  c om*/
                "Usage: dataDirectory numSteps evaluationPercentage property=min:max [property2=min2:max2 ...]");
        return;
    }

    final File dataDir = new File(args[0]);
    Preconditions.checkArgument(dataDir.exists() && dataDir.isDirectory(), "Not a directory: %s", dataDir);
    Preconditions.checkArgument(dataDir.listFiles().length > 0, "No files in: %s", dataDir);
    int numSteps = Integer.parseInt(args[1]);
    Preconditions.checkArgument(numSteps >= 2, "# steps must be at least 2: %s", numSteps);
    final double evaluationPercentage = Double.parseDouble(args[2]);
    Preconditions.checkArgument(evaluationPercentage > 0.0 && evaluationPercentage <= 1.0,
            "evaluationPercentage must be in (0,1]: %s", evaluationPercentage);

    Map<String, ParameterRange> parameterRanges = Maps.newHashMapWithExpectedSize(args.length);
    for (int i = 3; i < args.length; i++) {
        String[] propValue = EQUALS.split(args[i]);
        String systemProperty = propValue[0];
        String[] minMax = COLON.split(propValue[1]);
        ParameterRange range;
        try {
            int min = Integer.parseInt(minMax[0]);
            int max = Integer.parseInt(minMax.length == 1 ? minMax[0] : minMax[1]);
            range = new ParameterRange(min, max);
        } catch (NumberFormatException ignored) {
            double min = Double.parseDouble(minMax[0]);
            double max = Double.parseDouble(minMax.length == 1 ? minMax[0] : minMax[1]);
            range = new ParameterRange(min, max);
        }
        parameterRanges.put(systemProperty, range);
    }

    Callable<Number> evaluator = new Callable<Number>() {
        @Override
        public Number call() throws IOException, TasteException, InterruptedException {
            MyrrixIRStatistics stats = (MyrrixIRStatistics) new PrecisionRecallEvaluator().evaluate(dataDir,
                    0.9, evaluationPercentage, null);
            return stats == null ? null : stats.getMeanAveragePrecision();
        }
    };

    ParameterOptimizer optimizer = new ParameterOptimizer(parameterRanges, numSteps, evaluator, false);
    Map<String, Number> optimalValues = optimizer.findGoodParameterValues();
    System.out.println(optimalValues);
}

From source file:com.opengamma.financial.comparison.PositionSetComparator.java

private static Set<PositionInfo> createPositionInfoSet(final ComparisonContext context,
        final Iterable<Position> positions, final int sizeHint) {
    final Map<PositionInfo, PositionInfo> map = Maps.newHashMapWithExpectedSize(sizeHint);
    for (Position position : positions) {
        final PositionInfo info = new PositionInfo(context, position);
        final PositionInfo previous = map.get(info);
        if (previous != null) {
            previous.associateAdditionalUnderlying(position);
        } else {//from w w w.  j a v  a  2s.  co m
            map.put(info, info);
        }
    }
    return new HashSet<PositionInfo>(map.keySet());
}

From source file:com.google.wave.splash.rpc.RpcParam.java

/**
 * Constructs a map from a list of parameters.
 */// w w  w.  ja va  2 s.  c  o  m
public static Map<String, Object> toMap(RpcParam... params) {
    Map<String, Object> map;
    if (params.length > 0) {
        map = Maps.newHashMapWithExpectedSize(params.length);
        for (RpcParam p : params) {
            map.put(p.getName(), p.getValue());
        }
    } else {
        map = Collections.emptyMap();
    }
    return map;
}

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

@BeforeClass
public static void doSetup() throws Exception {
    Map<String, String> props = Maps.newHashMapWithExpectedSize(1);
    // do lot's of spooling!
    props.put(QueryServices.SERVER_SPOOL_THRESHOLD_BYTES_ATTRIB, Integer.toString(1));
    props.put(QueryServices.CLIENT_SPOOL_THRESHOLD_BYTES_ATTRIB, Integer.toString(1));
    setUpTestDriver(new ReadOnlyProps(props.entrySet().iterator()));
}

From source file:org.javersion.path.PropertyTree.java

public static PropertyTree build(Collection<PropertyPath> paths) {
    Map<PropertyPath, PropertyTree> nodes = Maps.newHashMapWithExpectedSize(paths.size());
    for (PropertyPath path : paths) {
        PropertyTree parentTree = getOrCreate(PropertyPath.ROOT, nodes);
        for (PropertyPath subpath : path) {
            PropertyTree childTree = getOrCreate(subpath, nodes);
            if (parentTree != null) {
                parentTree.children.put(subpath.getName(), childTree);
            }/*  ww  w.  ja v a2 s .  com*/
            parentTree = childTree;
        }
    }
    return nodes.get(PropertyPath.ROOT);
}

From source file:com.opengamma.engine.target.resolver.AbstractIdentifierResolver.java

public static Map<ExternalIdBundle, UniqueId> resolveExternalIdsMultiThread(final PoolExecutor executor,
        final IdentifierResolver resolver, final Collection<ExternalIdBundle> identifiers,
        final VersionCorrection versionCorrection) {
    final PoolExecutor.Service<Void> jobs = executor.createService(null);
    final Map<ExternalIdBundle, UniqueId> result = Maps.newHashMapWithExpectedSize(identifiers.size());
    for (final ExternalIdBundle identifier : identifiers) {
        jobs.execute(new Runnable() {
            @Override/*ww  w.j  ava  2  s  .c o  m*/
            public void run() {
                final UniqueId uid = resolver.resolveExternalId(identifier, versionCorrection);
                if (uid != null) {
                    synchronized (result) {
                        result.put(identifier, uid);
                    }
                }
            }
        });
    }
    try {
        jobs.join();
    } catch (InterruptedException e) {
        throw new OpenGammaRuntimeException("Interrupted", e);
    }
    return result;
}

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

@BeforeClass
@Shadower(classBeingShadowed = BaseClientManagedTimeIT.class)
public static void doSetup() throws Exception {
    Map<String, String> props = Maps.newHashMapWithExpectedSize(1);
    props.put(QueryServices.SPOOL_THRESHOLD_BYTES_ATTRIB, Integer.toString(100));
    // Must update config before starting server
    setUpTestDriver(new ReadOnlyProps(props.entrySet().iterator()));
}

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

@BeforeClass
@Shadower(classBeingShadowed = BaseHBaseManagedTimeIT.class)
public static void doSetup() throws Exception {
    Map<String, String> props = Maps.newHashMapWithExpectedSize(1);
    props.put(QueryServices.SPOOL_THRESHOLD_BYTES_ATTRIB, Integer.toString(1024 * 1024));
    // Must update config before starting server
    setUpTestDriver(new ReadOnlyProps(props.entrySet().iterator()));
}

From source file:com.exoplatform.iversion.object.PropertyTree.java

public static PropertyTree build(Collection<PropertyPath> paths) {
    Map<PropertyPath, PropertyTree> nodes = Maps.newHashMapWithExpectedSize(paths.size());
    for (PropertyPath path : paths) {
        PropertyTree parentTree = null;//  w w w.  jav  a 2 s .  c  o  m
        for (PropertyPath subpath : path) {
            PropertyTree childTree = nodes.get(subpath);
            if (childTree == null) {
                childTree = new PropertyTree(subpath);
                nodes.put(subpath, childTree);
            }
            if (parentTree != null) {
                parentTree.children.put(subpath.getName(), childTree);
            }
            parentTree = childTree;
        }
    }
    return nodes.get(PropertyPath.ROOT);
}