本文整理匯總了Java中java.util.TreeMap.subMap方法的典型用法代碼示例。如果您正苦於以下問題:Java TreeMap.subMap方法的具體用法?Java TreeMap.subMap怎麽用?Java TreeMap.subMap使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.TreeMap
的用法示例。
在下文中一共展示了TreeMap.subMap方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getRangeMap
import java.util.TreeMap; //導入方法依賴的package包/類
public void getRangeMap(TreeMap<String, String> maps, String firstKey, String lastKey) {
SortedMap<String,String> subMaps = maps.subMap(firstKey, lastKey);
Iterator iterator = subMaps.entrySet().iterator();
System.out.println("子Map如下");
while (iterator.hasNext()) {
Map.Entry<String, String> entry = (Map.Entry<String, String>) iterator.next();
System.out.print("key = " + entry.getKey());
System.out.println(" value = " + entry.getValue());
}
}
示例2: testSubMapContents
import java.util.TreeMap; //導入方法依賴的package包/類
/**
* subMap returns map with keys in requested range
*/
public void testSubMapContents() {
TreeMap map = map5();
NavigableMap sm = map.subMap(two, true, four, false);
assertEquals(two, sm.firstKey());
assertEquals(three, sm.lastKey());
assertEquals(2, sm.size());
assertFalse(sm.containsKey(one));
assertTrue(sm.containsKey(two));
assertTrue(sm.containsKey(three));
assertFalse(sm.containsKey(four));
assertFalse(sm.containsKey(five));
Iterator i = sm.keySet().iterator();
Object k;
k = (Integer)(i.next());
assertEquals(two, k);
k = (Integer)(i.next());
assertEquals(three, k);
assertFalse(i.hasNext());
Iterator r = sm.descendingKeySet().iterator();
k = (Integer)(r.next());
assertEquals(three, k);
k = (Integer)(r.next());
assertEquals(two, k);
assertFalse(r.hasNext());
Iterator j = sm.keySet().iterator();
j.next();
j.remove();
assertFalse(map.containsKey(two));
assertEquals(4, map.size());
assertEquals(1, sm.size());
assertEquals(three, sm.firstKey());
assertEquals(three, sm.lastKey());
assertEquals("C", sm.remove(three));
assertTrue(sm.isEmpty());
assertEquals(3, map.size());
}
示例3: testSubMapContents2
import java.util.TreeMap; //導入方法依賴的package包/類
public void testSubMapContents2() {
TreeMap map = map5();
NavigableMap sm = map.subMap(two, true, three, false);
assertEquals(1, sm.size());
assertEquals(two, sm.firstKey());
assertEquals(two, sm.lastKey());
assertFalse(sm.containsKey(one));
assertTrue(sm.containsKey(two));
assertFalse(sm.containsKey(three));
assertFalse(sm.containsKey(four));
assertFalse(sm.containsKey(five));
Iterator i = sm.keySet().iterator();
Object k;
k = (Integer)(i.next());
assertEquals(two, k);
assertFalse(i.hasNext());
Iterator r = sm.descendingKeySet().iterator();
k = (Integer)(r.next());
assertEquals(two, k);
assertFalse(r.hasNext());
Iterator j = sm.keySet().iterator();
j.next();
j.remove();
assertFalse(map.containsKey(two));
assertEquals(4, map.size());
assertEquals(0, sm.size());
assertTrue(sm.isEmpty());
assertSame(sm.remove(three), null);
assertEquals(4, map.size());
}
示例4: map5
import java.util.TreeMap; //導入方法依賴的package包/類
/**
* Returns a new map from Integers 1-5 to Strings "A"-"E".
*/
private static NavigableMap map5() {
TreeMap map = new TreeMap();
assertTrue(map.isEmpty());
map.put(zero, "Z");
map.put(one, "A");
map.put(five, "E");
map.put(three, "C");
map.put(two, "B");
map.put(four, "D");
map.put(seven, "F");
assertFalse(map.isEmpty());
assertEquals(7, map.size());
return map.subMap(one, true, seven, false);
}