1. Introduction In this example we will see how and when to use the Java Treemap class java.util.TreeMap. A TreeMap is a Red-Black tree based NavigableMap implementation which has log(n) time cost for the basic operations: add, remove, and contains. A TreeMap guarantees that the elements inserted remains sorted on the order of keys. The elements are ordered using the ...
Read More »Java Sorted Map Example
In this example we shall show you how to make use of Java Sorted Map. A SortedMap is a Map that sort its entries in ascending order according to the keys’ natural ordering, or according to a Comparator provided at the time of the SortedMap creation. All keys inserted into a SortedMap must implement the Comparable interface (or be accepted by the specified Comparator). Furthermore, all such elements must be mutually comparable (i.e, Mutually ...
Read More »Remove mapping from TreeMap example
This is an example of how to remove mapping from a TreeMap, that is removing a key value pair from a TreeMap. Removing mapping from a TreeMap implies that you should: Create a new TreeMap. Populate the map with elements, with put(K key, V value) API method of TreeMap. Invoke remove(Object o) API method of TreeMap with a key as ...
Read More »Remove all mappings from TreeMap example
In this example we shall show you how to remove all mappings from a TreeMap, that is removing all key value pairs from the TreeMap. To clear a TreeMap one should perform the following steps: Create a new TreeMap. Populate the map with elements, with put(K key, V value) API method of TreeMap. Invoke clear() API method of TreeMap. The ...
Read More »TreeMap Iterator example
With this example we are going to demonstrate how to obtain a TreeMap Iterator, that is an iterator over the key value pairs of the TreeMap. The TreeMap API provides methods to get an Iterator. In short, to obtain a TreeMap Iterator you should: Create a new TreeMap. Populate the map with elements, with put(K key, V value) API method ...
Read More »Obtain tail map from TreeMap example
This is an example of how to obtain a tail map from a TreeMap. The TreeMap API provides methods for this operation. Obtaining a tail map from a TreeMap implies that you should: Create a new TreeMap. Populate the map with elements, with put(K key, V value) API method of TreeMap. Invoke tailMap(Object fromKey) API method of TreeMap. It returns ...
Read More »Obtain sub Map from TreeMap example
In this example we shall show you how to obtain a sub Map from a TreeMap. The TreeMap API provides methods for this operation. To obtain a sub Map from a TreeMap one should perform the following steps: Create a new TreeMap. Populate the map with elements, with put(K key, V value) API method of TreeMap. Invoke subMap(Object fromKey, Object ...
Read More »Get size of TreeMap example
With this example we are going to demonstrate how to get the size of a TreeMap, that is the number of key value pairs that the TreeMap contains. In short, to get the size of a TreeMap you should: Create a new TreeMap. Populate the map with elements, with put(K key, V value) API method of TreeMap. Invoke size() API ...
Read More »Get Set view of TreeMap keys example
This is an example of how to get a Set view of the TreeMap keys. Getting a Set view of the TreeMap keys implies that you should: Create a new TreeMap. Populate the map with elements, with put(K key, V value) API method of TreeMap. Invoke keySet() API method of TreeMap. The method returns a Set of all the keys ...
Read More »Obtain lowest and highest keys from TreeMap example
In this example we shall show you how to obtain the lowest and highest keys from a TreeMap. The TreeMap API provides methods for these operations. To obtain the lowest and highest keys from a TreeMap one should perform the following steps: Create a new TreeMap. Populate the map with elements, with put(K key, V value) API method of TreeMap. ...
Read More »