Example usage for java.util List set

List of usage examples for java.util List set

Introduction

In this page you can find the example usage for java.util List set.

Prototype

E set(int index, E element);

Source Link

Document

Replaces the element at the specified position in this list with the specified element (optional operation).

Usage

From source file:edu.umn.cs.spatialHadoop.operations.Union.java

private static <S extends OGCJTSShape> void unionLocal(Path inPath, Path outPath, final OperationsParams params)
        throws IOException, InterruptedException, ClassNotFoundException {
    // 1- Split the input path/file to get splits that can be processed independently
    final SpatialInputFormat3<Rectangle, S> inputFormat = new SpatialInputFormat3<Rectangle, S>();
    Job job = Job.getInstance(params);//from  w  ww .  ja  v a 2s  .c o  m
    SpatialInputFormat3.setInputPaths(job, inPath);
    final List<InputSplit> splits = inputFormat.getSplits(job);
    int parallelism = params.getInt("parallel", Runtime.getRuntime().availableProcessors());

    // 2- Process splits in parallel
    final List<Float> progresses = new Vector<Float>();
    final IntWritable overallProgress = new IntWritable(0);
    List<List<Geometry>> results = Parallel.forEach(splits.size(), new RunnableRange<List<Geometry>>() {
        @Override
        public List<Geometry> run(final int i1, final int i2) {
            final int pi;
            final IntWritable splitsProgress = new IntWritable();
            synchronized (progresses) {
                pi = progresses.size();
                progresses.add(0f);
            }
            final float progressRatio = (i2 - i1) / (float) splits.size();
            Progressable progress = new Progressable.NullProgressable() {
                @Override
                public void progress(float p) {
                    progresses.set(pi, p * ((splitsProgress.get() - i1) / (float) (i2 - i1)) * progressRatio);
                    float sum = 0;
                    for (float f : progresses)
                        sum += f;
                    int newProgress = (int) (sum * 100);
                    if (newProgress > overallProgress.get()) {
                        overallProgress.set(newProgress);
                        LOG.info("Local union progress " + newProgress + "%");
                    }
                }
            };

            final List<Geometry> localUnion = new ArrayList<Geometry>();
            ResultCollector<Geometry> output = new ResultCollector<Geometry>() {
                @Override
                public void collect(Geometry r) {
                    localUnion.add(r);
                }
            };

            final int MaxBatchSize = 100000;
            Geometry[] batch = new Geometry[MaxBatchSize];
            int batchSize = 0;
            for (int i = i1; i < i2; i++) {
                splitsProgress.set(i);
                try {
                    FileSplit fsplit = (FileSplit) splits.get(i);
                    final RecordReader<Rectangle, Iterable<S>> reader = inputFormat.createRecordReader(fsplit,
                            null);
                    if (reader instanceof SpatialRecordReader3) {
                        ((SpatialRecordReader3) reader).initialize(fsplit, params);
                    } else if (reader instanceof RTreeRecordReader3) {
                        ((RTreeRecordReader3) reader).initialize(fsplit, params);
                    } else if (reader instanceof HDFRecordReader) {
                        ((HDFRecordReader) reader).initialize(fsplit, params);
                    } else {
                        throw new RuntimeException("Unknown record reader");
                    }
                    while (reader.nextKeyValue()) {
                        Iterable<S> shapes = reader.getCurrentValue();
                        for (S s : shapes) {
                            if (s.geom == null)
                                continue;
                            batch[batchSize++] = s.geom;
                            if (batchSize >= MaxBatchSize) {
                                SpatialAlgorithms.multiUnion(batch, progress, output);
                                batchSize = 0;
                            }
                        }
                    }
                    reader.close();
                } catch (IOException e) {
                    LOG.error("Error processing split " + splits.get(i), e);
                } catch (InterruptedException e) {
                    LOG.error("Error processing split " + splits.get(i), e);
                }
            }
            // Union all remaining geometries
            try {
                Geometry[] finalBatch = new Geometry[batchSize];
                System.arraycopy(batch, 0, finalBatch, 0, batchSize);
                SpatialAlgorithms.multiUnion(finalBatch, progress, output);
                return localUnion;
            } catch (IOException e) {
                // Should never happen as the context is passed as null
                throw new RuntimeException("Error in local union", e);
            }
        }
    }, parallelism);

    // Write result to output
    LOG.info("Merge the results of all splits");
    int totalNumGeometries = 0;
    for (List<Geometry> result : results)
        totalNumGeometries += result.size();
    List<Geometry> allInOne = new ArrayList<Geometry>(totalNumGeometries);
    for (List<Geometry> result : results)
        allInOne.addAll(result);

    final S outShape = (S) params.getShape("shape");
    final PrintStream out;
    if (outPath == null || !params.getBoolean("output", true)) {
        // Skip writing the output
        out = new PrintStream(new NullOutputStream());
    } else {
        FileSystem outFS = outPath.getFileSystem(params);
        out = new PrintStream(outFS.create(outPath));
    }

    SpatialAlgorithms.multiUnion(allInOne.toArray(new Geometry[allInOne.size()]),
            new Progressable.NullProgressable() {
                int lastProgress = 0;

                public void progress(float p) {
                    int newProgresss = (int) (p * 100);
                    if (newProgresss > lastProgress) {
                        LOG.info("Global union progress " + (lastProgress = newProgresss) + "%");
                    }
                }
            }, new ResultCollector<Geometry>() {
                Text line = new Text2();

                @Override
                public void collect(Geometry r) {
                    outShape.geom = r;
                    outShape.toText(line);
                    out.println(line);
                }
            });
    out.close();
}

