Java Example – Simple Java TreeMap example




Java Tutorial for Beginners
Java Tutorial for Beginners

In this Java Example we will see how to use Java TreeMap. It example also describes how to add elements to TreeMap and how to retrieve the value added from TreeMap.
TreeMap implementation

  • Uses a Red – Black tree to implement a Map
  • relies on the compareTo method of the keys
  • somewhat slower than the HashMap
  • keys stored in sorted order

We can add key value pair to TreeMap using Object put(Object key, Object value) method of Java TreeMap class, where key and value both are objects put method returns Object which is either the value previously tied to the key or null if no value mapped to the key.

Please note that put method accepts Objects. Java Primitive values CAN NOT be added directly to TreeMap. It must be converted to corresponding wrapper class first.

 

 


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com