Example usage for java.util TreeMap remove

List of usage examples for java.util TreeMap remove

Introduction

In this page you can find the example usage for java.util TreeMap remove.

Prototype

public V remove(Object key) 

Source Link

Document

Removes the mapping for this key from this TreeMap if present.

Usage

From source file:org.apache.nutch.crawl.CrawlDbReader.java

public void processStatJob(String crawlDb, Configuration config, boolean sort)
        throws IOException, InterruptedException, ClassNotFoundException {

    double quantiles[] = { .01, .05, .1, .2, .25, .3, .4, .5, .6, .7, .75, .8, .9, .95, .99 };
    if (config.get("db.stats.score.quantiles") != null) {
        List<Double> qs = new ArrayList<>();
        for (String s : config.getStrings("db.stats.score.quantiles")) {
            try {
                double d = Double.parseDouble(s);
                if (d >= 0.0 && d <= 1.0) {
                    qs.add(d);//from  www.j  a  va 2s .  c  om
                } else {
                    LOG.warn("Skipping quantile {} not in range in db.stats.score.quantiles: {}", s);
                }
            } catch (NumberFormatException e) {
                LOG.warn("Skipping bad floating point number {} in db.stats.score.quantiles: {}", s,
                        e.getMessage());
            }
            quantiles = new double[qs.size()];
            int i = 0;
            for (Double q : qs) {
                quantiles[i++] = q;
            }
            Arrays.sort(quantiles);
        }
    }

    if (LOG.isInfoEnabled()) {
        LOG.info("CrawlDb statistics start: " + crawlDb);
    }
    TreeMap<String, Writable> stats = processStatJobHelper(crawlDb, config, sort);

    if (LOG.isInfoEnabled()) {
        LOG.info("Statistics for CrawlDb: " + crawlDb);
        LongWritable totalCnt = new LongWritable(0);
        if (stats.containsKey("T")) {
            totalCnt = ((LongWritable) stats.get("T"));
            stats.remove("T");
        }
        LOG.info("TOTAL urls:\t" + totalCnt.get());
        for (Map.Entry<String, Writable> entry : stats.entrySet()) {
            String k = entry.getKey();
            long value = 0;
            double fvalue = 0.0;
            byte[] bytesValue = null;
            Writable val = entry.getValue();
            if (val instanceof LongWritable) {
                value = ((LongWritable) val).get();
            } else if (val instanceof FloatWritable) {
                fvalue = ((FloatWritable) val).get();
            } else if (val instanceof BytesWritable) {
                bytesValue = ((BytesWritable) val).getBytes();
            }
            if (k.equals("scn")) {
                LOG.info("min score:\t" + fvalue);
            } else if (k.equals("scx")) {
                LOG.info("max score:\t" + fvalue);
            } else if (k.equals("sct")) {
                LOG.info("avg score:\t" + (fvalue / totalCnt.get()));
            } else if (k.equals("scNaN")) {
                LOG.info("score == NaN:\t" + value);
            } else if (k.equals("ftn")) {
                LOG.info("earliest fetch time:\t" + new Date(1000 * 60 * value));
            } else if (k.equals("ftx")) {
                LOG.info("latest fetch time:\t" + new Date(1000 * 60 * value));
            } else if (k.equals("ftt")) {
                LOG.info("avg of fetch times:\t" + new Date(1000 * 60 * (value / totalCnt.get())));
            } else if (k.equals("fin")) {
                LOG.info("shortest fetch interval:\t{}", TimingUtil.secondsToDaysHMS(value));
            } else if (k.equals("fix")) {
                LOG.info("longest fetch interval:\t{}", TimingUtil.secondsToDaysHMS(value));
            } else if (k.equals("fit")) {
                LOG.info("avg fetch interval:\t{}", TimingUtil.secondsToDaysHMS(value / totalCnt.get()));
            } else if (k.startsWith("status")) {
                String[] st = k.split(" ");
                int code = Integer.parseInt(st[1]);
                if (st.length > 2)
                    LOG.info("   " + st[2] + " :\t" + val);
                else
                    LOG.info(st[0] + " " + code + " (" + CrawlDatum.getStatusName((byte) code) + "):\t" + val);
            } else if (k.equals("scd")) {
                MergingDigest tdigest = MergingDigest.fromBytes(ByteBuffer.wrap(bytesValue));
                for (double q : quantiles) {
                    LOG.info("score quantile {}:\t{}", q, tdigest.quantile(q));
                }
            } else {
                LOG.info(k + ":\t" + val);
            }
        }
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("CrawlDb statistics: done");
    }

}

From source file:org.eclipse.dataset.Stats.java

public static double[] outlierValuesMap(final Dataset a, int nl, int nh) {
    final TreeMap<Double, Integer> lMap = new TreeMap<Double, Integer>();
    final TreeMap<Double, Integer> hMap = new TreeMap<Double, Integer>();

    int ml = 0;//from  w  ww. j  ava  2s .  c o m
    int mh = 0;
    IndexIterator it = a.getIterator();
    while (it.hasNext()) {
        Double x = a.getElementDoubleAbs(it.index);
        Integer i;
        if (ml == nl) {
            Double k = lMap.lastKey();
            if (x < k) {
                i = lMap.get(k) - 1;
                if (i == 0) {
                    lMap.remove(k);
                } else {
                    lMap.put(k, i);
                }
                i = lMap.get(x);
                if (i == null) {
                    lMap.put(x, 1);
                } else {
                    lMap.put(x, i + 1);
                }
            }
        } else {
            i = lMap.get(x);
            if (i == null) {
                lMap.put(x, 1);
            } else {
                lMap.put(x, i + 1);
            }
            ml++;
        }

        if (mh == nh) {
            Double k = hMap.firstKey();
            if (x > k) {
                i = hMap.get(k) - 1;
                if (i == 0) {
                    hMap.remove(k);
                } else {
                    hMap.put(k, i);
                }
                i = hMap.get(x);
                if (i == null) {
                    hMap.put(x, 1);
                } else {
                    hMap.put(x, i + 1);
                }
            }
        } else {
            i = hMap.get(x);
            if (i == null) {
                hMap.put(x, 1);
            } else {
                hMap.put(x, i + 1);
            }
            mh++;
        }
    }

    // Attempt to make values distinct
    double lx = lMap.lastKey();
    double hx = hMap.firstKey();
    if (lx >= hx) {
        Double h = hMap.higherKey(lx);
        if (h != null) {
            hx = h;
            mh--;
        } else {
            Double l = lMap.lowerKey(hx);
            if (l != null) {
                lx = l;
                ml--;
            }
        }

    }
    return new double[] { lMap.lastKey(), hMap.firstKey(), ml, mh };
}

From source file:org.eclipse.january.dataset.Stats.java

static double[] outlierValuesMap(final Dataset a, int nl, int nh) {
    final TreeMap<Double, Integer> lMap = new TreeMap<Double, Integer>();
    final TreeMap<Double, Integer> hMap = new TreeMap<Double, Integer>();

    int ml = 0;//from www  .ja  v a 2s . co m
    int mh = 0;
    IndexIterator it = a.getIterator();
    while (it.hasNext()) {
        Double x = a.getElementDoubleAbs(it.index);
        Integer i;
        if (ml == nl) {
            Double k = lMap.lastKey();
            if (x < k) {
                i = lMap.get(k) - 1;
                if (i == 0) {
                    lMap.remove(k);
                } else {
                    lMap.put(k, i);
                }
                i = lMap.get(x);
                if (i == null) {
                    lMap.put(x, 1);
                } else {
                    lMap.put(x, i + 1);
                }
            }
        } else {
            i = lMap.get(x);
            if (i == null) {
                lMap.put(x, 1);
            } else {
                lMap.put(x, i + 1);
            }
            ml++;
        }

        if (mh == nh) {
            Double k = hMap.firstKey();
            if (x > k) {
                i = hMap.get(k) - 1;
                if (i == 0) {
                    hMap.remove(k);
                } else {
                    hMap.put(k, i);
                }
                i = hMap.get(x);
                if (i == null) {
                    hMap.put(x, 1);
                } else {
                    hMap.put(x, i + 1);
                }
            }
        } else {
            i = hMap.get(x);
            if (i == null) {
                hMap.put(x, 1);
            } else {
                hMap.put(x, i + 1);
            }
            mh++;
        }
    }

    // Attempt to make values distinct
    double lx = lMap.lastKey();
    double hx = hMap.firstKey();
    if (lx >= hx) {
        Double h = hMap.higherKey(lx);
        if (h != null) {
            hx = h;
            mh--;
        } else {
            Double l = lMap.lowerKey(hx);
            if (l != null) {
                lx = l;
                ml--;
            }
        }

    }
    return new double[] { lMap.lastKey(), hMap.firstKey(), ml, mh };
}

From source file:edu.utexas.cs.tactex.tariffoptimization.TariffOptimizerBinaryOneShot.java

/**
 * evaluate suggestedSpecs(index), and record result to utilToIndex
 * and result//from   w  w w  .ja  v a  2s .c o  m
 * 
 * @param index
 * @param utilToIndex
 * @param result
 * @param suggestedSpecs
 * @param consideredTariffActions
 * @param tariffSubscriptions
 * @param competingTariffs
 * @param costCurvesPredictor
 * @param currentTimeslot
 * @param me
 * @param customer2ShiftedEnergy
 * @param customer2RelevantTariffCharges
 * @param customer2NonShiftedEnergy 
 */
private void evaluateAndRecord(int index, TreeMap<Double, Integer> utilToIndex,
        TreeMap<Double, TariffSpecification> result, List<TariffSpecification> suggestedSpecs,
        ArrayList<TariffSpecification> consideredTariffActions,
        HashMap<TariffSpecification, HashMap<CustomerInfo, Integer>> tariffSubscriptions,
        List<TariffSpecification> competingTariffs, CostCurvesPredictor costCurvesPredictor,
        int currentTimeslot, Broker me,
        HashMap<CustomerInfo, HashMap<TariffSpecification, ShiftedEnergyData>> customer2ShiftedEnergy,
        HashMap<CustomerInfo, ArrayRealVector> customer2NonShiftedEnergy,
        HashMap<CustomerInfo, HashMap<TariffSpecification, Double>> customer2RelevantTariffCharges) {
    TreeMap<Double, TariffSpecification> sortedTariffs;
    consideredTariffActions.clear();
    consideredTariffActions.add(suggestedSpecs.get(index));
    //log.info("computing utilities");
    sortedTariffs = utilityEstimator.estimateUtilities(consideredTariffActions, tariffSubscriptions,
            competingTariffs, customer2RelevantTariffCharges, customer2ShiftedEnergy, customer2NonShiftedEnergy,
            marketPredictionManager, costCurvesPredictor, currentTimeslot, me);
    utilToIndex.put(sortedTariffs.lastEntry().getKey(), index);
    // maintain top 3
    if (utilToIndex.size() > 3) {
        utilToIndex.remove(utilToIndex.firstKey());
    }
    result.putAll(sortedTariffs);
}

From source file:com.example.camera2raw.Camera2RawFragment.java

/**
 * If the given request has been completed, remove it from the queue of active requests and
 * send an {@link ImageSaver} with the results from this request to a background thread to
 * save a file./*w  ww .j  a v a 2 s.co m*/
 * <p/>
 * Call this only with {@link #mCameraStateLock} held.
 *
 * @param requestId the ID of the {@link CaptureRequest} to handle.
 * @param builder   the {@link ImageSaverBuilder} for this request.
 * @param queue     the queue to remove this request from, if completed.
 */
private void handleCompletionLocked(int requestId, ImageSaverBuilder builder,
        TreeMap<Integer, ImageSaverBuilder> queue) {
    if (builder == null)
        return;
    ImageSaver saver = builder.buildIfComplete();
    if (saver != null) {
        queue.remove(requestId);
        AsyncTask.THREAD_POOL_EXECUTOR.execute(saver);
    }
}

From source file:com.sfs.whichdoctor.importer.Importer.java

/**
 * Sets the column map.//  w  ww.  ja  v a  2 s .co  m
 *
 * @param type the type
 * @param data the data
 * @param includeRowsVal the include rows
 */
public final void setColumnMap(final String type, final TreeMap<Integer, TreeMap<Integer, String>> data,
        final TreeMap<Integer, String> includeRowsVal) {

    TreeMap<Integer, String> columnMapVal = new TreeMap<Integer, String>();

    List<String> fields = new ArrayList<String>();

    if (StringUtils.equalsIgnoreCase(type, "exam")) {
        ExamImporter examImporter = new ExamImporter();
        fields = examImporter.getFields();
    }

    // Inspect the first row of data supplied
    Integer rowIndex = data.keySet().iterator().next();

    TreeMap<Integer, String> firstRow = data.get(rowIndex);

    int fieldMatches = 0;

    for (Integer columnNumber : firstRow.keySet()) {
        String dataField = firstRow.get(columnNumber);
        String fieldName = "";

        // Iterate through each field to see if there is a match
        // If there is more than two matches then the first row
        // is indicating column field names
        for (int i = 0; i < fields.size(); i++) {
            String field = (String) fields.get(i);
            if (StringUtils.equalsIgnoreCase(dataField, field)) {
                // Matching field
                fieldName = dataField;
                fieldMatches++;
            }
        }
        columnMapVal.put(columnNumber, fieldName);
    }
    if (fieldMatches > 2) {
        // There were more than two field matches
        // Deselect the first column from the list of imports
        if (includeRowsVal.containsKey(rowIndex)) {
            includeRowsVal.remove(rowIndex);
        }
    }

    setIncludeRows(includeRowsVal);
    setColumnMap(columnMapVal);

}

From source file:com.jefftharris.passwdsafe.NotificationMgr.java

/** Update the notification expirations for a password file */
private void doUpdatePasswdFileData(long uriId, PasswdFileData fileData, SQLiteDatabase db)
        throws SQLException {
    PasswdSafeUtil.dbginfo(TAG, "Update %s, id: %d", fileData.getUri(), uriId);

    TreeMap<ExpiryEntry, Long> entries = new TreeMap<>();
    Cursor cursor = db.query(DB_TABLE_EXPIRYS,
            new String[] { DB_COL_EXPIRYS_ID, DB_COL_EXPIRYS_UUID, DB_COL_EXPIRYS_TITLE, DB_COL_EXPIRYS_GROUP,
                    DB_COL_EXPIRYS_EXPIRE },
            DB_MATCH_EXPIRYS_URI, new String[] { Long.toString(uriId) }, null, null, null);
    try {/*from  w  w  w . j  a va 2s.com*/
        while (cursor.moveToNext()) {
            ExpiryEntry entry = new ExpiryEntry(cursor.getString(1), cursor.getString(2), cursor.getString(3),
                    cursor.getLong(4));
            entries.put(entry, cursor.getLong(0));
        }
    } finally {
        cursor.close();
    }

    boolean dbchanged = false;
    ContentValues values = null;
    for (PasswdRecord rec : fileData.getPasswdRecords()) {
        PasswdExpiration expiry = rec.getPasswdExpiry();
        if (expiry == null) {
            continue;
        }

        PwsRecord pwsrec = rec.getRecord();
        ExpiryEntry entry = new ExpiryEntry(rec.getUUID(), fileData.getTitle(pwsrec), fileData.getGroup(pwsrec),
                expiry.itsExpiration.getTime());
        if (entries.remove(entry) == null) {
            if (values == null) {
                values = new ContentValues();
                values.put(DB_COL_EXPIRYS_URI, uriId);
            }
            values.put(DB_COL_EXPIRYS_UUID, entry.itsUuid);
            values.put(DB_COL_EXPIRYS_TITLE, entry.itsTitle);
            values.put(DB_COL_EXPIRYS_GROUP, entry.itsGroup);
            values.put(DB_COL_EXPIRYS_EXPIRE, entry.itsExpiry);
            db.insertOrThrow(DB_TABLE_EXPIRYS, null, values);
            dbchanged = true;
        }
    }

    for (Long rmId : entries.values()) {
        db.delete(DB_TABLE_EXPIRYS, DB_MATCH_EXPIRYS_ID, new String[] { rmId.toString() });
        dbchanged = true;
    }

    if (dbchanged) {
        loadEntries(db);
    }
}

From source file:com.sfs.whichdoctor.dao.VoteDAOImpl.java

/**
 * Loads an array of groups based on the submitted voteNumber and year.
 *
 * @param voteNumber the vote number/*  w  w  w.j a  v  a  2  s  . com*/
 * @param year the year
 *
 * @return the collection< group bean>
 *
 * @throws WhichDoctorDaoException the which doctor dao exception
 */
@SuppressWarnings("unchecked")
public final Collection<GroupBean> findElections(final int voteNumber, final int year)
        throws WhichDoctorDaoException {

    if (year == 0) {
        throw new WhichDoctorDaoException("Sorry a valid year is required");
    }

    Collection<GroupBean> elections = new ArrayList<GroupBean>();

    dataLogger.info("Loading elections for: " + voteNumber + "/" + year);

    /* Identify the referenceGUID from the vote number and year */
    int referenceGUID = PersonBean.getVoterGUID(voteNumber, year);

    TreeMap<Integer, Integer> electionList = new TreeMap<Integer, Integer>();

    try {
        Collection<Integer> guids = this.getJdbcTemplateReader().query(
                this.getSQL().getValue("vote/findPossibleElections"), new Object[] { year, referenceGUID },
                new RowMapper() {
                    public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                        return rs.getInt("GUID");
                    }
                });

        for (Integer guid : guids) {
            electionList.put(guid, guid);
        }
    } catch (IncorrectResultSizeDataAccessException ie) {
        dataLogger.debug("No results found for this search: " + ie.getMessage());
    }

    try {
        Collection<Integer> guids = this.getJdbcTemplateReader().query(
                this.getSQL().getValue("vote/findVotedElections"), new Object[] { year, referenceGUID },
                new RowMapper() {
                    public Object mapRow(final ResultSet rs, final int rowNum) throws SQLException {
                        return rs.getInt("GUID");
                    }
                });

        for (Integer guid : guids) {
            if (electionList.containsKey(guid)) {
                // This election has been voted for already, remove it from the list
                electionList.remove(guid);
            }
        }
    } catch (IncorrectResultSizeDataAccessException ie) {
        dataLogger.debug("No results found for this search: " + ie.getMessage());
    }

    if (electionList.size() > 0) {
        // An unvoted election exists in the map, perform a group search to load it
        Collection<Object> guidCollection = new ArrayList<Object>();
        for (Integer groupGUID : electionList.keySet()) {
            guidCollection.add(groupGUID);
        }
        try {
            SearchBean search = this.searchDAO.initiate("group", null);
            search.setLimit(0);
            search.setOrderColumn("groups.Weighting");
            search.setOrderColumn2("groups.Name");
            search.setSearchArray(guidCollection, "Unvoted list of elections");

            BuilderBean loadDetails = new BuilderBean();
            loadDetails.setParameter("CANDIDATES", true);

            SearchResultsBean results = this.searchDAO.search(search, loadDetails);

            if (results != null) {
                // Add each group to the election array
                for (Object result : results.getSearchResults()) {
                    GroupBean election = (GroupBean) result;
                    elections.add(election);
                }
            }
        } catch (Exception e) {
            dataLogger.error("Error performing election search: " + e.getMessage());
            throw new WhichDoctorDaoException("Error performing election search: " + e.getMessage());
        }
    }
    return elections;
}

From source file:org.opendatakit.common.persistence.engine.gae.TaskLockImpl.java

/**
 * Remove the given lockId from the (formId, taskType) entry.
 * //  w  w w .j a  v a 2s .  c  o m
 * NOTE: We make 10 attempts. If all of these fail, the lockId will be left
 * active. This can cause lock-outs for the duration of the locking period.
 * 
 * @param lockId
 * @param formId
 * @param taskType
 */
private synchronized void deleteLockIdMemCache(String lockId, String formId, ITaskLockType taskType) {
    if (syncCache != null) {
        int i = 0;
        try {
            String formTask = ((formId == null) ? "" : formId) + "@" + taskType.getName();
            for (i = 0; i < 10; i++) {

                IdentifiableValue v = syncCache.contains(formTask) ? syncCache.getIdentifiable(formTask) : null;
                if (v == null || v.getValue() == null) {
                    break;
                } else {
                    @SuppressWarnings("unchecked")
                    TreeMap<Long, String> tmOrig = (TreeMap<Long, String>) v.getValue();
                    TreeMap<Long, String> tm = new TreeMap<Long, String>(tmOrig);

                    // remove any old entries for lockId and any that are very old
                    Long currentTimestamp = System.currentTimeMillis();
                    Long oldTimestamp;
                    do {
                        oldTimestamp = null;
                        for (Map.Entry<Long, String> entry : tm.entrySet()) {
                            if (entry.getKey() + 300000L < currentTimestamp) {
                                // more than 5 minutes old -- remove it
                                oldTimestamp = entry.getKey();
                                break;
                            }
                            if (entry.getValue().equals(lockId)) {
                                oldTimestamp = entry.getKey();
                                break;
                            }
                        }
                        if (oldTimestamp != null) {
                            tm.remove(oldTimestamp);
                        }
                    } while (oldTimestamp != null);

                    if (syncCache.putIfUntouched(formTask, v, tm)) {
                        break;
                    }
                }
            }
        } catch (Throwable t) {
            t.printStackTrace();
            // ignore
        }
        // don't care if we had contention and didn't do anything.
        // This will eventually self-correct.
        if (i == 10) {
            log.warn("deleteLockIdMemCache -- stall has been introduced  lock : " + lockId + " " + formId + " "
                    + taskType.getName());
        }
    }
}

From source file:com.sfs.beans.PrivilegesBean.java

/**
 * Gets a list of email addresses for the supplied user.
 *
 * @param user the user bean/*  w w  w  .  j  ava  2  s.  co m*/
 *
 * @return the list of email addresses
 */
public final Collection<String> getEmailAddresses(final UserBean user) {
    Collection<String> addresses = new ArrayList<String>();

    TreeMap<String, String> unique = new TreeMap<String, String>();

    if (user.getMemberOf() != null) {
        for (String role : user.getMemberOf()) {
            if (role != null) {
                RoleBean roleBean = (RoleBean) this.roles.get(role);
                StringBuffer address = new StringBuffer();

                if (StringUtils.isNotBlank(roleBean.getEmailAddress())) {
                    address.append(roleBean.getEmailAddress().trim());
                    if (StringUtils.isNotBlank(roleBean.getEmailName())) {
                        address.insert(0, roleBean.getEmailName().trim() + " <");
                        address.append(">");
                    }
                }

                if (StringUtils.isNotBlank(address.toString())) {
                    unique.put(address.toString(), address.toString());
                }
            }
        }
    }

    StringBuffer userAddress = new StringBuffer();

    if (StringUtils.isNotBlank(user.getEmail())) {

        userAddress.append(user.getEmail().trim());
        if (StringUtils.isNotBlank(user.getLastName())) {
            userAddress.insert(0, user.getLastName().trim() + " <");
            userAddress.append(">");
        }
        if (StringUtils.isNotBlank(user.getPreferredName())) {
            userAddress.insert(0, user.getPreferredName().trim() + " ");
        }
        // Remove the address if it is already in the unique tree map
        if (unique.containsKey(userAddress.toString())) {
            unique.remove(userAddress.toString());
        }
    }

    // Add the user name to the top of the list
    if (StringUtils.isNotBlank(userAddress.toString())) {
        addresses.add(userAddress.toString());
    }
    for (String address : unique.keySet()) {
        addresses.add(address);
    }

    return addresses;
}