Question Index

Write a program to check the given Map contains given key or not.Use one of the Map interface methods.

Input (Map, String) Output (Boolean)
{A=1, B=2, C=3, D=4, E=5}, 'B' true
{P=12, Q=-20, R=36, S=22, T=96, X=2, Y=5}, 'T' true
{Lion=1, Tiger=2, Cat=3, Rabbit=4, Mouse=5, Donkey=6}, 'Rain' false
{Ferrari=1, Mercedes=2, BMW=3, Audi=4, Ford=3}, null null

import java.util.*;
class ContainsKey
    public static void main(String s[])
        Map<String, Integer> input = new LinkedHashMap<String, Integer>();
        input.put("A", 1);
        input.put("E", 5);
        input.put("B", 2);
        input.put("C", 3);
        input.put("D", 4);
        System.out.println("Map has key: " + hasKey(input, "A"));


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




Topic: LinkedHashMap In Java with Code Example

Read this topic
Take test on this topic

Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App