Learn TreeMap in Java with Easy Programming Example

In this chapter you will learn:

  1. What is TreeMap class in Java?
  2. TreeMap Programming Example
  3. Constructors and Methods of TreeMap class in Java

What is TreeMap class in Java?

TreeMap class in Java uses Tree based Map to store elements. A Tree based map provides faster searching and retrieval of data and sorting is based on natural order of keys(More often it is an ascending order). A TreeMap is not implicitly Synchronized, it means multiple thread can access TreeMap but only one Thread have right to modify treemap. However, you can explicitly sync treemap class in java. More about TreeMap class in Java is listed below:

  1. A TreeMap class stores values based on Key.
  2. It contains only unique elements.
  3. It doesn’t accept null key however it can have multiple null values.
  4. It is almost same as HashMap but it maintains ascending order.

Constructors and Methods

Constructors of Java TreeMap class

ConstructorDescription
TreeMap()It is used to construct an empty tree map that will be sorted using the natural order of its key
TreeMap(Comparator comp)It is used to construct an empty tree-based map that will be sorted using the comparator comp.
TreeMap(Map m)It is used to initialize a tree map with the entries from m, which will be sorted using the natural order of the keys.
TreeMap(SortedMap sm)It is used to initialize a tree map with the entries from the SortedMap sm, which will be sorted in the same order as sm.
MethodsDescription
boolean containsKey(Object key)It is used to return true if this map contains a mapping for the specified key.
boolean containsValue(Object value)It is used to return true if this map maps one or more keys to the specified value.
Object firstKey()It is used to return the first (lowest) key currently in this sorted map.
Object get(Object key)It is used to return the value to which this map maps the specified key.
Object lastKey()It is used to return the last (highest) key currently in this sorted map.
Object remove(Object key)It is used to remove the mapping for this key from this TreeMap if present.
void putAll(Map map)It is used to copy all of the mappings from the specified map to this map.
Set entrySet()It is used to return a set view of the mappings contained in this map.
int size()It is used to return the number of key-value mappings in this map.
Collection values()It is used to return a collection view of the values contained in this map.

Programming Example

Output

{1=Jack, 2=Steven, 3=Clark, 4=Sarah}
Key is : 1 and Value is : Jack
Key is : 2 and Value is : Steven
Key is : 3 and Value is : Clark
Key is : 4 and Value is : Sarah
Key : 1 | Value = Jack
Key : 2 | Value = Steven
Key : 3 | Value = Clark
Key : 4 | Value = Sarah
Item Removed : Steven
First Key of this list is : 1
Last Key of this list is : 4
Value of Key 3 is : Clark
_

Summary

In this chapter you learned TreeMap class in Java with complete and easy programming example. My aim is to teach you Java with the help of easy programming not the help of bulky theory. Once you will be familiar with collection objects you will start exploring collection more deeply than tutorial.


Leave a Reply

Your email address will not be published. Required fields are marked *