From source file:cn.ctyun.amazonaws.auth.AWS3Signer.java

protected String getCanonicalizedHeadersForStringToSign(Request<?> request) {
    List<String> headersToSign = getHeadersForStringToSign(request);

    for (int i = 0; i < headersToSign.size(); i++) {
        headersToSign.set(i, headersToSign.get(i).toLowerCase());
    }//from w w  w.j  ava2  s .  c  om

    SortedMap<String, String> sortedHeaderMap = new TreeMap<String, String>();
    for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
        if (headersToSign.contains(entry.getKey().toLowerCase())) {
            sortedHeaderMap.put(entry.getKey().toLowerCase(), entry.getValue());
        }
    }

    StringBuilder builder = new StringBuilder();
    for (Map.Entry<String, String> entry : sortedHeaderMap.entrySet()) {
        builder.append(entry.getKey().toLowerCase()).append(":").append(entry.getValue()).append("\n");
    }

    return builder.toString();
}

From source file:core.com.qiniu.auth.AWS3Signer.java

protected String getCanonicalizedHeadersForStringToSign(Request<?> request) {
    List<String> headersToSign = getHeadersForStringToSign(request);

    for (int i = 0; i < headersToSign.size(); i++) {
        headersToSign.set(i, StringUtils.lowerCase(headersToSign.get(i)));
    }/*ww w. j  a  va2 s .c  o  m*/

    SortedMap<String, String> sortedHeaderMap = new TreeMap<String, String>();
    for (Map.Entry<String, String> entry : request.getHeaders().entrySet()) {
        if (headersToSign.contains(StringUtils.lowerCase(entry.getKey()))) {
            sortedHeaderMap.put(StringUtils.lowerCase(entry.getKey()), entry.getValue());
        }
    }

    StringBuilder builder = new StringBuilder();
    for (Map.Entry<String, String> entry : sortedHeaderMap.entrySet()) {
        builder.append(StringUtils.lowerCase(entry.getKey())).append(":").append(entry.getValue()).append("\n");
    }

    return builder.toString();
}

From source file:gov.nih.nci.caarray.plugins.nimblegen.PairDataHandler.java

private Map<String, String> getHeaderMetadata(DelimitedFileReader reader) throws IOException {
    reset(reader);//ww  w .ja  v  a 2 s.  c om
    final Map<String, String> result = new HashMap<String, String>();
    final List<String> line = reader.nextLine();
    line.set(0, line.get(0).substring(2));
    for (final String value : line) {
        final String[] v = value.split("=");
        if (v.length < 2) {
            continue;
        }
        result.put(v[0], v[1]);
    }
    return result;
}

From source file:com.opengamma.analytics.math.curve.InterpolatedCurveShiftFunction.java

/**
 * {@inheritDoc}//from  www .j av  a2s  . c  o  m
 */
