Question Index

Write a program to get value from given key by using the one of the Map interface methods.

Input (Map, String) Output (Integer)
{A=1, B=2, C=3, D=4, E=5}, 'A' 1
{Lion=1, Tiger=2, Cat=3, Rabbit=4, Mouse=5}, 'Tiger' 2
{Ferrari=1, Mercedes=2, BMW=3, Audi=4, Ford=3}, 'Mercedse' null
null, 'Rabbit' null

import java.util.*;
class GetValue
    public static void main(String s[])
        Map<String, Integer> input = new TreeMap<String, Integer>();
        input.put("A", 1);
        input.put("B", 2);
        input.put("C", 3);
        input.put("D", 4);
        input.put("E", 5);
        System.out.println("The value of given key is : " + getValue(input, "B"));


    public static Integer getValue(Map<String, Integer> input, String key) {




Topic: TreeMap In Java - java.util.TreeMap

Read this topic
Take test on this topic

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App