Results 1 to 5 of 5
  1. #1
    Crakrjack is offline Member
    Join Date
    Sep 2013
    Posts
    20
    Rep Power
    0

    Default Iterating through hashset

    I'm trying to iterate through a hash set using the following code:

    Java Code:
    Iterator<Place> itr = m.getPlaces().iterator();
        	while(itr.hasNext())
        	{
        		System.out.println("place "+itr.next().getName()+" "+itr.next().getX()+" "+itr.next().getY()+"\n");
        	}
    getPlaces() returns a set of Place objects that are part of map "m" getName(), getX() and getY() are all functions in the Place interface.
    I've used something very similar before but now I'm getting the following error:

    Java Code:
    Exception in thread "main" java.util.NoSuchElementException
    	at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
    	at java.util.HashMap$KeyIterator.next(Unknown Source)
    	at assign2.MapReaderWriter.write(MapReaderWriter.java:31)
    	at assign2.Driver.main(Driver.java:47)
    It does print out the first line as expected, then the error.

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default Re: Iterating through hashset

    In the print line you call next 3 times but only check hasNext once. What happens if there is not a multiple of 3 in the HashSet?

  3. #3
    Crakrjack is offline Member
    Join Date
    Sep 2013
    Posts
    20
    Rep Power
    0

    Default Re: Iterating through hashset

    Ahh, ok, so each time I call itr.next(), it's Incrementing the iterator. I actually need call the three functions on the same Place object pointed to by itr, then increment it.
    How do I make the subsequent function calls without incrementing? Perhaps an iterator isn't ideal?

  4. #4
    Crakrjack is offline Member
    Join Date
    Sep 2013
    Posts
    20
    Rep Power
    0

    Default Re: Iterating through hashset

    Never mind, Ive sorted it. My code is now.

    Java Code:
    for(Place p: m.getPlaces())
    		str+= "place "+p.getName()+" "+p.getX()+" "+p.getY()+"\n";

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Iterating through hashset

    Just assign the instance returned by itr.next() to a type Place and use that to access the fields of the object.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. HashSet
    By Dayanand in forum New To Java
    Replies: 7
    Last Post: 03-12-2011, 09:37 AM
  2. Getting value for key in HashSet
    By Venny in forum New To Java
    Replies: 12
    Last Post: 02-02-2011, 08:42 AM
  3. :( anyone here plz help on HashSet
    By waklo99 in forum New To Java
    Replies: 8
    Last Post: 09-20-2010, 03:02 AM
  4. HashSet anomaly
    By jon80 in forum New To Java
    Replies: 1
    Last Post: 06-21-2009, 08:22 PM
  5. Iterating through a HashSet
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-21-2008, 04:34 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
  •