下面列出了java.util.SortedMap#subMap ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Construct a map based upon all ColumnQualifiers and Values in the Column Family <code>columnFamily</code>,
* optionally removing any used Key-Value pairs from <code>row</code>, the source map.
*
* @param row
* @param columnFamily
* @param clearUsed when true, clear used entries from <code>row</code>
* @return
*/
public static Map<String, String> readMap(SortedMap<Key, Value> row, String columnFamily, boolean clearUsed) {
Map<String, String> retVal = new LinkedHashMap<>();
String rowid = row.firstKey().getRow().toString();
SortedMap<Key, Value> familyMap = row.subMap(
new Key(rowid, columnFamily),
new Key(rowid, columnFamily + "\0"));
for (Entry<Key, Value> entry : familyMap.entrySet()) {
retVal.put(entry.getKey().getColumnQualifier().toString(), entry.getValue().toString());
}
if (clearUsed) {
familyMap.clear();
}
return retVal;
}
public void test_lastKey_after_subMap() {
TreeMap<String, String> tm = new TreeMap<String, String>();
tm.put("001", "VAL001");
tm.put("003", "VAL003");
tm.put("002", "VAL002");
SortedMap<String, String> sm = tm;
String firstKey = (String) sm.firstKey();
String lastKey = "";
for (int i = 1; i <= tm.size(); i++) {
try {
lastKey = (String) sm.lastKey();
} catch (NoSuchElementException excep) {
fail("NoSuchElementException thrown when there are elements in the map");
}
sm = sm.subMap(firstKey, lastKey);
}
}
private void filterOffsets(long fromOffset, long toOffset) {
Iterator iterator = mTopicPartitionToOffsetToFiles.entrySet().iterator();
while (iterator.hasNext()) {
long firstOffset = -2;
long lastOffset = Long.MAX_VALUE;
Map.Entry entry = (Map.Entry) iterator.next();
SortedMap<Long, HashSet<LogFilePath>> offsetToFiles =
(SortedMap<Long, HashSet<LogFilePath>>) entry.getValue();
for (long offset : offsetToFiles.keySet()) {
if (offset <= fromOffset || firstOffset == -2) {
firstOffset = offset;
}
if (offset >= toOffset && toOffset == Long.MAX_VALUE) {
lastOffset = offset;
}
}
if (firstOffset != -2) {
TopicPartition topicPartition = (TopicPartition) entry.getKey();
offsetToFiles = offsetToFiles.subMap(firstOffset, lastOffset);
mTopicPartitionToOffsetToFiles.put(topicPartition, offsetToFiles);
}
}
}
/**
* @param qualifier
* @param initialsToInfo this is where we are going to get the info from (currently: inner or top level list)
* @param toks (out) the tokens will be added to this list
* @return
*/
protected void getWithFilter(String qualifier, SortedMap<String, Set<IInfo>> initialsToInfo,
Collection<IInfo> toks, Filter filter, boolean useLowerCaseQual) {
String initials = getInitials(qualifier);
String qualToCompare = qualifier;
if (useLowerCaseQual) {
qualToCompare = qualifier.toLowerCase();
}
//get until the end of the alphabet
SortedMap<String, Set<IInfo>> subMap = initialsToInfo.subMap(initials, initials + "\uffff\uffff\uffff\uffff");
for (Set<IInfo> listForInitials : subMap.values()) {
for (IInfo info : listForInitials) {
if (filter.doCompare(qualToCompare, info)) {
toks.add(info);
}
}
}
}
/**
* Returns sub map from SortedMap, where keys match the prefix
*/
private static <V> SortedMap<String, V> filterPrefix(SortedMap<String,V> baseMap, String prefix) {
if(prefix.length() > 0) {
char nextLetter = (char)(prefix.charAt(prefix.length() -1) + 1);
String end = prefix.substring(0, prefix.length()-1) + nextLetter;
return baseMap.subMap(prefix, end);
}
return baseMap;
}
/**
* Returns true if value is found in SortedMap, including also simple wildcard *
*/
private static <V> boolean simpleWildcardCheck(SortedMap<String,V> baseMap, String value) {
if (value == null) {
return false;
}
if (baseMap.get(value) != null) {
//System.out.println("======= " + value);
return true;
} else if (value.length() > 0){
String v = value;
v = v.substring(0, v.length() - 1);
while (v.length() > 0) {
char nextLetter = (char)(v.charAt(v.length() -1) + 1);
String end = v.substring(0, v.length()-1) + nextLetter;
SortedMap<String, V> b = baseMap.subMap(v, end);
for (String key : b.keySet()) {
if ((key.length() == v.length() + 1) && key.endsWith("*")) {
//System.out.println("======= " + key);
return true;
}
}
v = v.substring(0, v.length() - 1);
}
}
return false;
}
/**
* Returns sub map from SortedMap, where keys match the prefix
*/
private static <V> SortedMap<String, V> filterPrefix(SortedMap<String,V> baseMap, String prefix) {
if(prefix.length() > 0) {
char nextLetter = (char)(prefix.charAt(prefix.length() -1) + 1);
String end = prefix.substring(0, prefix.length()-1) + nextLetter;
return baseMap.subMap(prefix, end);
}
return baseMap;
}
/**
* Returns Key if value is found in SortedMap, including also simple wildcard *
*/
public static <V> String simpleWildcardKeyFind(SortedMap<String,V> baseMap, String value) {
if (value == null) {
return null;
}
if (baseMap.get(value) != null) {
//System.out.println("======= " + value);
return value;
} else if (value.length() > 0){
String v = value;
v = v.substring(0, v.length() - 1);
while (v.length() > 0) {
char nextLetter = (char)(v.charAt(v.length() -1) + 1);
String end = v.substring(0, v.length()-1) + nextLetter;
SortedMap<String, V> b = baseMap.subMap(v, end);
for (String key : b.keySet()) {
if ((key.length() == v.length() + 1) && key.endsWith("*")) {
//System.out.println("======= " + key);
return key;
}
}
v = v.substring(0, v.length() - 1);
}
}
return null;
}
static SortedMap<String, FreqValue> getCut(SortedMap<String, FreqValue> coll, String[] arr, String pattern) {
int startpos = Arrays.binarySearch(arr, pattern);
if (startpos == -1 || (startpos < 0 && !pattern.startsWith(arr[-startpos - 2]))) return null;
if (startpos < 0) {
startpos = -startpos - 2;
}
int endpos = Arrays.binarySearch(arr, startpos, arr.length, pattern + Character.MAX_VALUE);
assert(endpos < 0);
endpos = -endpos - 1;
return endpos == arr.length ?
coll.tailMap(arr[startpos]) :
coll.subMap(arr[startpos], arr[endpos]);
}
public static SortedMap<String, List<CompletionProposalTemplate>> filterPrefix(SortedMap<String, List<CompletionProposalTemplate>> baseMap, String prefix) {
if (prefix.length() > 0) {
final char nextLetter = (char) (prefix.charAt(prefix.length() - 1) + 1);
final String end = prefix.substring(0, prefix.length() - 1) + nextLetter;
return baseMap.subMap(prefix, end);
}
return baseMap;
}
/**
* java.util.TreeMap#subMap(java.lang.Object, java.lang.Object)
*/
public void test_subMapLjava_lang_ObjectLjava_lang_Object() {
// Test for method java.util.SortedMap
// java.util.TreeMap.subMap(java.lang.Object, java.lang.Object)
SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109]
.toString());
assertEquals("subMap is of incorrect size", 9, subMap.size());
for (int counter = 100; counter < 109; counter++) {
assertTrue("SubMap contains incorrect elements", subMap.get(
objArray[counter].toString()).equals(objArray[counter]));
}
try {
tm.subMap(objArray[9].toString(), objArray[1].toString());
fail("end key less than start key should throw IllegalArgumentException");
} catch (IllegalArgumentException e) {
// Expected
}
// Regression for Harmony-1161
TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>(
new MockComparatorNullTolerable());
treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$
treeMapWithNull.put(null, "value2"); //$NON-NLS-1$
SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null,
"key1"); //$NON-NLS-1$
assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$
// Regression test for typo in lastKey method
SortedMap<String, String> map = new TreeMap<String, String>();
map.put("1", "one"); //$NON-NLS-1$ //$NON-NLS-2$
map.put("2", "two"); //$NON-NLS-1$ //$NON-NLS-2$
map.put("3", "three"); //$NON-NLS-1$ //$NON-NLS-2$
assertEquals("3", map.lastKey());
SortedMap<String, String> sub = map.subMap("1", "3"); //$NON-NLS-1$ //$NON-NLS-2$
assertEquals("2", sub.lastKey()); //$NON-NLS-1$
// NOTE: The contract of this method allows us to throw either
// an NPE or a class cast exception.
TreeMap t = new TreeMap();
try {
t.subMap(null, new Object());
fail("Should throw a ClassCastException");
} catch (ClassCastException cce) {
// expected
} catch (NullPointerException npe) {
// expected
}
}