Java SortedMap Usage submapWithPrefix(SortedMap map, String prefix)

Here you can find the source of submapWithPrefix(SortedMap map, String prefix)

Description

Returns a living view of the given map containing all strings that starts with the given prefix (including the prefix string itself).

License

Open Source License

Declaration

public static <V> SortedMap<String, V> submapWithPrefix(SortedMap<String, V> map, String prefix) 

Method Source Code

//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.util.SortedMap;

public class Main {
    /**/*from  w  ww  . j  a v  a2  s  . c o  m*/
     * Returns a living view of the given map containing all strings that starts with the given
     * prefix (including the prefix string itself). The view is backed by the original map.
     * <p>
     * Note: This method uses {@link SortedMap#subMap(Object, Object)} internally and works
     * <em>only</em> for sorted string maps with natural (lexiographic) ordering!
     */
    public static <V> SortedMap<String, V> submapWithPrefix(SortedMap<String, V> map, String prefix) {
        if (map.comparator() != null)
            throw new IllegalArgumentException("only natural (lexiographic) ordering supported");
        int length = prefix.length();
        if (length == 0)
            return map;
        // create a string lhs which is the _least higher string_ for the prefix, i.e.
        // there is no other string value between any prefixed string and lhs w.r.t ordering.
        StringBuilder lhs = new StringBuilder(prefix);
        char ch = lhs.charAt(length - 1);
        lhs.setCharAt(length - 1, (char) (ch + 1));
        return map.subMap(prefix, lhs.toString());
    }
}

Related

  1. lastElement(List c)
  2. pruneStartAndEndKeys(SortedMap setOfStartKeyByteArray, List listOfStartKeyByteArray)
  3. putWeightToBin(SortedMap hist, double bin, double weight)
  4. removeSetFromBulkLm(Set toRemoveSet, List> byteSizeByNameList, String exceptRegex)
  5. sortedMapOf(final Object obj, final Class keyCastTo, final Class valueCastTo)
  6. unwrapList(SortedMap sortedMap, K fromKey)
  7. unwrapList(SortedMap sortedMap, K fromKey)