Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
treemap

What is the TreeMap class in Java?

Non Stop Learning...

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Introduction

The TreeMap class belongs to java.util.package. The TreeMap is stored in the form of keys and values format, but it maintains ascending and sorting order by using their keys.

Syntax

public class TreeMap<K,V> extends AbstractMap<K,V> implements   NavigableMap<K,V>, Cloneable, Serializable

Parameters

K: This is the type of key maintained in the TreeMap

V: This is the type of value maintained in the TreeMap

Example

import java.util.TreeMap;
class Main {
public static void main(String[] args) {
// Creating TreeMap of empID
TreeMap<String, Integer> empID = new TreeMap<>();
// Using put()
empID.put("John", 3);
empID.put("Charlie",4);
empID.put("Jack",5);
System.out.println("TreeMap of empID:"+empID);
}
}

Explanation

  • First, import the TreeMap from the util, and introduce the main class.
  • Create an object with arguments of string and integer of empID.
  • Using the put method enter the keys(as string) and values(as integer).
  • Now print empID.
  • It will return the natural sorting order of empID using their keys.
  • Likewise, you can use the many methods of TreeMap. For example, remove method to remove the element.

Some methods supported by TreeMap

Method Name

Syntax

Usage

clear

Tree_Map.clear() 

The clear method is used to remove all key-value from the tree map.

containsKey

Tree_Map.containsKey(key_element)


The containsKey method is used to check whether the key is present in the tree map or not.

containsValue

Tree_Map.containsValue(Object Value)


The containsValue method is used to check whether a particular value is present in the tree map or not.

get

Tree_Map.get(Object key_element)


The get method is used to get a value corresponding to the particular key.

keySet

Tree_Map.keySet()


The keySet method is used to get a set of keys.

put

Tree_Map.put(key, value)


The put method is used to store a key and value.

remove

Tree_Map.remove(Object key)


The remove method is used to remove the key from the tree map.

size

Tree_Map.size(key)


The size method is used to check the size of the tree map.

values

Tree_Map.values()


The values method will return a collection of values.

RELATED TAGS

java
treemap

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring