zozig
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.
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.
Read the collections tutorial: http://java.sun.com/docs/books/tutorial/collections/index.html
http://www.java2s.com/Code/Java/Collections-Data-Structure/CatalogCollections-Data-Structure.htm
http://javaalmanac.com/egs/java.util/pkg.html
you can check out for examples here
http://javaalmanac.com/egs/java.util/pkg.html
you can check out for examples here
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
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.
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
Actually, he knows it :) >> and then be able to utilize the ID object in a map for efficient lookup by ID
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.
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.
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ()
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).