The Java TreeMap class shops knowledge in a tree construction utilizing a map interface. This class extends the AbstractMap class and, like its mother or father class, TreeMap has two kind parameters. One in all its kind parameters represents the keys within the TreeMap, whereas the opposite represents the values.
The TreeMap knowledge construction shops key-value pairs and permits you to carry out CRUD operations on this knowledge.
The way to Create a TreeMap in Java
The TreeMap class has 4 constructors that you should use to create a brand new TreeMap object. The default constructor is the preferred of the 4. This constructor takes no arguments and generates an empty tree map.
TreeMap<Integer,String> clients = new TreeMap<Integer,String>();
The code above generates an empty tree map known as clients.
Populating the TreeMap Knowledge Construction
The put() technique provides an merchandise to a TreeMap object. It takes two arguments—a key and its worth. You’ll be able to add gadgets to the tree map in any random order and the information construction will retailer them in ascending order, in line with their keys.
clients.put(105, "Jessica Jones");
clients.put(102, "Mark Williams");
clients.put(104, "Phil Blair");
clients.put(101, "Kim Brown");
clients.put(103, "Jim Riley");
The code above provides 5 clients, in random order, to the shoppers tree map.
Viewing Gadgets in a TreeMap
The TreeMap class shops its knowledge in an object. So, to see all of the gadgets in a tree map you possibly can merely print the tree map object to the console:
System.out.println(clients);
The code above prints the next output to the console:
101=Kim Brown, 102=Mark Williams, 103=Jim Riley, 104=Phil Blair, 105=Jessica Jones
Notice that the thing above shows the gadgets in ascending order. You too can view every merchandise and its corresponding key utilizing a Java for loop.
for (Entry<Integer, String> buyer : clients.entrySet())
System.out.println("Key: " + buyer.getKey() + " Worth: " + buyer.getValue());
The code above prints the next output to the console:
Key: 101 Worth: Kim Brown
Key: 102 Worth: Mark Williams
Key: 103 Worth: Jim Riley
Key: 104 Worth: Phil Blair
Key: 105 Worth: Jessica Jones
Updating Gadgets in a TreeMap
The TreeMap class permits you to replace an current merchandise utilizing the substitute() technique. There are two substitute strategies. The primary technique takes an current key and the brand new worth you wish to map the present key to.
clients.substitute(101,"Kim Smith");
System.out.println(clients);
The code above prints the next object within the console:
101=Kim Smith, 102=Mark Williams, 103=Jim Riley, 104=Phil Blair, 105=Jessica Jones
As you possibly can see Kim Brown is now Kim Smith. The second substitute() technique takes an current key, the important thing’s present worth, and the brand new worth you wish to map to the important thing.
clients.substitute(103,"Jim Riley", "Michelle Noah");
System.out.println(clients);
The code above prints the next object within the console:
101=Kim Brown, 102=Mark Williams, 103=Michelle Noah, 104=Phil Blair, 105=Jessica Jones
Within the object above Michelle Noah replaces Jim Riley.
Deleting Gadgets From the TreeMap
If you wish to take away a single merchandise from the tree map, the take away() technique is your solely choice. It takes the important thing related to the merchandise you wish to take away and returns the deleted worth.
clients.take away(104);
System.out.println(clients);
Operating the code above prints the next object to the console:
101=Kim Smith, 102=Mark Williams, 103=Michelle Noah, 105=Jessica Jones
This Java Class additionally has a clear() technique that permits you to delete all of the gadgets within the tree map.
The TreeMap vs. the HashMap Java Class
TreeMap and HashMap are two of the extra well-liked Java map lessons. They each lengthen the AbstractMap class. This relationship offers the TreeMap and HashMap lessons entry to lots of the identical capabilities.
Nonetheless, there are some noteworthy variations between these two map lessons. The TreeMap makes use of a Pink-Black tree implementation of the Map interface, whereas the HashMap makes use of a hash desk. HashMap permits you to retailer a single null key, whereas TreeMap doesn’t. Lastly, a HashMap is quicker than a TreeMap. The previous’s algorithmic velocity is O(1) whereas the latter’s is O(log(n)).