Example usage for java.util NavigableMap headMap

List of usage examples for java.util NavigableMap headMap

Introduction

In this page you can find the example usage for java.util NavigableMap headMap.

Prototype

SortedMap<K, V> headMap(K toKey);

Source Link

Document

Equivalent to headMap(toKey, false) .

Usage

From source file:Main.java

public static void main(String[] args) {
    NavigableMap<Integer, String> map = new TreeMap<Integer, String>();
    map.put(2, "two");
    map.put(1, "one");
    map.put(3, "three");
    System.out.println(map.headMap(1) + "\n");

}

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

/**
 * Test method for 'java.util.SortedMap.headMap(Object)' and
 * 'java.util.NavigableMap.headMap(Object, boolean)'.
 *
 * @see java.util.SortedMap#headMap(Object)
 * @see java.util.NavigableMap#headMap(Object, boolean)
 *//*from   w w w .  j  a  v  a 2s  .  c o  m*/
public void testHeadMap() {
    // test with no entries
    K[] keys = getSortedKeys();
    NavigableMap<K, V> map = createNavigableMap();
    assertNotNull(map.headMap(keys[0]));
    assertNotNull(map.headMap(keys[0], false));
    assertNotNull(map.headMap(keys[0], true));
}

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

/**
 * Test method for 'java.util.SortedMap.headMap(Object)' and
 * 'java.util.NavigableMap.headMap(Object, boolean)'.
 *
 * @see java.util.SortedMap#headMap(Object)
 * @see java.util.NavigableMap#headMap(Object, boolean)
 *//*from w  w  w.  j  a  v  a2  s  .  c  o m*/
public void testHeadMap_entries() {
    K[] keys = getSortedKeys();
    V[] values = getSortedValues();
    NavigableMap<K, V> map = createNavigableMap();

    // test with a single entry map
    map.put(keys[0], values[0]);

    assertEquals(0, map.headMap(keys[0]).size());
    assertEquals(0, map.headMap(keys[0], false).size());
    assertEquals(1, map.headMap(keys[0], true).size());

    // test with two entry map
    map.put(keys[1], values[1]);

    assertEquals(0, map.headMap(keys[0]).size());
    assertEquals(1, map.headMap(keys[1]).size());
    assertEquals(keys[0], map.tailMap(keys[0]).keySet().toArray()[0]);

    assertEquals(0, map.headMap(keys[0], false).size());
    assertEquals(1, map.headMap(keys[1], false).size());
    assertEquals(keys[0], map.headMap(keys[0], true).keySet().toArray()[0]);

    assertEquals(1, map.headMap(keys[0], true).size());
    assertEquals(2, map.headMap(keys[1], true).size());
    assertEquals(keys[0], map.headMap(keys[1], false).keySet().toArray()[0]);
    assertEquals(keys[1], map.headMap(keys[1], true).keySet().toArray()[1]);
    assertEquals(0, map.headMap(keys[0], false).keySet().size());
    assertEquals(keys[1], map.headMap(keys[1], true).keySet().toArray()[1]);
}

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

public void testHeadMapLjava_lang_Object() {
    K[] keys = getSortedKeys();// w w w.  j av a2  s  .c o  m
    V[] values = getSortedValues();
    NavigableMap<K, V> map = createNavigableMap();
    for (int i = 0; i < keys.length; i++) {
        map.put(keys[i], values[i]);
    }

    Map<K, V> head = map.headMap(keys[3]);
    assertEquals(3, head.size());
    assertTrue(head.containsKey(keys[0]));
    assertTrue(head.containsValue(values[1]));
    assertTrue(head.containsKey(keys[2]));

    if (useNullKey() && useNullValue()) {
        map.put(null, null);

        SortedMap<K, V> submap = map.headMap(null);
        assertEquals(0, submap.size());

        Set<K> keySet = submap.keySet();
        assertEquals(0, keySet.size());

        Set<Map.Entry<K, V>> entrySet = submap.entrySet();
        assertEquals(0, entrySet.size());

        Collection<V> valueCollection = submap.values();
        assertEquals(0, valueCollection.size());

        map.remove(null);
    }

    SortedMap<K, V> submap = map.headMap(getLessThanMinimumKey());
    assertEquals(submap.size(), 0);
    assertTrue(submap.isEmpty());
    try {
        submap.firstKey();
        fail("NoSuchElementException should be thrown");
    } catch (NoSuchElementException expected) {
    }

    try {
        submap.lastKey();
        fail("NoSuchElementException should be thrown");
    } catch (NoSuchElementException expected) {
    }

    try {
        submap.headMap(null);
        assertTrue("expected exception", useNullKey());
    } catch (NullPointerException e) {
        assertFalse("unexpected NPE", useNullKey());
    }
}

From source file:com.google.gwt.emultest.java.util.TreeMapTest.java

/**
 * Test method for 'java.util.Map.size()'.
 *
 * @see java.util.Map#size()/*from   w  ww  .j  av a2s.  com*/
 */
public void testSize() {
    K[] keys = getSortedKeys();
    V[] values = getSortedValues();
    NavigableMap<K, V> map = createNavigableMap();

    // Test size behavior on put
    map.put(keys[0], values[0]);
    assertEquals(1, map.size());
    map.put(keys[1], values[1]);
    assertEquals(2, map.size());
    map.put(keys[2], values[2]);
    assertEquals(3, map.size());

    // Test size behavior on remove
    map.remove(keys[0]);
    assertEquals(2, map.size());
    map.remove(keys[1]);
    assertEquals(1, map.size());
    map.remove(keys[2]);
    assertEquals(0, map.size());

    // Test size behavior on putAll
    map.put(keys[0], values[0]);
    map.put(keys[1], values[1]);
    map.put(keys[2], values[2]);
    assertEquals(3, map.size());

    // Test size behavior on clear
    map.clear();
    _assertEmpty(map);

    for (int i = 0; i < keys.length; i++) {
        map.put(keys[i], values[i]);
    }

    assertEquals(keys.length, map.size());
    for (int i = 0; i < keys.length; i++) {
        assertEquals(i, map.headMap(keys[i]).size());
    }
    assertEquals(keys.length, map.headMap(getGreaterThanMaximumKey()).size());
    for (int i = 0; i < keys.length; i++) {
        assertEquals(keys.length - i, map.tailMap(keys[i]).size());
    }
    assertEquals(keys.length, map.tailMap(getLessThanMinimumKey()).size());
    assertEquals(1, map.subMap(keys[1], keys[2]).size());
    assertEquals(2, map.subMap(keys[0], keys[2]).size());
    try {
        map.subMap(keys[2], keys[1]);
        fail("Should throw an IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    assertEquals(keys.length, map.subMap(getLessThanMinimumKey(), getGreaterThanMaximumKey()).size());
}

From source file:org.apache.hadoop.hbase.master.RegionManager.java

static MetaRegion getMetaRegionPointingTo(NavigableMap<byte[], MetaRegion> metaRegions, HRegionInfo newRegion) {
    if (metaRegions.isEmpty()) {
        return null;
    } else if (metaRegions.size() == 1) {
        return metaRegions.get(metaRegions.firstKey());
    } else {/*  www  . jav  a 2  s . c  o  m*/
        if (metaRegions.containsKey(newRegion.getRegionName())) {
            return metaRegions.get(newRegion.getRegionName());
        }
        return metaRegions.get(metaRegions.headMap(newRegion.getRegionName()).lastKey());
    }
}