Monday, July 4, 2022
HomeSoftware DevelopmentWhy String is widespread HashMap key in Java?

Why String is widespread HashMap key in Java?


import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.Scanner;

public class StringHashMapExample {

    public static void major(String args[])

    {

        HashMap<String, Lengthy> hashMapOfUsers

            = new HashMap<String, Lengthy>();

        System.out.println(

            "What number of particular person particulars containing telephone"

            "numbers are going to get saved: ");

        Scanner scanner = new Scanner(System.in);

        int phoneNumber = scanner.nextInt();

        for (int i = 0; i < phoneNumber; i++) {

            System.out.println(

                "Enter Person Title [key (String)]: ");

            String key = scanner.subsequent();

            System.out.println(

                "Enter Telephone Quantity [value (Long)]: ");

            lengthy worth = scanner.nextLong();

  

            

            

            hashMapOfUsers.put(key.toLowerCase(), worth);

        }

        System.out.println(

            "Get the main points of customers . . . . . .");

        Iterator hmIterator

            = hashMapOfUsers.entrySet().iterator();

  

        

        whereas (hmIterator.hasNext()) {

            Map.Entry hashMapElement

                = (Map.Entry)hmIterator.subsequent();

  

            

            

            System.out.println(hashMapElement.getKey()

                               + " : "

                               + hashMapElement.getValue());

        }

  

        System.out.println(

            "Enter a reputation to look within the hashmap(key): ");

        String reqKey = scanner.subsequent();

  

        

        

        System.out.println(

            "Telephone quantity (worth): "

            + hashMapOfUsers.get(reqKey.toLowerCase()));

    }

}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments