How do I return a value in a HashSet?

I need to return a particular value in a HashSet. How do I do this?
[74 byte] By [sunlordclavea] at [2007-11-27 5:02:56]
# 1
If you mean what I think you mean, you can't do it easily. You'd have to construct and equal object and then iterate until equals returns true.A simpler way is just to have a Map where the key and value are the same.
jverda at 2007-7-12 10:20:58 > top of Java-index,Java Essentials,Java Programming...
# 2

I need to write a method for this:

** returns the customer with the specified id, or null if the

* customer is not registered at the branch

* @param id the customer's number

* @return the customer is registered with the branch,or null

**/

allCustomers is the HashSet of customers. So I need to return the customer in the HashSet that has the id specified in the parameter....

sunlordclavea at 2007-7-12 10:20:58 > top of Java-index,Java Essentials,Java Programming...
# 3
Sounds like you want a map where the key is ID and the value is Customer.If you insist on using a Set rather than a Map, you'll have to iterate and test each ID until you find the one you want.
jverda at 2007-7-12 10:20:58 > top of Java-index,Java Essentials,Java Programming...
# 4

Its not something I want! Its a question on my University coursework. But yeah it sounds like HashMap would have done a better job, don't know why they chose HashSet.

Would this work:

public Customer getCustomer(int cNo)

{

for(Customer c: allCustomers){

if(c.getCustomerNo()==cNo){

return c;

}

}

return null;

}

sunlordclavea at 2007-7-12 10:20:58 > top of Java-index,Java Essentials,Java Programming...
# 5
> Would this work:Test it and see.
jverda at 2007-7-12 10:20:58 > top of Java-index,Java Essentials,Java Programming...
# 6
Its a written thing. They don't give us the program, we have to write the code we think will work on paper.
sunlordclavea at 2007-7-12 10:20:58 > top of Java-index,Java Essentials,Java Programming...
# 7
> Its a written thing. They don't give us the program,> we have to write the code we think will work on paper.If it's homework, then you should have a compiler available to you to test it out. If it's a test, then it would be wrong for me to answer it for you.
jverda at 2007-7-12 10:20:58 > top of Java-index,Java Essentials,Java Programming...