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:

Java Code:
`HashMap<Point> h = new HashMap<Point>();`
I know how to store items in a HashMap like this:

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]);
}
}```
But how do I assign the Point x,y?

Any help is appreciated thank you!  Reply With Quote

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));```  Reply With Quote

3. Member Join Date
Sep 2008
Posts
25
Rep Power
0

## Thanks a lot! just what I needed - what do you call the <> syntax????  Reply With Quote

4. Member Join Date
Sep 2008
Posts
25
Rep Power
0

## Oh I see this is "generic" thanks  Reply With Quote

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:

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]);
}```
Later on I iterate through the HashMap to display the values/keys and this is what I get:

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  Reply With Quote

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.  Reply With Quote

7. Moderator   Join Date
Apr 2009
Posts
13,541
Rep Power
27

## There's no ordering with a HashMap either, so you really shouldn't expect any particular order back.  Reply With Quote

8. Member Join Date
Sep 2008
Posts
25
Rep Power
0

##   Reply With Quote

9. Senior Member Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
12

## if you wants to maintain order, use LinkedHashMap  Reply With Quote

#### Posting Permissions

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