Example usage for java.util TreeMap subMap

List of usage examples for java.util TreeMap subMap

Introduction

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

Prototype

public SortedMap<K, V> subMap(K fromKey, K toKey) 

Source Link

Usage

From source file:Main.java

public static void main(String[] args) {
    TreeMap<String, String> treeMap = new TreeMap<String, String>();

    treeMap.put("1", "One");
    treeMap.put("2", "Two");
    treeMap.put("3", "Three");
    treeMap.put("4", "Four");
    treeMap.put("5", "Five");

    SortedMap sortedMap = treeMap.subMap("2", "5");
    System.out.println("SortedMap Contains : " + sortedMap);
}

From source file:Main.java

public static void main(String[] args) {
    TreeMap<Integer, Product> db = new TreeMap<Integer, Product>();
    db.put(1000, new Product("D", 350));
    db.put(1011, new Product("p", 15.75));
    db.put(1102, new Product("M", 8.50));
    db.put(2023, new Product("A", 150));
    db.put(2034, new Product("T", 9.99));

    System.out.println(db.subMap(1000, 1999) + "\n");

    System.out.println(db.tailMap(1011) + "\n");

    System.out.println(db.headMap(2023));

    System.out.println("First key higher than 2034: " + db.higherKey(2034));
    System.out.println("First key lower than 2034: " + db.lowerKey(2034));
}

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");

    System.out.println("Getting a portion of the map");
    SortedMap<Integer, String> treemapincl = treemap.subMap(1, 5);
    System.out.println("Sub map values: " + treemapincl);
}

From source file:SortedMapDemo.java

public static void main(String[] args) {
    TreeMap sortedMap = new TreeMap();
    sortedMap.put("Adobe", "Mountain View, CA");
    sortedMap.put("IBM", "White Plains, NY");
    sortedMap.put("Learning Tree", "Los Angeles, CA");
    sortedMap.put("Microsoft", "Redmond, WA");
    sortedMap.put("Netscape", "Mountain View, CA");
    sortedMap.put("O'Reilly", "Sebastopol, CA");
    sortedMap.put("Sun", "Mountain View, CA");
    System.out.println(sortedMap);
    Object low = sortedMap.firstKey(), high = sortedMap.lastKey();
    System.out.println(low);//  ww w  . j a  va2  s .  c  om
    System.out.println(high);
    Iterator it = sortedMap.keySet().iterator();
    for (int i = 0; i <= 6; i++) {
        if (i == 3)
            low = it.next();
        if (i == 6)
            high = it.next();
        else
            it.next();
    }
    System.out.println(low);
    System.out.println(high);
    System.out.println(sortedMap.subMap(low, high));
    System.out.println(sortedMap.headMap(high));
    System.out.println(sortedMap.tailMap(low));
}

From source file:com.bombardier.plugin.scheduling.TestScheduler.java

/**
 * Used to get a sub map from a whole map based on the closest number of
 * bytes.//  w  ww  .ja  v a 2  s. co  m
 * 
 * @param sortedBySize
 *            the whole map
 * @param bytes
 *            the number of bytes
 * @return the sub map
 * @since 1.0
 */
private SortedMap<Long, Double> getSubMapForSize(TreeMap<Long, Double> sortedBySize, long bytes) {
    SortedMap<Long, Double> subMap = new TreeMap<Long, Double>();

    List<Long> keyList = new ArrayList<Long>(sortedBySize.keySet());

    Long closest = getClosestBySizeNum(keyList, bytes);

    List<List<Long>> list = splitToFiveLists(keyList);
    for (List<Long> subList : list) {
        if (subList.contains(closest)) {
            subMap = sortedBySize.subMap(subList.get(0), subList.get(subList.size() - 1));
            break;
        }
    }
    return subMap;
}

From source file:com.bombardier.plugin.scheduling.TestScheduler.java

/**
 * Used to get a sub map from a whole map based on the closest number of
 * lines//from   ww  w  .java2 s .com
 * 
 * @param sortedByLines
 *            the whole map
 * @param testLines
 *            the number of lines
 * @return the sub map
 * @since 1.0
 */
private SortedMap<Integer, Double> getSubMapForLines(TreeMap<Integer, Double> sortedByLines, int testLines) {
    SortedMap<Integer, Double> subMap = new TreeMap<Integer, Double>();

    List<Integer> keyList = new ArrayList<Integer>(sortedByLines.keySet());

    int closest = getClosestLineNum(keyList, testLines);

    List<List<Integer>> list = splitToFiveLists(keyList);
    for (List<Integer> subList : list) {
        if (subList.contains(closest)) {
            subMap = sortedByLines.subMap(subList.get(0), subList.get(subList.size() - 1));
            break;
        }
    }

    return subMap;
}

From source file:com.repeatability.pdf.PDFTextStripper.java

/**
 * This will process a TextPosition object and add the text to the list of characters on a page. It takes care of
 * overlapping text./*from  ww  w  .  ja  v a 2s  .c o m*/
 *
 * @param text The text to process.
 */
@Override
protected void processTextPosition(TextPosition text) {
    boolean showCharacter = true;
    if (suppressDuplicateOverlappingText) {
        showCharacter = false;
        String textCharacter = text.getUnicode();
        float textX = text.getX();
        float textY = text.getY();
        TreeMap<Float, TreeSet<Float>> sameTextCharacters = characterListMapping.get(textCharacter);
        if (sameTextCharacters == null) {
            sameTextCharacters = new TreeMap<Float, TreeSet<Float>>();
            characterListMapping.put(textCharacter, sameTextCharacters);
        }
        // RDD - Here we compute the value that represents the end of the rendered
        // text. This value is used to determine whether subsequent text rendered
        // on the same line overwrites the current text.
        //
        // We subtract any positive padding to handle cases where extreme amounts
        // of padding are applied, then backed off (not sure why this is done, but there
        // are cases where the padding is on the order of 10x the character width, and
        // the TJ just backs up to compensate after each character). Also, we subtract
        // an amount to allow for kerning (a percentage of the width of the last
        // character).
        boolean suppressCharacter = false;
        float tolerance = text.getWidth() / textCharacter.length() / 3.0f;

        SortedMap<Float, TreeSet<Float>> xMatches = sameTextCharacters.subMap(textX - tolerance,
                textX + tolerance);
        for (TreeSet<Float> xMatch : xMatches.values()) {
            SortedSet<Float> yMatches = xMatch.subSet(textY - tolerance, textY + tolerance);
            if (!yMatches.isEmpty()) {
                suppressCharacter = true;
                break;
            }
        }
        if (!suppressCharacter) {
            TreeSet<Float> ySet = sameTextCharacters.get(textX);
            if (ySet == null) {
                ySet = new TreeSet<Float>();
                sameTextCharacters.put(textX, ySet);
            }
            ySet.add(textY);
            showCharacter = true;
        }
    }
    if (showCharacter) {
        // if we are showing the character then we need to determine which article it belongs to
        int foundArticleDivisionIndex = -1;
        int notFoundButFirstLeftAndAboveArticleDivisionIndex = -1;
        int notFoundButFirstLeftArticleDivisionIndex = -1;
        int notFoundButFirstAboveArticleDivisionIndex = -1;
        float x = text.getX();
        float y = text.getY();
        if (shouldSeparateByBeads) {
            for (int i = 0; i < beadRectangles.size() && foundArticleDivisionIndex == -1; i++) {
                PDRectangle rect = beadRectangles.get(i);
                if (rect != null) {
                    if (rect.contains(x, y)) {
                        foundArticleDivisionIndex = i * 2 + 1;
                    } else if ((x < rect.getLowerLeftX() || y < rect.getUpperRightY())
                            && notFoundButFirstLeftAndAboveArticleDivisionIndex == -1) {
                        notFoundButFirstLeftAndAboveArticleDivisionIndex = i * 2;
                    } else if (x < rect.getLowerLeftX() && notFoundButFirstLeftArticleDivisionIndex == -1) {
                        notFoundButFirstLeftArticleDivisionIndex = i * 2;
                    } else if (y < rect.getUpperRightY() && notFoundButFirstAboveArticleDivisionIndex == -1) {
                        notFoundButFirstAboveArticleDivisionIndex = i * 2;
                    }
                } else {
                    foundArticleDivisionIndex = 0;
                }
            }
        } else {
            foundArticleDivisionIndex = 0;
        }
        int articleDivisionIndex;
        if (foundArticleDivisionIndex != -1) {
            articleDivisionIndex = foundArticleDivisionIndex;
        } else if (notFoundButFirstLeftAndAboveArticleDivisionIndex != -1) {
            articleDivisionIndex = notFoundButFirstLeftAndAboveArticleDivisionIndex;
        } else if (notFoundButFirstLeftArticleDivisionIndex != -1) {
            articleDivisionIndex = notFoundButFirstLeftArticleDivisionIndex;
        } else if (notFoundButFirstAboveArticleDivisionIndex != -1) {
            articleDivisionIndex = notFoundButFirstAboveArticleDivisionIndex;
        } else {
            articleDivisionIndex = charactersByArticle.size() - 1;
        }

        List<TextPosition> textList = charactersByArticle.get(articleDivisionIndex);

        // In the wild, some PDF encoded documents put diacritics (accents on
        // top of characters) into a separate Tj element. When displaying them
        // graphically, the two chunks get overlayed. With text output though,
        // we need to do the overlay. This code recombines the diacritic with
        // its associated character if the two are consecutive.
        if (textList.isEmpty()) {
            textList.add(text);
        } else {
            // test if we overlap the previous entry.
            // Note that we are making an assumption that we need to only look back
            // one TextPosition to find what we are overlapping.
            // This may not always be true. */
            TextPosition previousTextPosition = textList.get(textList.size() - 1);
            if (text.isDiacritic() && previousTextPosition.contains(text)) {
                previousTextPosition.mergeDiacritic(text);
            }
            // If the previous TextPosition was the diacritic, merge it into this
            // one and remove it from the list.
            else if (previousTextPosition.isDiacritic() && text.contains(previousTextPosition)) {
                text.mergeDiacritic(previousTextPosition);
                textList.remove(textList.size() - 1);
                textList.add(text);
            } else {
                textList.add(text);
            }
        }
    }
}

From source file:org.apache.james.mailbox.maildir.MaildirFolder.java

/**
 * Sorts the given map and returns a subset which is constricted by a lower and an upper limit.
 * @param source The source map//  w w  w  .  ja v  a  2 s .co m
 * @param from The lower limit
 * @param to The upper limit; <code>-1</code> disables the upper limit.
 * @return The sorted subset
 */
private SortedMap<Long, MaildirMessageName> truncateMap(Map<Long, MaildirMessageName> source, long from,
        long to) {
    TreeMap<Long, MaildirMessageName> sortedMap;
    if (source instanceof TreeMap<?, ?>)
        sortedMap = (TreeMap<Long, MaildirMessageName>) source;
    else
        sortedMap = new TreeMap<Long, MaildirMessageName>(source);
    if (to != -1)
        return sortedMap.subMap(from, to + 1);
    return sortedMap.tailMap(from);
}