Trending September 2023 # What Is Treemap In Java? # Suggested October 2023 # Top 14 Popular | Cersearch.com

Trending September 2023 # What Is Treemap In Java? # Suggested October 2023 # Top 14 Popular

You are reading the article What Is Treemap In Java? updated in September 2023 on the website Cersearch.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 What Is Treemap In Java?

Introduction to TreeMap in Java

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

The following are some of the properties of TreeMap.

It consists of unique elements.

It cannot have a null key.

Can have null values multiple times.

Non-synchronized.

Declaration:

Here, Key and Value are the type of keys and mapped values in this map, respectively.

Constructors of TreeMap

TreeMap(): A new and empty treemap will be constructed by naturally ordering the keys.

Methods of TreeMap

TreeMap offers a wide collection of methods that helps in performing different functions. They are:

clear(): All the mapping in the map will be removed.

clone(): A shallow copy will be returned for the TreeMap instance.

containsKey(Objectk): If there is mapping available for the specified key, true will be returned.

containsValue(Objectv): If there is mapping available for one or more keys for the value v, true will be returned.

ceilingKey(Kkey): Least key which is larger than or equal to the specified key will be returned. If there is no key, then null will be returned.

ceilingEntry(Kkey): Key-value pair for the least key, which is larger than or equal to the specified key will be returned. If there is no key, then null will be returned.

firstKey(): First or last key in the map will be returned.

firstEntry(): Key-value pair for the least or first key in the map will be returned. If there is no key, then null will be returned.

floorKey(Kkey): The Largest key, which is less than or equal to the specified key, will be returned. If there is no key, then null will be returned.

floorEntry(Kkey): Key-value pair for the largest key, which is less than or equal to the specified key, will be returned. If there is no key, then null will be returned.

lastKey(): Highest or last key in the map will be returned.

lastEntry(): Key-value pair for the largest key in the map will be returned. If there is no key, then null will be returned.

lowerKey(Kkey): The Largest key, which is strictly smaller than the specified key, will be returned. If there is no key, then null will be returned.

lowerEntry(Kkey): Key-value pair for the largest key, which is strictly smaller than the specified key, will be returned. If there is no key, then null will be returned.

remove(Objectk): Mapping for the specified key in the map will be removed.

size(): Count of key-value pairs in the map will be returned.

higherEntry(Kkey): Key-value pair for the smallest key, which is strictly larger than the specified key, will be returned. If there is no key, then null will be returned.

higherKey(Kkey): The Smallest key, which is strictly higher than the specified key, will be returned. If there is no key, then null will be returned.

descendingMap(): Reverse order will be returned for the mappings.

get(Objectk): The value of the specified key will be returned. If the key does not contain any mapping, then null will be returned.

keySet(): Set view will be returned for the keys.

navigableKeySet(): NavigableSet view will be returned for the keys.

pollFirstEntry(): Key-value pair for the least or first key in the map will be removed and returned. If the map is empty, then null will be returned.

pollLastEntry(): Key-value pairs for the greatest key in the map will be removed and returned. If the map is empty, then null will be returned.

values(): Collection view will be returned for the values in the map.

Example to Implement TreeMap in Java

Now, let us see a sample program to create a treemap and add elements to it.

import java.util.Iterator; import java.util.Map; import java.util.Set; import java.util.TreeMap; public class TreeMapExample { public static void main(String args[]) { TreeMap tmap = new TreeMap(); tmap.put("Anna Jeshua", new Double(3459.70)); tmap.put("Annamu Jeshua", new Double(321.56)); tmap.put("Izanorah Denan", new Double(1234.87)); tmap.put("Adam Jeshua", new Double(89.35)); tmap.put("Anabeth Jeshua", new Double(-20.98)); Iterator itr = set.iterator(); while(itr.hasNext()) { Map.Entry mp = (Map.Entry)itr.next(); System.out.print(" Key : " + mp.getKey() + ";"); System.out.println(" Value : "+ mp.getValue()); } System.out.println(); double val = ((Double)tmap.get("Anabeth Jeshua")).doubleValue(); tmap.put("Anabeth Jeshua", new Double(val + 2500)); System.out.println("Anabeth's new value: " + tmap.get("Anabeth Jeshua")); } }

Output:

Explanation:

Firstly, create a TreeMap and add elements to it.

In order to display the elements, an iterator has to be created.

Using the iterator, all the keys and their corresponding values are displayed.

To add 2500 to a key’s value, the put() method is also used.

Conclusion

Java TreeMap is an implementation of the Red-Black tree that helps in storing key-value pairs in sorted order. In this document, several details such as declaration, constructors, methods, and sample program of Java TreeMap is discussed in detail.

Recommended Articles

This is a guide to What is TreeMap in Java?. Here we discuss the constructors and method of treemap in java, along with an example and its code implementation. You may also look at the following articles to learn more –

You're reading What Is Treemap In Java?

Update the detailed information about What Is Treemap In Java? on the Cersearch.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!