tailMap(K fromKey, boolean inclusive) method

tailMap(K fromKey, boolean inclusive): It returns key-value pairs of NavigableMap whose keys are greater than (or equal to, if inclusive is true).
throws ClassCastException,  if specified key toKey is incompatible for TreeMap
NullPointerException, if toKey is null and TreeMap uses natural ordering, or its comparator does not permit null keys. It returns null if TreeMap is empty.

SortedMap<K,V> tailMap(K fromKey, boolean inclusive);
import java.util.TreeMap;
lass ExampleOfTreeMap
{
	public static void main(String arg[])
	{
		TreeMap<Integer, String> listOfNames = new TreeMap<Integer, String>();
		listOfNames.put(3, "C");
		listOfNames.put(1, "A");
		listOfNames.put(2, "D");
		listOfNames.put(6, "B");
		listOfNames.put(8, "B");
		
		System.out.println("Map :"+listOfNames.tailMap(3, false));
		System.out.println("Map :"+listOfNames.tailMap(3, true));

	}
}

Output: Map :{6=B, 8=B}
Map :{3=C, 6=B, 8=B}

Leave a Comment