Link to home
Start Free TrialLog in
Avatar of zozig
zozigFlag for United States of America

asked on

Working with Collections and Maps

Hi All,

I'm trying to learn a bit more about using collections and maps.  What I'd like to do is be able to utilize an ID object that would allow me to query a collections using Collection.contains() and then be able to utilize the ID object in a map for efficient lookup by ID.  If my ID object looked like the one below would I encounter any issues?  Is it ok to use Strings for my ID?

/** An ID is just a String */
public class myID {
    /** The ID value */
    private String thisId;
 
    /** Construct an ID given its String value */
    public myID(String id) {
        if (id == null)
            throw new NullPointerException();
 
        thisId = id;
    }
 
    /** Get the ID value */
    public String getID() {
        return thisId;
    }
}

Thanks.

Avatar of Mayank S
Mayank S
Flag of India image

>> Is it ok to use Strings for my ID?

Yes. Depends on your case, actually but by default the one most preferred would be a String if your ID can hold alphanumeric data.

>> throw new NullPointerException();

Might be better to throw some other exception (like BadDataException - some class which you make).
you can just use the String class directly, using your own id class as you have posted does not gain you much.

Also the contains() method looks for objects in the collection that match the specified object according to its equals()( method. So it would not look for object that had that id, but *were that id if you get my differencve,
A Map is better for doing lookups by object id
>> Yes. Depends on your case, actually but by default the one most preferred would be a String if your ID can hold alphanumeric data

What I meant to say here was that a String should be used directly for an ID, unless you plan to add extra fields to your class and use it as the ID.
Sorry if it was not clear.
>> A Map is better for doing lookups by object id

Actually, he knows it :) >> and then be able to utilize the ID object in a map for efficient lookup by ID
Avatar of zozig

ASKER

objects,

Can you please explain a bit more what you mean by the following:

>>Also the contains() method looks for objects in the collection that match the specified object according to its equals()( method. So it would not look for object that had that id, but *were that id if you get my differencve>>

Do you mean that I would that the contains() method would not return true if I passed it an ID object that I am looking for that is NOT the exact same object I placed into the collection?  Does the contains() method search objects by value or reference?  Thanks.

I mean if you Collection contained myID objects them it would find it (once you implements equals()), but not if youy collection contained objects of some other class that had id as a property.
Avatar of zozig

ASKER

objects,  

So what would I need to do to implement the equals() method?  Would it be similar the equals implemented by the String object?

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
BTW, if you use this object as a key in a hash-map or a hash-table, you also have to over-ride hashCode () method along with equals ()