Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts

    hashmap but storing 2 values with an identifier

    Hello,

    I would like the functionality of a hashmap but I have 3 values to store, one of which would be an identifier to search for and 2 other values.

    From what I can see, hashmaps only allow 1 value to be stored.

    Is there something with the same functionality? I need to be able to search by an ID.

    Thanks,

    Nicola

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    if the other 2 values are of the same type then u can put them in the hashmap as an array

    Code:
    // assuming strings
    String[] values = new String[] {"key", "value1", "value2"};
    HashMap<String, String[]> map = new HashMap<String, String[]>();
    map.put(values[0], new String[]{values[1], values[2]});
    or if they are of dissimilar types you can create a holding class
    Code:
    class Info {
      String key;
      int intValue;
      double double Value;
    }
    ...
    
    Info info = new Info();
    info.key = "key";
    HashMap<String, Info> map = new HashMap<String, Info>();
    map.put(info.key, info);
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • Users who have thanked shyam for this post:

    nickyfraggle (04-19-2008)

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Hello,

    I think I might be going about this in the wrong way, so I'm going to try to explain what I'm trying to do.

    I am drawing directed graphs from set theory files.

    A file is opened, and then a graph is drawn based on it's contents.

    Every time a circle is drawn, I want to store:

    an X and Y co-ordinate of where it started drawing it (top left X,Y coord) setName (I get this using a method)
    Location (I get this using a method)

    Users will click on the circles in my graph to open a new set theory file, using it's location and setName, so I am getting the XY coordinates of where they clicked, and then need to check it against the values I've stored to find out which setName circle they clicked on.

    Each circle drawn is 20X20, so I'll just be looking for a user clicking within 20 of the initial coordinates.

    I thought about saving these values in an instance of a class, and then storing that instance in a Vector (as more than one circle might be drawn on the screen at one time.) BUT....

    When a user clicks a) how do I know which instance of the class I'm looking for.....and how do I search the instance stored inside the vector to decide if a user has clicked within the circle of a particular instance?? (remember each instance is a new circle with it's XY start coords stored)

    I'm hoping I'm making some sense here!

    REALLY appreciate your help. I'm a final year honours student and have been banging my head against a wall with this for a little while!!! I don't need to be told line for line what to do...I just don't know where to start.

    Nicky

  • #4
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by nickyfraggle View Post
    Each circle drawn is 20X20, so I'll just be looking for a user clicking within 20 of the initial coordinates.

    I thought about saving these values in an instance of a class, and then storing that instance in a Vector (as more than one circle might be drawn on the screen at one time.) BUT....

    When a user clicks a) how do I know which instance of the class I'm looking for.....and how do I search the instance stored inside the vector to decide if a user has clicked within the circle of a particular instance?? (remember each instance is a new circle with it's XY start coords stored)
    well, you've solved your own problem...you have a vector that contains objects of a class that have x & y coordinates of the circle, location and name...now when a user clicks somewhere you get the x & y coordinates of the mouse click...all that is left to do is to iterate through the elements in the vector and calculate the distance between the center of your circles (x-10, y-10) assuming 10 is your radius and the mouse click...if the distance is less than the radius (10) then the user has clicked inside that particular instance's circle and you use that one for further processing...
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #5
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question

    Hello ! I also have a similar problem.

    I need to implement a multiple choice assessment system, but I am at the stage of reading in the values from a text file. So far I have been reading them (using a tab delimited file) where each string is placed into a different variable.

    The problem is I would like to store the Question/Answers in a hashmap like this :

    ID (as the key which is randomly generated)

    Question (Which is a seperate class type (containing the question text)

    Answers : These consist of 4 possible answers (Which are of type Question) these hold the text of the answer and also a boolean value where it is the correct answer or not.

    My problem is that I don't know how to place the answers, question and id into the hashmap.

    After reading the previous post, I see that you could create a holding class. Would this be in my case of type QA (Questions and Answers)?

    class QA {
    int key;
    Question q;
    Answers [Answer];

    If so how would I go about placing my 4 answer types into the answers part of the collection????

    Hope this makes sense.
    Thanks

  • #6
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Hello,

    I wrote a multiple choice question program last year.
    I used Vectors to store the questions and answers.

    Have you looked at these?

    Nicky

  • Users who have thanked nickyfraggle for this post:

    jon_russ_lover (04-20-2008)

  • #7
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Not got my computer on me at the moment as it decided to just not bother starting up last week and is still being fixed but I can send you some stuff to look at tomorrow if you're still stuck!

  • #8
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    No I haven't looked at those so will have a search on the internet in a bit !

    Thats great thanks, I would much appreciate it if I could view it? But only if its not too much trouble for you.

    I will pm my email address to you,
    Thanks


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •