Newbie question: How to write a set?

M

mrstephengross

I have an object (Foo), and would like to create a set of Foo's.

(1) How should I implement Foo's 'equals()' function so that I can
ensure only unique instances of Foo are placed in the set?

(2) How should I create the actual set to store the Foo instances?

Thanks in advance,
--Steve
 
M

Mark Space

mrstephengross said:
I have an object (Foo), and would like to create a set of Foo's.

(1) How should I implement Foo's 'equals()' function so that I can
ensure only unique instances of Foo are placed in the set?

(2) How should I create the actual set to store the Foo instances?

Thanks in advance,
--Steve

My first reaction to this is "do your own homework."

If you have some specific question on equals(), we'd have to see the
definition of the class, the requirements of the class, and any other
pertinent information before giving you a good answer.

Creating a set of Foo: RTFM

<http://java.sun.com/docs/books/tutorial/collections/index.html>
 
T

Tom Anderson

class Foo {
public boolean equals(Object obj) {
// return true iff obj is a Foo
// that you consider "equal" to this one
}

public int hashCode() {
// return an integer whose value depends
// only on those aspects of a Foo that
// equals() takes into consideration
}
}

The important thing is to implement *both* equals() and
hashCode(), or to implement *neither* of them. It is
almost always a misteak to implement just one of the pair.

If the OP wants to use a TreeSet, he needs to implement
Comparable.compareTo, and hashCode doesn't matter. Although it would still
be prudent to implement it.

tom
 
R

Roedy Green

(1) How should I implement Foo's 'equals()' function so that I can
ensure only unique instances of Foo are placed in the set?

That cannot be answered in general. If Foos are people it might be
that they have the same SIN or SSI number. If Foos are gazelles it
might be they have the same sex, same weight, same antler length.
 
L

Lasse Reichstein Nielsen

mrstephengross said:
I have an object (Foo), and would like to create a set of Foo's.
Good.

(1) How should I implement Foo's 'equals()' function so that I can
ensure only unique instances of Foo are placed in the set?

When are two different Foo objects considered equal/not considered equal?
(2) How should I create the actual set to store the Foo instances?

new XXX<Foo>()

where XXX is the name of some Set implementation.

/L
 
T

Tom Anderson

Tom said:
[...]
The important thing is to implement *both* equals() and
hashCode(), or to implement *neither* of them. It is
almost always a misteak to implement just one of the pair.

If the OP wants to use a TreeSet, he needs to implement
Comparable.compareTo,

Right. (Alternatively, he could specify a Comparator when building
the TreeSet.) Good catch.

Ditto on the Comparator!
I'd still go beyond "prudent." If he implements Comparable it's a
very good idea to implement equals(); it's legal to have a natural order
that is inconsistent with equals(), but it's asking for trouble. And if
he implements equals() without implementing hashCode() (or vice versa),
he's not asking for trouble but begging for it.

Absolutely.

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top