Results 1 to 11 of 11
Thread: HashMap
- 11-02-2012, 05:33 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
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.
- 11-02-2012, 05:37 PM #2
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!
- 11-02-2012, 06:06 PM #3
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
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.
- 11-02-2012, 08:40 PM #4
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!
- 11-02-2012, 09:51 PM #5
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
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.
- 11-03-2012, 12:53 AM #6
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
Re: HashMap
I've come up with this:
However when I call it, it doesn't seem to be adding up the amount of cars correctly.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()); }
- 11-03-2012, 01:33 AM #7
Member
- Join Date
- Mar 2012
- Posts
- 88
- Rep Power
- 0
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....
- 11-03-2012, 01:39 AM #8
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
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.
- 11-03-2012, 05:11 AM #9
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: HashMap
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:carsListed.put(cars.get(key).get(x), carsListed.get(cars.get(key).get(x))+1);// This is a hot mess
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());
- 11-03-2012, 03:35 PM #10
Member
- Join Date
- Oct 2012
- Posts
- 13
- Rep Power
- 0
Re: HashMap
Not sure what to put where the *'s are. I want it to add one to the amount of the existing key.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()); }Last edited by ryan1234; 11-03-2012 at 03:53 PM.
- 11-03-2012, 11:17 PM #11
Senior Member
- Join Date
- Oct 2012
- Posts
- 108
- Rep Power
- 0
Re: HashMap
well, you have to get what's already there, and add 1 to it
-.gif)
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
-
final HashMap hm=new HashMap();
By sangramkeshari.jena in forum New To JavaReplies: 4Last Post: 07-21-2011, 09:44 PM -
Hashmap to TXT and TXT to Hashmap
By elvinny in forum Advanced JavaReplies: 4Last Post: 02-16-2011, 11:12 PM -
How to create a new HashMap from a HashMap entries of other methods
By pandeyalok in forum Advanced JavaReplies: 7Last Post: 12-08-2009, 07:17 PM -
hashmap
By tOpach in forum New To JavaReplies: 2Last Post: 09-24-2008, 12:55 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks