Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Hashmap

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Hashmap

    Nested for loops come to mind:
    1 for loop to iterate through the Map
    the inner one to iterate through each ArrayList.

    Inside the nested for loop fill up another Map<String, Integer> to associate each car with a count.

    In pseudo code

    Create a Map<String, Integer> called carCountMap
    Loop through all the keys of the first Map, the one with the owner/car String ArrayList
       get the associated ArrayList of Strings
       Loop through this ArrayList, getting each car String.
           Check if car String is held by carCountMap. If so, 
               get the associated Integer, increase it by one
               put it back into the carCountMap with the car String as key.
           if not, then put a new entry into the carCountMap with the car name as key and 1 as the value.
       end Loop through ArrayList.
    end for loop

  3. The Following User Says Thank You to curmudgeon For This Useful Post:

    ryan12345 (November 2nd, 2012)

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hashmap

    That's great thanks. Quick question, how would I go about looping through the associated ArrayList of Strings?

  5. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Hashmap

    Quote Originally Posted by ryan12345 View Post
    That's great thanks. Quick question, how would I go about looping through the associated ArrayList of Strings?
    Either a for loop (you know the size of the ArrayList, of course), or a for-each loop would work well here.

  6. The Following User Says Thank You to curmudgeon For This Useful Post:

    ryan12345 (November 2nd, 2012)

  7. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hashmap

    I've come up with this:

       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.

  8. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 139 Times in 134 Posts

    Default Re: Hashmap

    Please if you cross-post your question, tell all threads about the cross-post. Not informing us is not being fair to us as you then force us to duplicate answers that have already been given. We are volunteers with our own lives to lead, and we value our free time greatly. We would appreciate it if you valued our free time as well.

  9. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Hashmap

    My apologies if I caused offence, I will do so in the future. I certainly do value your replies and advice.

Similar Threads

  1. Cannot get values from hashmap
    By uhertz in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 17th, 2011, 07:44 PM
  2. [SOLVED] Should i use a hashmap?
    By 6Sloth9 in forum Collections and Generics
    Replies: 2
    Last Post: May 1st, 2011, 08:58 PM
  3. TreeMap vs HashMap
    By Kerr in forum Collections and Generics
    Replies: 7
    Last Post: March 10th, 2011, 09:12 AM
  4. Problem in HashMap
    By Hikari9 in forum Collections and Generics
    Replies: 2
    Last Post: April 19th, 2010, 10:44 PM
  5. Bitstrings vs Hashmap
    By April in forum Collections and Generics
    Replies: 3
    Last Post: February 2nd, 2010, 10:56 AM

Tags for this Thread