@Override
public InterpolatedDoublesCurve evaluate(final InterpolatedDoublesCurve curve, final double[] xShift,
        final double[] yShift, final String newName) {
    Validate.notNull(curve, "curve");
    Validate.notNull(xShift, "x shifts");
    Validate.notNull(yShift, "y shifts");
    Validate.isTrue(xShift.length == yShift.length);
    if (xShift.length == 0) {
        return InterpolatedDoublesCurve.from(curve.getXDataAsPrimitive(), curve.getYDataAsPrimitive(),
                curve.getInterpolator(), newName);
    }
    final List<Double> newX = new ArrayList<Double>(Arrays.asList(curve.getXData()));
    final List<Double> newY = new ArrayList<Double>(Arrays.asList(curve.getYData()));
    for (int i = 0; i < xShift.length; i++) {
        final int index = newX.indexOf(xShift[i]);
        if (index >= 0) {
            newY.set(index, newY.get(index) + yShift[i]);
        } else {
            newX.add(xShift[i]);
            newY.add(curve.getYValue(xShift[i]) + yShift[i]);
        }
    }
    return InterpolatedDoublesCurve.from(newX, newY, curve.getInterpolator(), newName);
}

From source file:se.uu.it.cs.recsys.ruleminer.datastructure.builder.FPTreeHeaderTableBuilder.java

/**
 *
 * @param idAndCount item id and count//from   ww  w  . j  a v a 2 s .c o  m
 * @param threshold, min support
 * @return non-null HeaderTableItem if the input contains id meets threshold
 * requirement; otherwise null.
 */
public static List<HeaderTableItem> build(Map<Integer, Integer> idAndCount, Integer threshold) {

    List<HeaderTableItem> instance = new ArrayList<>();

    Map<Integer, Integer> filteredIdAndCount = Util.filter(idAndCount, threshold);

    if (filteredIdAndCount.isEmpty()) {
        LOGGER.debug("Empty map after filtering. Empty list will be returned. Source: {}", idAndCount);
        return instance;
    }

    List<Integer> countList = new ArrayList<>(filteredIdAndCount.values());
    Collections.sort(countList);
    Collections.reverse(countList);// now the count is in DESC order

    for (int i = 1; i <= filteredIdAndCount.size(); i++) {
        instance.add(new HeaderTableItem()); // in order to call list.set(idx,elem)
    }

    Map<Integer, Set<Integer>> forIdsHavingSameCount = new HashMap<>();//different ids may have same count

    filteredIdAndCount.entrySet().forEach((entry) -> {
        Integer courseId = entry.getKey();
        Integer count = entry.getValue();

        Integer countFrequence = Collections.frequency(countList, count);

        if (countFrequence == 1) {
            Integer countIdx = countList.indexOf(count);
            instance.set(countIdx, new HeaderTableItem(new Item(courseId, count)));
        } else {
            // different ids have same count

            if (!forIdsHavingSameCount.containsKey(count)) {
                forIdsHavingSameCount.put(count, Util.findDuplicatesIndexes(countList, count));
            }

            Iterator<Integer> itr = forIdsHavingSameCount.get(count).iterator();
            Integer idx = itr.next();
            itr.remove();

            instance.set(idx, new HeaderTableItem(new Item(courseId, count)));
        }

    });

    //        LOGGER.debug("Final built header table: {}",
    //                instance.stream()
    //                .map(headerItem -> headerItem.getItem()).collect(Collectors.toList()));

    return instance;
}

From source file:com.sangupta.clitools.file.Trim.java

@Override
protected boolean processFile(File file) throws IOException {
    // read entire file in memory
    List<String> contents = FileUtils.readLines(file);
    final long initial = file.length();

    // now for each string - trim from end
    for (int index = 0; index < contents.size(); index++) {
        String line = contents.get(index);
        line = StringUtils.strip(line, null);
        contents.set(index, line);
    }//from   www.java 2  s  .  c o  m

    // write back contents of file
    FileUtils.writeLines(file, contents);
    long current = file.length();

    this.bytesSaved.addAndGet(initial - current);
    System.out.println("File " + file.getAbsoluteFile().getAbsolutePath() + " trimmed and saved "
            + (initial - current) + " bytes.");
    return true;
}

