permaculture playing cards*
The moose likes Beginning Java and the fly likes Need help with inserting and displaying using HashMap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Need help with inserting and displaying using HashMap" Watch "Need help with inserting and displaying using HashMap" New topic
Author

Need help with inserting and displaying using HashMap

Avinash Sridhar
Greenhorn

Joined: Apr 11, 2007
Posts: 6
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

Joined: Apr 11, 2007
Posts: 11
nice question
I also want to see answer.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
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


cmbhatt
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36501
    
  16
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

    Joined: Oct 13, 2005
    Posts: 36501
        
      16
    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

    Joined: Feb 28, 2007
    Posts: 1707
    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

    Joined: Feb 28, 2007
    Posts: 1707
    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

    Joined: Apr 11, 2007
    Posts: 6
    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

    Joined: Oct 13, 2005
    Posts: 36501
        
      16
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Need help with inserting and displaying using HashMap
     
    Similar Threads
    Storing a count in a || array
    Locking & DBConnection (Thread) Pooling
    Problem with my algorithm...
    How to Ensure I'm NOT Passing RMI Objects I don't Mean to
    Populating HashMaps of HashMap of Objects