Example usage for java.util TreeMap pollLastEntry

List of usage examples for java.util TreeMap pollLastEntry

Introduction

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

Prototype

public Map.Entry<K, V> pollLastEntry() 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {

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

    // populating tree map
    treemap.put(2, "two");
    treemap.put(1, "one");
    treemap.put(3, "three");
    treemap.put(6, "six");
    treemap.put(5, "from java2s.com");

    // polling last entry
    System.out.println("Value before poll: " + treemap);
    System.out.println("Value returned: " + treemap.pollLastEntry());
    System.out.println("Value after poll: " + treemap);
}

From source file:net.spfbl.core.User.java

public TreeMap<Long, Query> getQueryMap(Long begin, String filter) {
    TreeMap<Long, Query> queryLocalMap = getQueryHeadMap(begin);
    Connection connection = Core.poolConnectionMySQL();
    try {/*from  www. ja  v  a 2s .c  o m*/
        if (connection != null) {
            try {
                String ipParam = Subnet.isValidIP(filter) ? Subnet.normalizeIP(filter) : null;
                String emailParam = Domain.isValidEmail(filter) ? filter.toLowerCase() : null;
                String command = "SELECT * FROM spfbl.user_query\n" + "WHERE user = '" + getEmail() + "'\n"
                        + (begin == null ? "" : "AND time <= " + begin + "\n")
                        + ("rejeitada".equals(filter) ? "AND result " + "IN('BLOCK','REJECT')\n" : "")
                        + (ipParam == null ? "" : "AND ip = '" + ipParam + "'\n")
                        + (emailParam == null ? ""
                                : "AND '" + emailParam + "' " + "IN(sender, mailFrom, replyto, recipient)\n")
                        + "ORDER BY time DESC\n" + "LIMIT " + (QUERY_MAX_ROWS + 1);
                Statement statement = connection.createStatement();
                try {
                    ResultSet rs = statement.executeQuery(command);
                    while (rs.next()) {
                        try {
                            long time = rs.getLong("time");
                            Query query = queryLocalMap.get(time);
                            if (query == null) {
                                query = new Query(rs);
                                queryLocalMap.put(time, query);
                            }
                        } catch (Exception ex) {
                            Server.logError(ex);
                        }
                    }
                } finally {
                    statement.close();
                }
            } catch (SQLException ex) {
                Server.logError(ex);
            }
        }
    } finally {
        Core.offerConnectionMySQL(connection);
    }
    TreeMap<Long, Query> resultMap = new TreeMap<Long, Query>();
    while (resultMap.size() < (QUERY_MAX_ROWS + 1)) {
        Entry<Long, Query> entry = queryLocalMap.pollLastEntry();
        if (entry == null) {
            break;
        } else {
            long time = entry.getKey();
            Query query = entry.getValue();
            if (filter == null) {
                resultMap.put(time, query);
            } else if (filter.length() == 0) {
                resultMap.put(time, query);
            } else if (query.match(filter)) {
                resultMap.put(time, query);
            }
        }
    }
    return resultMap;
}

From source file:org.apdplat.superword.tools.TextAnalyzer.java

/**
 *
 * @param path ??/* w w w  .j  a va2s  . c  om*/
 * @param limit ???
 * @param isTopN ???
 */
public static TreeMap<Float, String> sentence(String path, int limit, boolean isTopN) {
    //?  
    Set<String> fileNames = getFileNames(path);
    //?
    Map<String, AtomicInteger> frequency = frequency(fileNames);
    //?
    TreeMap<Float, String> sentences = new TreeMap<>();
    //??
    int count = 0;
    for (String fileName : fileNames) {
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(new BufferedInputStream(new FileInputStream(fileName))))) {
            String line = null;
            while ((line = reader.readLine()) != null) {
                if (StringUtils.isBlank(line)) {
                    continue;
                }
                //
                float score = 0;
                List<String> words = seg(line);
                for (String word : words) {
                    AtomicInteger fre = frequency.get(word);
                    if (fre == null || fre.get() == 0) {
                        LOGGER.error("????" + line);
                        score = 0;
                        break;
                    }
                    score += 1 / (float) fre.get();
                }
                words.clear();
                if (score > 0) {
                    //???
                    if (sentences.get(score) != null) {
                        continue;
                    }
                    sentences.put(score, line + " <u><i>"
                            + Paths.get(fileName).toFile().getName().replace(".txt", "") + "</i></u>");
                    count++;
                    if (count >= limit) {
                        if (isTopN) {
                            //
                            sentences.pollFirstEntry();
                        } else {
                            //
                            sentences.pollLastEntry();
                        }
                    }
                }
            }
        } catch (IOException ex) {
            LOGGER.error("??", ex);
        }
    }
    return sentences;
}

From source file:org.wso2.carbon.governance.registry.extensions.executors.ServiceVersionExecutor.java

private String reformatPath(String path, String currentExpression, String targetExpression,
        String newResourceVersion) throws RegistryException {
    TreeMap<Integer, String> indexMap = new TreeMap<Integer, String>();

    String returnPath = targetExpression;
    String prefix;//from w  ww  . j  av a 2  s  . co  m

    if (currentExpression.equals(targetExpression)) {
        return path;
    }
    indexMap.put(currentExpression.indexOf(RESOURCE_NAME), RESOURCE_NAME);
    indexMap.put(currentExpression.indexOf(RESOURCE_PATH), RESOURCE_PATH);
    indexMap.put(currentExpression.indexOf(RESOURCE_VERSION), RESOURCE_VERSION);

    String tempExpression = currentExpression;

    while (indexMap.lastKey() < tempExpression.lastIndexOf(RegistryConstants.PATH_SEPARATOR)) {
        tempExpression = tempExpression.substring(0,
                tempExpression.lastIndexOf(RegistryConstants.PATH_SEPARATOR));
        path = path.substring(0, path.lastIndexOf(RegistryConstants.PATH_SEPARATOR));
    }

    prefix = currentExpression.substring(0, currentExpression.indexOf(indexMap.get(indexMap.higherKey(-1))));

    if (!path.startsWith(prefix)) {
        return path;
    }
    path = path.replace(prefix, "");

    while (true) {
        if (indexMap.firstKey() < 0) {
            indexMap.pollFirstEntry();
        } else {
            break;
        }
    }

    while (true) {
        if (indexMap.size() == 0) {
            break;
        }
        Map.Entry lastEntry = indexMap.pollLastEntry();
        if (lastEntry.getValue().equals(RESOURCE_PATH)) {
            String pathValue = path;

            for (int i = 0; i < indexMap.size(); i++) {
                //                    pathValue = formatPath(pathValue.substring(path.indexOf(RegistryConstants.PATH_SEPARATOR)));
                pathValue = formatPath(
                        pathValue.substring(pathValue.indexOf(RegistryConstants.PATH_SEPARATOR)));
            }

            if (!pathValue.equals("")) {
                returnPath = returnPath.replace(RESOURCE_PATH, formatPath(pathValue));
                path = path.replace(pathValue, "");
            } else {
                returnPath = returnPath.replace("/" + lastEntry.getValue(), "");
            }

            continue;
        }
        if (lastEntry.getValue().equals(RESOURCE_VERSION)) {
            returnPath = returnPath.replace(RESOURCE_VERSION, newResourceVersion);
            if (path.contains("/")) {
                path = path.substring(0, path.lastIndexOf(RegistryConstants.PATH_SEPARATOR));
            } else {
                path = "";
            }
            continue;
        }

        String tempPath;
        if (path.contains("/")) {
            tempPath = path.substring(path.lastIndexOf(RegistryConstants.PATH_SEPARATOR) + 1);
        } else {
            tempPath = path;
        }
        if (!tempPath.equals("")) {
            returnPath = returnPath.replace((String) lastEntry.getValue(), formatPath(tempPath));
            if (path.contains("/")) {
                path = path.substring(0, path.lastIndexOf(RegistryConstants.PATH_SEPARATOR));
            } else {
                path = "";
            }
        } else {
            returnPath = returnPath.replace("/" + lastEntry.getValue(), "");
            if (path.contains("/")) {
                path = path.substring(0, path.lastIndexOf(RegistryConstants.PATH_SEPARATOR));
            }
        }

    }

    //        Adding the version validation here.
    if (!newResourceVersion.matches("^\\d+[.]\\d+[.]\\d+(-[a-zA-Z0-9]+)?$")) {
        String message = "Invalid version found for " + RegistryUtils.getResourceName(path);
        log.error(message);
        throw new RegistryException(message);
    }
    if (returnPath.contains(RESOURCE_VERSION)) {
        return returnPath.replace(RESOURCE_VERSION, newResourceVersion);
    }
    return returnPath;
}