Thursday, June 16, 2022
HomeWordPress DevelopmentWhy String is in style HashMap key in Java?

Why String is in style 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 predominant(String args[])

    {

        HashMap<String, Lengthy> hashMapOfUsers

            = new HashMap<String, Lengthy>();

        System.out.println(

            "What number of particular person particulars containing cellphone"

            "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 Consumer 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

- Advertisment -
Google search engine

Most Popular

Recent Comments