Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Working with Collections and Maps

Posted on 2006-04-16
13
Medium Priority
?
182 Views
Last Modified: 2010-03-31
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.

0
Comment
Question by:zozig
  • 7
  • 3
  • 2
  • +1
13 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 16467102
>> 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).
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16467103
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 92

Expert Comment

by:objects
ID: 16467849
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
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16467915
>> 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.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16467958
Sorry if it was not clear.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16467964
>> 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
0
 
LVL 1

Author Comment

by:zozig
ID: 16474561
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.

0
 
LVL 92

Expert Comment

by:objects
ID: 16474577
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.
0
 
LVL 1

Author Comment

by:zozig
ID: 16474742
objects,  

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

0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 16474800
something  like:

/** 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;
    }
 
    public boolean equals(Object o) {
      myID other = (myID) o;
      return thisId.equals(other.thisId);
    }

    /** Get the ID value */
    public String getID() {
        return thisId;
    }
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16475095
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 ()
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month20 days, 15 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question