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

    Default Question regarding hash sets.

    I'm fairly new to Java and very new to the use of hash sets.
    I need to write a method that creates a new object of type "Place", adds it to a hash set and returns Place.
    My code as is:
    Java Code:
    public Place newPlace(String placeName, int xPos, int yPos)
    			throws IllegalArgumentException {
    		places.add(new PlaceImpl(placeName, xPos, yPos));
    		
    		return places("placeName");
    	}
    places is the hashset.
    All is working apart from the return, I'm really not sure how to return the Place? Any hints greatly appreciated.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Question regarding hash sets.

    First, you want Place to be returned but your adding PlaceImpl. So I presume that Place is an interface or class which is either implemented or extended by PlaceImpl.

    Why not just create an instance of Place before you add it to the HashSet? Then once added, return the created instance.

    Note: Unless Place is immutable, if you change any part of it, the object in the HashSet will also be changed.

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

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

    Default Re: Question regarding hash sets.

    Yeah, your correct. Place is an interface, I should mention that I'm new to those as well. I've only used classes with constructors and implemented methods.

    I've changed my code to:
    Java Code:
    public Place newPlace(String placeName, int xPos, int yPos)
    			throws IllegalArgumentException {
    		Place p = new PlaceImpl(placeName, xPos, yPos);
    		places.add(p);
    		
    		return p;
    	}

    I also need to implment the method

    Java Code:
    public void deletePlace(Place s) {
    		// TODO Auto-generated method stub
    		places.remove(s);
    		
    	}
    So when I create the Place initially, it needs to be identifiable by the placeName, I can't simply call it placeName as it creates duplicate variables.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Question regarding hash sets.

    Your code is just like I suggested. Does that work for you? Are you still having problems?

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

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

    Default Re: Question regarding hash sets.

    It compiles just fine, but given my driver code:

    Java Code:
    public class Driver
    {
        public static void main(String[] args) 
        {
        	Map m= new MapImpl();
        	
        	m.newPlace("Adelaide", 200, 200);
        	m.deletePlace(Adelaide);
        }
        
    }
    I am able to create a newPlace "Adelaide" but not sure how to delete it given the place is not identifiable by the string "Adelaide"

    Java Code:
    Set<Place> places = new HashSet<Place>();
    	Set<Road> roads = new HashSet<Road>();
    Are declared in MapImpl.java.

    To paint more of a picture, here is the Map interface
    Java Code:
    public interface Map
    {
        //Add the MapListener ml to this map.
        //Note: A map can have multiple listeners
        public void addListener(MapListener ml);
    
    
        //Delete the MapListener ml from this map.
        public void deleteListener(MapListener ml);
    
    
        //Create a new Place and add it to this map
        //Return the new place
        //Throws IllegalArgumentException if:
        //  the name is not valid or is the same as that
        //  of an existing place
        //Note: A valid placeName begins with a letter, and is 
        //followed by optional letters, digits, or underscore characters
        public Place newPlace(String placeName, int xPos, int yPos)
          throws IllegalArgumentException;
    
    
        //Remove a place from the map
        //If the place does not exist, returns without error
        public void deletePlace(Place s);
    
    
        //Find and return the Place with the given name
        //If no place exists with given name, return NULL
        public Place findPlace(String placeName);
    
    
        //Return a set containing all the places in this map
        public Set<Place> getPlaces();
        
    
        //Create a new Road and add it to this map
        //Returns the new road.
        //Throws IllegalArgumentException if:
        //  the firstPlace or secondPlace does not exist or
        //  the roadName is invalid or
        //  the length is negative
        //Note: A valid roadName is either the empty string, or starts
        //with a letter and is followed by optional letters and digits
        public Road newRoad(Place from, Place to, 
          String roadName, int length) 
          throws IllegalArgumentException;
    
    
        //Remove a road r from the map
        //If the road does not exist, returns without error
        public void deleteRoad(Road r);
    
    
        //Return a set containing all the roads in this map
        public Set<Road> getRoads();
        
    
        //Set the place p as the starting place
        //If p==null, unsets the starting place
        //Throws IllegalArgumentException if the place p is not in the map
        public void setStartPlace(Place p)
          throws IllegalArgumentException;
    
    
        //Return the starting place of this map
        public Place getStartPlace();
    
    
        //Set the place p as the ending place
        //If p==null, unsets the ending place
        //Throws IllegalArgumentException if the place p is not in the map
        public void setEndPlace(Place p)
          throws IllegalArgumentException;
    
    
        //Return the ending place of this map
        public Place getEndPlace();
    
    
        //Causes the map to compute the shortest trip between the
        //"start" and "end" places
        //For each road on the shortest route, sets the "isChosen" property
        //to "true".
        //Returns the total distance of the trip.
        //Returns -1, if there is no route from start to end
        public int getTripDistance();
    
    
        //Return a string describing this map
        //Returns a string that contains (in this order):
        //for each place in the map, a line (terminated by \n)
        //  PLACE followed the toString result for that place
        //for each road in the map, a line (terminated by \n)
        //  ROAD followed the toString result for that road
        //if a starting place has been defined, a line containing
        //  START followed the name of the starting-place (terminated by \n)
        //if an ending place has been defined, a line containing
        //  END followed the name of the ending-place (terminated by \n)
        public String toString();
    }
    And the place interface:
    Java Code:
    import java.util.Set;
    
    public interface Place
    {
        //Add the PlaceListener pl to this place. 
        //Note: A place can have multiple listeners
        public void addListener(PlaceListener pl);
    
    
        //Delete the PlaceListener pl from this place.
        public void deleteListener(PlaceListener pl);
    
    
        //Return a set containing all roads that reach this place
        public Set<Road> toRoads();
    
    
        //Return the road from this place to dest, if it exists
        //Returns null, if it does not
        public Road roadTo(Place dest);
        
    
        //Move the position of this place 
        //by (dx,dy) from its current position
        public void moveBy(int dx, int dy);
        
    
        //Return the name of this place 
        public String getName();
        
    
        //Return the X position of this place
        public int getX();
        
    
        //Return the Y position of this place
        public int getY();
    
    
        //Return true if this place is the starting place for a trip
        public boolean isStartPlace();
    
    
        //Return true if this place is the ending place for a trip
        public boolean isEndPlace();
    
    
        //Return a string containing information about this place 
        //in the form (without the quotes, of course!) :
        //"placeName(xPos,yPos)"  
        public String toString();
    }
    I am not permitted to change either of these interfaces.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Question regarding hash sets.

    Let's say you add an object to a set. To delete that object you must specify the object you added. So you can't remove it by simply specifying its name because that is not how you added it. You have several possiblities.

    1. Don't use HashSets but use HashMaps. That way you can use the name of the object as a key to get the object.

    Java Code:
    Map<String, Place> map = new HashMap<>();
    Place p = new PlaceImpl(placeName, xPos, yPos);
    map.put(placeName, p); 
    
    // then later...
    Place pp = map.get(placeName);
    2. Use a combination of both. If you must use HashSet, then use HashMap to get the object you want to delete from the set.

    Java Code:
    Map<String, Place> map = new HashMap<>();
    Place p = new PlaceImpl(placeName, xPos, yPos);
    map.put(placeName, p);
    places.add(p);
    
    // then later...
    
    Place pp = map.get(placeName);
    places.remove(pp);

    The only other way I can think of is the brute force method. Iterate thru the HashSet, checking the name of each entry. When you find the one you want, delete it from the set.

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

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

    Default Re: Question regarding hash sets.

    I've implemented the second method you have shown, but still am not sure how to call deletePlace(Place s) from main() as it take a Place rather than a string.

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

    Default Re: Question regarding hash sets.

    Thanks a heap for your help Jim, I think Ive sorted this issue:

    my main function was
    Java Code:
    public class Driver
    {
        public static void main(String[] args)
        {
            Map m= new MapImpl();
             
            m.newPlace("Adelaide", 200, 200);
            m.deletePlace(Adelaide);
        }
         
    }
    I should have been assigning m.newPlace("Adelaide", 200, 200) to a place so I could reference it in the delete function.
    like so

    Java Code:
    public class Driver
    {
        public static void main(String[] args) 
        {
        	Map m= new MapImpl();
        	
        	Place Adelaide =m.newPlace("Adelaide", 200, 200);
        	m.deletePlace(Adelaide);
        	}
        
    }
    Thanks again.

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Question regarding hash sets.

    Honestly I am not certain what you are doing with the Map. Where is the method newPlace coming from? Is that your own implementation of the Map? My whole point was to store the object in a map using its assigned name so you could retrieve it by that name and then delete it from the set. If you already have the object, then just delete it directly.

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

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Question regarding hash sets.

    See post 5.
    They have a Map interface, which is (considering it adds Places) representative of a geographical map.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,036
    Rep Power
    6

    Default Re: Question regarding hash sets.

    Thanks! I thought it was the Map interface we all know and love.

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

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Question regarding hash sets.

    Quote Originally Posted by jim829 View Post
    Thanks! I thought it was the Map interface we all know and love.

    Regards,
    Jim
    All the more reason to check if a name you pick already exists in the standard JDK :) I would probably have called it "GeographicMap" or something similar.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Sets Question
    By Malv in forum New To Java
    Replies: 3
    Last Post: 07-22-2013, 08:15 PM
  2. Replies: 9
    Last Post: 11-12-2012, 12:38 PM
  3. Maps and Sets
    By RedKMan in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 10:36 AM
  4. Duplicates in more than two sets
    By JavaJ in forum New To Java
    Replies: 8
    Last Post: 12-03-2009, 05:07 PM
  5. Question about hash tables
    By behrk2 in forum New To Java
    Replies: 2
    Last Post: 07-08-2008, 05:40 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
  •