From source file:com.amalto.core.history.accessor.record.ManyValue.java

@Override
public void set(MetadataRepository repository, DataRecord record, PathElement element, String value) {
    List list = (List) record.get(element.field);
    if (list == null) {
        list = new ArrayList(element.index);
        record.set(element.field, list);
    }/*from   w  w  w.jav  a 2  s . c  om*/
    while (element.index >= list.size()) {
        list.add(null);
    }
    if (value == null) {
        list.set(element.index, null);
    }
    if (element.field instanceof ReferenceFieldMetadata) {
        ComplexTypeMetadata referencedType = ((ReferenceFieldMetadata) element.field).getReferencedType();
        DataRecord referencedRecord = (DataRecord) StorageMetadataUtils.convert(String.valueOf(value),
                element.field, referencedType);
        list.set(element.index, referencedRecord);
    } else {
        list.set(element.index, StorageMetadataUtils.convert(String.valueOf(value), element.field));
    }
}

From source file:com.sangupta.clitools.file.RightTrim.java

@Override
protected boolean processFile(File file) throws IOException {
    // read entire file in memory
    List<String> contents = FileUtils.readLines(file);
    final long initial = file.length();

    // now for each string - trim from end
    for (int index = 0; index < contents.size(); index++) {
        String line = contents.get(index);
        line = StringUtils.stripEnd(line, null);
        contents.set(index, line);
    }/*w  ww.  j  a v a  2 s. c om*/

    // write back contents of file
    FileUtils.writeLines(file, contents);
    long current = file.length();

    this.bytesSaved.addAndGet(initial - current);
    System.out.println("File " + file.getAbsoluteFile().getAbsolutePath() + " right-trimmed and saved "
            + (initial - current) + " bytes.");
    return true;
}

From source file:de.bund.bfr.math.MultivariateOptimization.java

@Override
public Result optimize(int nParameterSpace, int nOptimizations, boolean stopWhenSuccessful,
        Map<String, Double> minStartValues, Map<String, Double> maxStartValues, int maxIterations,
        DoubleConsumer progessListener, ExecutionContext exec) throws CanceledExecutionException {
    if (exec != null) {
        exec.checkCanceled();//ww  w . ja  v  a 2  s  .co  m
    }

    progessListener.accept(0.0);

    List<ParamRange> ranges = MathUtils.getParamRanges(parameters, minStartValues, maxStartValues,
            nParameterSpace);

    ranges.set(parameters.indexOf(sdParam), new ParamRange(1.0, 1, 1.0));

    List<StartValues> startValuesList = MathUtils.createStartValuesList(ranges, nOptimizations,
            values -> optimizerFunction.value(Doubles.toArray(values)),
            progress -> progessListener.accept(0.5 * progress), exec);
    Result result = new Result();
    AtomicInteger currentIteration = new AtomicInteger();
    SimplexOptimizer optimizer = new SimplexOptimizer(new SimpleValueChecker(1e-10, 1e-10) {

        @Override
        public boolean converged(int iteration, PointValuePair previous, PointValuePair current) {
            if (super.converged(iteration, previous, current)) {
                return true;
            }

            return currentIteration.incrementAndGet() >= maxIterations;
        }
    });
    int count = 0;

    for (StartValues startValues : startValuesList) {
        if (exec != null) {
            exec.checkCanceled();
        }

        progessListener.accept(0.5 * count++ / startValuesList.size() + 0.5);

        try {
            PointValuePair optimizerResults = optimizer.optimize(new MaxEval(Integer.MAX_VALUE),
                    new MaxIter(maxIterations), new InitialGuess(Doubles.toArray(startValues.getValues())),
                    new ObjectiveFunction(optimizerFunction), GoalType.MAXIMIZE,
                    new NelderMeadSimplex(parameters.size()));
            double logLikelihood = optimizerResults.getValue() != null ? optimizerResults.getValue()
                    : Double.NaN;

            if (result.logLikelihood == null || logLikelihood > result.logLikelihood) {
                result = getResults(optimizerResults);

                if (result.logLikelihood == 0.0 || stopWhenSuccessful) {
                    break;
                }
            }
        } catch (TooManyEvaluationsException | TooManyIterationsException | ConvergenceException e) {
        }
    }

    return result;
}