• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with inserting and displaying using HashMap

 
Avinash Sridhar
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

Each title has a bunch of question. Those questions should have numbering to be displayed.

I am trying to find out how I can use a HashMap to do this.

HashMap quesAnsHash = new HashMap();



forEach title, i want to use something like this

quesAnsHash.put(question,new Integer(1)); // This integer should increment for each question within the title.



I need to increment this integer for subsequent questions for the same title

and I need help to display them too..

Please let me know how I can do this. Hopefully, I question is conveyed properly.

Thanks
 
Amruta Agarwal
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nice question
I also want to see answer.
 
Chandra Bhatt
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Avinash,

HashMap<K,V>
Key and Value

Instead of making key your question why don't you do vice versa. Is there any special need to do so.

Key is unique, only one null is allowed. If you follow what I said, you may do it conveniently.

Taking risk of making a question (that probably would be a big String or StringBuffer or so), it wont be nice attempt.

What do you say?

Otherwise you can also follow this:



But if question is repeated, (a slight difference in the String)? That you have to tacle.

Does this help you to solve the issue?

Regards,
cmbhatt
 
Campbell Ritchie
Sheriff
Pie
Posts: 49849
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Do you really need a HashMap? An ArrayList would be easier.
Find the API documentation for the Map interface and the HashMap class. Read about Maps in the Java Tutorial.
Suggest:
  • Set up a Question class (or change Question to String throughout)
  • You need a HashMap to hold Questions as V=values using Integers as your K=keys.
  • Declare a Map<Integer, Question> questionMap object as a field
  • Initialise it as a HashMap<Integer, Question> in your constructor. No need for constructor arguments
  • Set up an int field and call it counter or similar. Initialise it to 1 in the constructor.
  • Set up an "addQuestion(Question q)" method.
  • In that method, whenever a Question is sent, use the method of questionMap which inserts members, using "counter++" and "q" as the two things to put in. Counter will probably be converted to an Integer object by auto-boxing, so you don't have to say "new Integer(counter++)"
  • Set up a printQuestion(int number) method.
  • In that method use the questionMap method which returns the "V" using "number" as the argument.
  • Easy.
  •  
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49849
    70
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Both of you have managed to misread the lettering in the Map<K, V> interface; both have written map.put(question, counter) or Map<String, Integer>. It ought to be put(counter++, question) or Map<Integer, Question>.
     
    Chandra Bhatt
    Ranch Hand
    Posts: 1710
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Campbell,


    Didn't you see my following line,

    Instead of making key your question why don't you do vice versa.


    Even writing Map<String,Integer> or HashMap<String,Integer>, I dont see any problem in them.
    Can't String be a Key? Why not?


    Correct me, if wrong.

    cmbhatt
     
    Chandra Bhatt
    Ranch Hand
    Posts: 1710
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    How my previous descriptive post not complete. Some 10 lines were also there where I had another solution. Devil ate it.


    It was :




    Is that OK?


    Regards,
    cmbhatt
     
    Avinash Sridhar
    Greenhorn
    Posts: 6
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks a lot for the useful insight. I will try them out and see whether I can print them on the console
     
    Campbell Ritchie
    Sheriff
    Pie
    Posts: 49849
    70
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you, Chandra Bhatt. Yes, you do usually use String as the "K" variable in a Map, but here Avinash Sridhar specifically said he wants the questions to be numbered, so the number (int or Integer) would be the "K." You want to enter "No 1" and get "How do you insert into a HashMap?" rather than vice versa.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic