Knowee
Questions
Features
Study Tools

Write a Java program to create a map with the following inputs and check if a particular key exists in the map.Input:1 Anu(Key,value)2 Renu3 Meena4 Rohith5 Iftar5(check if this key exist)outputyes

Question

Write a Java program to create a map with the following inputs and check if a particular key exists in the map.Input:1 Anu(Key,value)2 Renu3 Meena4 Rohith5 Iftar5(check if this key exist)outputyes

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Here is a simple Java program that creates a HashMap, adds the provided key-value pairs, and checks if a particular key exists:

import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        // Step 1: Create a new HashMap
        HashMap<Integer, String> map = new HashMap<>();

        // Step 2: Add key-value pairs to the map
        map.put(1, "Anu");
        map.put(2, "Renu");
        map.put(3, "Meena");
        map.put(4, "Rohith");
        map.put(5, "Iftar");

        // Step 3: Check if a particular key exists
        if (map.containsKey(5)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

This program will output "Yes" because the key 5 does exist in the map.

This problem has been solved

Solution 2

Here is a simple Java program that creates a HashMap, adds the given key-value pairs, and checks if a particular key exists in the map.

import java.util.HashMap;

public class Main {
    public static void main(String[] args) {
        // Create a new HashMap
        HashMap<Integer, String> map = new HashMap<>();

        // Add key-value pairs to the map
        map.put(1, "Anu");
        map.put(2, "Renu");
        map.put(3, "Meena");
        map.put(4, "Rohith");
        map.put(5, "Iftar");

        // Check if a particular key exists
        if (map.containsKey(5)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}

In this program, we first import the HashMap class from the java.util package. Then, we create a new HashMap named 'map'. We add the given key-value pairs to the map using the put() method. Finally, we check if the key '5' exists in the map using the containsKey() method. If the key exists, we print "Yes". Otherwise, we print "No".

This problem has been solved

Similar Questions

What is the output of the following code?import java.util.*;public class Main { public static void main(String[] args) { Map map = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); for (Map.Entry entry : map.entrySet()) { if (entry.getValue() == 2) { map.remove(entry.getKey()); } } System.out.println(map); }}a){A=1, C=3}b)IllegalArgumentExceptionc){A=1, B=2, C=3}d){B=2, A=1, C=3}e)ConcurrentModificationException

Which method of the Map interface is used to check if a specific key is present in the map?Question 27Answera.containsKey()b.hasKey()c.keyExists()d.includesKey()

import java.util.*;        public class Test {              public static void main(String[] args) {                     Map<Set<String>, List<String>> m = new HashMap<>();                     ArrayList<String> list1 = new ArrayList<>();                     list1.add("30");                     list1.add("40");                                    ArrayList<String> list2 = new ArrayList<String>();                     list2.add("35");                     list2.add("45");                     Set<String> set1 = new HashSet<>();                     set1.add("ravi");                     set1.add("ravi");                     m.put(set1,list1);                     m.put(set1,list2);                     System.out.println(m);              }         }

What will be the output of the following Java code?Code:import java.util.*;import java.util.Map.Entry;public class OrderValue{ public static void main(String a[]){ Map<String, Integer> names = new HashMap<String, Integer>(); names.put("Anne", 10); names.put("John", 35); names.put("Bob", 2); Set<Entry<String, Integer>> set = names.entrySet(); List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set); Collections.sort( list, new Comparator<Map.Entry<String, Integer>>() { public int compare( Map.Entry<String, Integer> order1, Map.Entry<String, Integer> order2) { return (order2.getValue()).compareTo( order1.getValue() ); } }); for(Map.Entry<String, Integer> entry:list){ System.out.print(" "+entry.getKey()); } }}

Identify the valid sequence of statements to read integer data from the keyboard.*1 pointimport java.util.Scanner;int num = scan.nextInt();Scanner scan = new Scanner(System.in);Scanner scan = new Scanner(System.in); import java.util.Scanner; int num = scan.nextInt();import java.util.Scanner; Scanner scan = new Scanner(System.in); int num = scan.nextInt();Scanner scan = new Scanner(System.in); int num = scan.nextInt(); import java.util.Scanner;

1/1

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.