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


Stuff I use to make videos

Windows notebook – http://amzn.to/2zcXPyF

Apple MacBook Pro – http://amzn.to/2BTJBZ7

Desktop - http://amzn.to/2zct252

Microphone – http://amzn.to/2zcYbW1

notebook mouse – http://amzn.to/2BVs4Q3

Screen Recording Software – Camtasia