Let's learn java programming language with easy steps. This Java tutorial provides you complete knowledge about java technology.

Sunday 11 June 2017

Java TreeMap

 TreeMap Class

TreeMap is a class in java which is available in java.util.* package. TreeMap class implements Map interface and extends AbtractMap class in java collection framework.

TreeMap class is a Red-Black tree based NavigableMap implementation.

TreeMap class Stores values on the basis of key i.e it contains key & value pairs like HashMap, LinkedHashMap class.

TreeMap stores the elements in ascending order.


Some Points About TreeMap Class

  • TreeMap class implements Map interface and extends AbstractMap class.
  • TreeMap class contains key & value pairs.
  • TreeMap class contains only unique elements i.e it doesn't contain duplicate elements.
  • TreeMap class stores the elements in ascending order.
  • It cannot have null key but can have multiple null values.

Hierarchy of TreeMap Class

Java TreeMap

In the above hierarchy, TreeMap class implements the Navigable interface and Navigable interface extends SortedMap interface and SortedMap interface extends Map interface.



Constructors of TreeMap Class

There are some constructors of TreeMap class.

1) TreeMap()

It is used to create a new and empty tree map that will be sorted using the natural ordering of its key.

2) TreeMap(Comparator  comp)

This constructor is used to create a new and empty tree map that will be sorted using the given comparator comp.


3) TreeMap(Map m)

This constructor initializes a tree map with the entries from m, which will be sorted using the natural order or its key.

4) TreeMap(SortedMap sm)

This constructor initializes a tree map with the entries from the SortedMap  sm, which will be sorted in the same order as sm.


Methods of TreeMap Class

There are certain methods of TreeMap Class, These are

1) void clear()

This method removes all of the mapping from this map.

2) Object clone()

This method returns a shallow copy of the TreeMap instance.

3) boolean containsKey(Object key)

This method returns true if this map contains a mapping for the specified key.

4) boolean containsValue(Object value)

This method returns true if this map maps one or more keys to the specified value.

5) Object firstKey()

This method returns the first(lowest) key currently in this sorted map.

6) Set keySet()

This method returns a Set view of the key contained in this map.

7) Set entrySet()

Returns a Set view of the mappings contained in this map.

8) int size()

This method returns the number of key-value mappings in this map.

9) Collection values()

This method returns a collection view of the values contained in this map.

10) void putAll(Map m)

Copies all the elements of a map to the another specified map.

11) Object lastKey()

This method returns the last(highest) key currently in this sorted map.


Java TreeMap Example

import java.util.*;
class TreeMapExample
{
public static void main(String args[])
{
//creating TreeMap object
TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
tm.put(5, "sachin");
tm.put(4, "virat");
tm.put(3, "rohit");
tm.put(2, "bumrah");
tm.put(1, "dhoni");

for(Map.Entry me : tm.entrySet())
{
System.out.println(me.getKey()+" "+me.getValue());
}
}


output : 1 dhoni
              2 bumrah
              3 rohit
              4 virat
              5 sachin

Q. What is difference between HashMap and TreeMap class ?

Ans. HashMap : HashMap class doesn't maintains any order of elements.

TreeMap : TreeMap maintains ascending order of an elements.


Share:

2 comments:

  1. Thanks very informative and helpful.
    Great java tutorial blog.

    ReplyDelete
  2. well explained .There is also good treemap exaples visit Treemap Tutorial

    ReplyDelete

Facebook Page Likes

Follow javatutorial95 on twitter

Popular Posts

Translate