Results 1 to 9 of 9
  1. #1
    RR_QQ is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default 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!

  2. #2
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    9

    Default

    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

  3. #3
    RR_QQ is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default

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

  4. #4
    RR_QQ is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default

    Oh I see this is "generic" thanks

  5. #5
    RR_QQ is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default 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

  6. #6
    mrmatt1111's Avatar
    mrmatt1111 is offline Senior Member
    Join Date
    Aug 2009
    Location
    San Jose, CA, USA
    Posts
    320
    Rep Power
    9

    Default

    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 01:05 AM.
    My Hobby Project: LegacyClone

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,532
    Rep Power
    24

    Default

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

  8. #8
    RR_QQ is offline Member
    Join Date
    Sep 2008
    Posts
    25
    Rep Power
    0

    Default

    Thanks for your input

  9. #9
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    10

Similar Threads

  1. Please explain Java
    By MarkWilson in forum New To Java
    Replies: 7
    Last Post: 07-02-2008, 09:38 AM
  2. Need Help Can anyone explain what this means
    By Clemenza1983 in forum New To Java
    Replies: 6
    Last Post: 02-16-2008, 04:13 AM
  3. Can anyone briefy explain what does that mean?
    By Clemenza1983 in forum New To Java
    Replies: 6
    Last Post: 01-29-2008, 08:05 AM
  4. Iam new in Java Please explain to me
    By vinaytvijayan in forum AWT / Swing
    Replies: 1
    Last Post: 12-30-2007, 12:35 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
  •