Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9105757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:10:43+00:00 2026-06-17T02:10:43+00:00

Map<Object,String> mp=new HashMap<Object, String>(); Scanner sc=new Scanner(System.in); System.out.println(Enter the instrument name); String name=sc.next(); mp.put(name,

  • 0
      Map<Object,String> mp=new HashMap<Object, String>();
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter the instrument name");
      String name=sc.next();


        mp.put(name, "Control Valve");
        mp.put(name, "BDV");
        mp.put(name, "SDV");
        mp.put(name, "ON-OFF VALVE");
        mp.put(name,"Analyser");


        Set s=mp.entrySet();

        Iterator it=s.iterator();

        while(it.hasNext())
        {

            Map.Entry m =(Map.Entry)it.next();

            String key=(String)m.getKey();

            String value=(String)m.getValue();

            System.out.println("Instrument name :"+key+"  fields:"+value);
        }
    }
}

In this only last value is mapped to the key .i.e analyser to key .

How to map all the values to one key entered by the user .And also it has to ask user to enter values for each value field.

updated code -:It asks for instrument name but then shows exception “java.util.ArrayList cannot be cast to java.lang.String”

 Map<String,List<String>> mp=new HashMap<String,List<String>>();
      Scanner sc=new Scanner(System.in);
      System.out.println("Enter the instrument name");
      String name=sc.next();
      List<String> valList = new ArrayList<String>();
      valList.add("Control Valve");
      valList.add("BDV");
      valList.add("SDV");
      valList.add("ON-OFF VALVE");
      valList.add("Analyser");
      mp.put(name, valList);




        Set s=mp.entrySet();

        Iterator it=s.iterator();

        while(it.hasNext())
        {

            Map.Entry m =(Map.Entry)it.next();

            String key=(String)m.getKey();

            String value=(String)m.getValue();

            System.out.println("Instrument name :"+key+" fields:"+value);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:10:45+00:00Added an answer on June 17, 2026 at 2:10 am

    I would suggest to use

    Map<Object,List<String>> mp=new HashMap<Object, List<String>>();
    

    So that you can maintain set of values to a given key.

    if a list available for given key , get the list and add the new value to list.

    UPDATED

    Map<String,List<String>> mp=new HashMap<String,List<String>>();
          Scanner sc=new Scanner(System.in);
          System.out.println("Enter the instrument name");
          String name=sc.next();
          List<String> valList = new ArrayList<String>();
          valList.add("Control Valve");
          valList.add("BDV");
          valList.add("SDV");
          valList.add("ON-OFF VALVE");
          valList.add("Analyser");
          mp.put(name,valList);
    
          for(String key : mp.keySet()){
              System.out.print("Instrument name :"+key+" Values : ");
              for(String val : mp.get(key)){
                  System.out.print(val+",");
              }
    
    
          }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's what I want to do: Map<String, Object> model = new Hashmap<String, Object>(); model.put(a,
Gson gson = new Gson(); Map<String,Object> map = new HashMap<String, Object>(); map.put(a, 1); map.put(b,
//set input parameters Map<String,Object> inParams = new HashMap<String,Object>(); inParams.put(Sig,resourceHistoryBean.getId()); List<ResourceHistoryBean> resourceHistoryList= new ArrayList<ResourceHistoryBean>(); //
I have this object: Map<Long, Set<String>> resourcesByID = new HashMap<Long, Set<String>>(); and another: Set<String>
I have collection of maps. Collection<Map<String,Object>> xyz = (Collection<Map<String,Object>>) someMethod(); xyz.add(new HashMap<>()); If I
Map testMap = new HashMap(); for(Map.Entry<String, Object> entry:testMap.entrySet()){ } it has error tip:Type mismatch:
List<Object> myList = new ArrayList<String>(); //(hint: no) Map<Integer> myMap = new HashMap<int>(); // (hint:
In spring, I can do like this Map<String, Object> model = new HashMap<String, Object>();
I deploy rest web-service (Jersey) in embedded jetty. My server: Map<String,Object> initMap = new
I have for instance this Map: Map<String, Integer> map = new HashMap<String,Integer>(); than I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.