TreeMap subMap(K fromKey K toKey) method in java code example
Example 1: TreeMap subMap(K fromKey K toKey) method in java
import java.util.SortedMap;
import java.util.TreeMap;
public class TreeMapSubMapFromKeyMethodExample
{
public static void main(String[] args)
{
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
SortedMap<Integer, String> sm = new TreeMap<Integer, String>();
tm.put(18, "violet");
tm.put(12, "red");
tm.put(14, "violet");
tm.put(16, "green");
tm.put(20, "blue");
System.out.println("Gett a portion of TreeMap: ");
sm = tm.subMap(12, 18);
System.out.println("Sub map values are: " + sm);
}
}
Example 2: TreeMap subMap() method in java
import java.util.NavigableMap;
import java.util.TreeMap;
public class TreeMapSubMapMethodExample
{
public static void main(String[] args)
{
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
NavigableMap<Integer, String> nm = new TreeMap<Integer, String>();
tm.put(18, "violet");
tm.put(12, "red");
tm.put(14, "violet");
tm.put(16, "green");
tm.put(20, "blue");
System.out.println("Get a portion of TreeMap: ");
nm = tm.subMap(12, true, 18, true);
System.out.println("Sub map values are: " + nm);
}
}