TreeMap in java 8

Java TreeMap in Java 8.

TreeMap is implements navigableMap and NavigableMap extends SortedMap and sortedMap extends Map interface in java .

TreeMap class able to  access  all NavigableMap,SortedMap,and Map interface methods .

Treemap Is  A Red-Black tree based NavigableMap implementation. The map is sorted according to the natural ordering of its keys, or by a Comparator provided at map creation time, depending on which constructor is used.

The SortedMap interface provides functionalities to maintain the order of keys.

 the NavigableMap interface provides functionalities to navigate through the map.

For instance , finding the entry just greater than or just less than the given key, finding the first and last entry in the TreeMap etc.

We know that  a TreeMap implements NavigableMap interface, it has the functionalities of both the NavigableMap as well as the SortedMap.

Following key features about TreeMap:

  • A TreeMap is always sorted based on keys. The sorting order follows the natural ordering of keys. You may also provide a custom Comparator to the TreeMap at the time of creation to let it sort the keys using the supplied Comparator.
  • A TreeMap cannot contain duplicate keys.
  • TreeMap cannot contain the null key. However, It can have null values.
  • TreeMap is not synchronized. Access to TreeMaps must be synchronized explicitly in a multi-threaded environment.
  • Java TreeMap maintains ascending order.

Consturctors in TreeMap:

TreeMap():Constructs an empty tree map that will be sorted by using the natural order of its keys

Constructor and Description
TreeMap() Constructs a new, empty tree map, using the natural ordering of its keys.
TreeMap(Comparator<? super K> comparator) Constructs a new, empty tree map, ordered according to the given comparator.
TreeMap(Map<? extends K,? extends V> m) Constructs a new tree map containing the same mappings as the given map,  ordered according to the natural ordering of its keys.
TreeMap(SortedMap<K,? extends V> m) Constructs a new tree map containing the same mappings and using the same ordering as  the specified sorted map.

Creating a TreeMap

1. Simple TreeMap

This example shows how to create a simple TreeMap and add new key-value pairs to it. The entries in the TreeMap will be sorted based on the natural ordering of keys –

import java.util.SortedMap;

import java.util.TreeMap;

public class CreateTreeMapExample {

    public static void main(String[] args) {

        // Creating a TreeMap

        SortedMap<Integer, String> mostHundredOdi  = new TreeMap<>();

        // Adding new key-value pairs to a TreeMap

        mostHundredOdi.put(43,”Virat Kohli”);

        mostHundredOdi.put(26,”Kumar Sangakkara”);

        mostHundredOdi.put(30,”Ricky Ponting”);

        mostHundredOdi.put(27,”Rohit sharma”);

        mostHundredOdi.put(49,”Sachin Tendulkar”);

        // Printing the TreeMap (Output will be sorted based on keys)

        System.out.println(mostHundredOdi);

    }

}

output:

{26=Kumar
Sangakkara, 27=Rohit sharma, 30=Ricky Ponting, 43=Virat Kohli, 49=Sachin
Tendulkar}

TreeMap with a custom Comparator (Descending Order)

This example demonstrates how to create a TreeMap with a custom comparator that orders the TreeMap entries in the descending order of keys

import java.util.SortedMap;

import java.util.TreeMap;

import java.util.Comparator;

public class CreateTreeMapExample {

    public static void main(String[] args) {

        // Creating a TreeMap

        //SortedMap<Integer, String> mostHundredOdi  = new TreeMap<>(Comparator.reverseOrder());

        //we can sort using lambda expression also.

        SortedMap<Integer, String> mostHundredOdi  = new TreeMap<>((I1,I2)->(I1>I2)?-1:(I1<I2)?1:0);

        // Adding new key-value pairs to a TreeMap

        mostHundredOdi.put(43,”Virat Kohli”);

        mostHundredOdi.put(26,”Kumar Sangakkara”);

        mostHundredOdi.put(30,”Ricky Ponting”);

        mostHundredOdi.put(27,”Rohit sharma”);

        mostHundredOdi.put(49,”Sachin Tendulkar”);

        // Printing the TreeMap (Output will be sorted based on keys)

        System.out.println(mostHundredOdi);

    }

}

output:

{49=Sachin Tendulkar, 43=Virat Kohli, 30=Ricky Ponting, 27=Rohit sharma, 26=Kumar Sangakkara}

Also read :

Leave a Reply