Results 1 to 11 of 11

Thread: HashMap

  1. #1
    ryan1234 is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default HashMap

    I have a hashmap which currently contains people's names as keys and then has a string arraylist as the values which are the peoples currently owned cars.

    For example:

    Tom: BMW, Audi
    John: Vauxhall
    Steve: Audi, Ford

    What I want to do is, is print each car's name and how many people have it as an owned car e.g. Audi: 2, BMW: 1

    Any ideas of how I would do this?

    Thanks.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: HashMap

    How would you do this without a computer? Say you have a filing cabinet with a folder for each person. In that folder is a list of the cars s/he owns. How do you tally up how many people own which type of car?
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    ryan1234 is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: HashMap

    I would search through each folder searching for each car. But how would I do it if I don't know what I'm searching for, cos there would it a wide range of cars in the world.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,993
    Rep Power
    9

    Default Re: HashMap

    Like I said, how would you do it if you had to do it without a computer? A big part of programming is taking apart a task and thinking about how to do it without programming. Write out the steps you'd take with a piece of paper and a pencil, and when you have that, you have an algorithm that can be turned into code.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    ryan1234 is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: HashMap

    1. Take out each piece of paper containing car name from each folder.
    2. Go through each piece of paper and if new type of car found put in new pile.
    3. Count pieces of paper in each pile.

    Can do 1. but not sure how to do 2. and 3.

  6. #6
    ryan1234 is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: HashMap

    I've come up with this:

    Java Code:
       public static void listCars()
        {
            for (String key : cars.keySet()) {
               for (int x=0; x < (cars.get(key)).size(); x++)
               {
                   carsListed.put((cars.get(key)).get(x), +1);
                }
            }
            System.out.println(carsListed.toString());
        }
    However when I call it, it doesn't seem to be adding up the amount of cars correctly.

  7. #7
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: HashMap

    hey, just out the blue - what is the +1 for in line 6? That to me appears to be the 1st thing that is messing your code up....

  8. #8
    ryan1234 is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: HashMap

    I'm not exactly sure what to put there. Basically I want a variable that adds 1 to it everytime a same element is found. For example, if Audi is found twice, the variable should be 2.

  9. #9
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: HashMap

    Java Code:
                      carsListed.put(cars.get(key).get(x), carsListed.get(cars.get(key).get(x))+1);// This is a hot mess
    Is kind of what you're looking for... if you want to confuse EVERYBODY. Not to mention the fact that this will throw a NPE! (No doubt, because you never check if your carsListed.containsKey())

    Java Code:
        for (String key : cars.keySet()) {                          // OK this is fine
            for (int x=0; x < (cars.get(key)).size(); x++)      // This is fine too... but we're getting to the point of confusing author ;)
            {
                carsListed.put((cars.get(key)).get(x), +1);   // ..... and.... we've done it.  This is saying put "+1" into the carsListed Map at key [B]cars.get(key).get(x)[/B]  ... so the key for this one is a list item from an arraylist that is in another map...
                                                                            // we're now well past the point of using temporary variables to make this make sense!
                                                                            // String currentKey = cars.get(key).get(x);
                                                                            // if (carsListed.containsKey(currentKey)){ ...........
             }
         }
         System.out.println(carsListed.toString());

  10. #10
    ryan1234 is offline Member
    Join Date
    Oct 2012
    Posts
    13
    Rep Power
    0

    Default Re: HashMap

    Java Code:
    public static void listCars()
        {
            int amount = 1;
            
            for (String key : cars.keySet()) {
               for (int x=0; x < cars.get(key)).size(); x++)
               {
                  if (carsListed.containsKey((cars.get(key)).get(x)))
                  {
                      *******
                  }
                  else
                  {
                      carsListed.put((cars.get(key)).get(x), amount);
                  }
                }
            }
            System.out.println(carsListed.toString());
          }
    Not sure what to put where the *'s are. I want it to add one to the amount of the existing key.
    Last edited by ryan1234; 11-03-2012 at 03:53 PM.

  11. #11
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: HashMap

    well, you have to get what's already there, and add 1 to it
    Java Code:
    Integer cVal = carsListed.get(/*STUFF*/) + 1;   // The current count... need to use Integer (instead of int) b/c of object requirement of Map/HashMap... 
                                                                      // though I never saw your HashMap definition for carsListed
    carsListed.put(/*STUFF*/, cVal);  // Or something similar

Similar Threads

  1. final HashMap hm=new HashMap();
    By sangramkeshari.jena in forum New To Java
    Replies: 4
    Last Post: 07-21-2011, 09:44 PM
  2. Hashmap to TXT and TXT to Hashmap
    By elvinny in forum Advanced Java
    Replies: 4
    Last Post: 02-16-2011, 11:12 PM
  3. Replies: 7
    Last Post: 12-08-2009, 07:17 PM
  4. hashmap
    By tOpach in forum New To Java
    Replies: 2
    Last Post: 09-24-2008, 12:55 PM

Posting Permissions

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