Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

The TreeMap.get() method in Java

Non Stop Learning...

Overview

The TreeMap.get() method is used to get values for the key from the TreeMap entries. It returns the value of the key called or returns NULL when such key is not found in the entries on the TreeMap.

Syntax

TreeMap.get( K Key)

Parameter

Key: We need to get the value of this in the entries on TreeMap.

Example

import java.util.*;
class GetMethod // create a class
{
  public static void main(String[] args)
  { // create empty treeMap
    TreeMap<Integer, String> tm = new TreeMap<Integer, String>();
// inserting data/entries in treeMap
    tm.put(1, "Apple");
    tm.put(6, "Mango");
    tm.put(8, "Orange");
    tm.put(7, "Papaya");
    tm.put(4, "banana");
// Printing the data at given key
    System.out.println("The value for key 8 is: " + tm.get(8));
    System.out.println("The value for key 2 is: " + tm.get(2));
  }
}
TreeMap.get() method code example

Explanation

  • Line 1: We import the util package.

  • Line 2: We create the class GetMethod.

  • Line 6: We create the empty TreeMap.

  • Line 8–12 : We insert the entries in the TreeMap using TreeMap.put() method.

  • Line 14: We print that given key is found and the value of the key is returned.

  • Line 15: We print that the given key has not been found so it returns NULL.

RELATED TAGS

java
RELATED COURSES

View all Courses

Keep Exploring