Results 1 to 9 of 9
Thread: HashMap<Point> ? Please explain
- 09-19-2009, 01:21 AM #1
Member
- Join Date
- Sep 2008
- Posts
- 25
- Rep Power
- 0
HashMap<Point> ? Please explain
Hello, so I've never seen this type of syntax. I need to create a Hashmap with each value having an (x,y) coordinate and I was told to do this:
I know how to store items in a HashMap like this:Java Code:HashMap<Point> h = new HashMap<Point>();
But how do I assign the Point x,y?Java Code:for (int i=0; i<strParts.length; i++) { // add all words except any remaining "-" (hyphens) if (!strParts[i].equals("-")) { mapStrParts.put(i, strParts[i]); } }
Any help is appreciated thank you!
- 09-19-2009, 01:30 AM #2
I would suggest reading through the Generics Tutorial.
And you probably want this instead:
Java Code:HashMap<Integer,Point> h = new HashMap<Integer,Point>();
Which would take in something like this:
Java Code:h.put(0, new Point(0,0)); h.put(1, new Point(1,1)); System.out.println("Point 0 - " + h.get(0));My Hobby Project: LegacyClone
- 09-19-2009, 01:41 AM #3
Member
- Join Date
- Sep 2008
- Posts
- 25
- Rep Power
- 0
Thanks a lot! just what I needed - what do you call the <> syntax????
- 09-19-2009, 01:45 AM #4
Member
- Join Date
- Sep 2008
- Posts
- 25
- Rep Power
- 0
Oh I see this is "generic" thanks
- 09-21-2009, 11:39 PM #5
Member
- Join Date
- Sep 2008
- Posts
- 25
- Rep Power
- 0
How are HashMap values stored?
I have the following string input "a b c d" which I store in a hashmap with this code:
Later on I iterate through the HashMap to display the values/keys and this is what I get:Java Code:HashMap<Point, String> mapStrParts = new HashMap<Point, String>(); // loop through each of the string parts to save them in the HashMap for (int i=0; i<strParts.length; i++) { mapStrParts.put(new Point(0, i), strParts[i]); }
java.awt.Point[x=0,y=3]: d
java.awt.Point[x=0,y=0]: a
java.awt.Point[x=0,y=2]: c
java.awt.Point[x=0,y=1]: b
How exactly are teh values stored int he HashMap? The order I expected is:
java.awt.Point[x=0,y=0]: a
java.awt.Point[x=0,y=1]: b
java.awt.Point[x=0,y=2]: c
java.awt.Point[x=0,y=3]: d
any thoughts? Thank you
- 09-22-2009, 12:00 AM #6
it is stored by the hash value of the key object, which would be in this case the points "hashCode()".
Last edited by mrmatt1111; 09-22-2009 at 12:05 AM.
My Hobby Project: LegacyClone
- 09-22-2009, 09:07 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
There's no ordering with a HashMap either, so you really shouldn't expect any particular order back.
- 09-22-2009, 09:06 PM #8
Member
- Join Date
- Sep 2008
- Posts
- 25
- Rep Power
- 0
Thanks for your input
- 09-23-2009, 06:43 AM #9
Senior Member
- Join Date
- Dec 2008
- Location
- Hong Kong
- Posts
- 473
- Rep Power
- 5
if you wants to maintain order, use LinkedHashMap
Similar Threads
-
Please explain Java
By MarkWilson in forum New To JavaReplies: 7Last Post: 07-02-2008, 08:38 AM -
Need Help Can anyone explain what this means
By Clemenza1983 in forum New To JavaReplies: 6Last Post: 02-16-2008, 03:13 AM -
Can anyone briefy explain what does that mean?
By Clemenza1983 in forum New To JavaReplies: 6Last Post: 01-29-2008, 07:05 AM -
Iam new in Java Please explain to me
By vinaytvijayan in forum AWT / SwingReplies: 1Last Post: 12-30-2007, 11:35 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks