From: Lew on
Tom Anderson wrote:
> if ((obj == null) || !(obj instanceof SearchResultHotels)) return false;

The '(obj == null)' clause is superfluous.

--
Lew
From: Lew on
laredotornado wrote:
>>> I never see my log statement printed out when I call "map.put". What
>>> am I missing?

Michal Kleczek wrote:
>> Probably int hashCode() (if a hashing map implementation is used). Or
>> appropriate Comparable/Comparator implementation if you use a tree based map.

laredotornado wrote:
>> --
>> Michal

Don't quote sigs.

> When I defined a hashCode method, then everything was fine -- the
> HashMap object did not allow me to insert two keys that I considered
> the same.
>
> Per the other points about ClassCastExceptions in the equals method, I
> have implemented your suggestions.

Always override 'equals()', 'hashCode()' and 'toString()' together or not at
all; keep them consistent with each other. If 'Foo' implements
'Comparable<Foo>' then it must override those three, all consistent with
'compareTo(Foo)'.

--
Lew
From: Arne Vajhøj on
On 31-05-2010 10:49, Lew wrote:
> laredotornado wrote:
>>>> I never see my log statement printed out when I call "map.put". What
>>>> am I missing?
>
> Michal Kleczek wrote:
>>> Probably int hashCode() (if a hashing map implementation is used). Or
>>> appropriate Comparable/Comparator implementation if you use a tree
>>> based map.
>
> laredotornado wrote:
>>> --
>>> Michal
>
> Don't quote sigs.
>
>> When I defined a hashCode method, then everything was fine -- the
>> HashMap object did not allow me to insert two keys that I considered
>> the same.
>>
>> Per the other points about ClassCastExceptions in the equals method, I
>> have implemented your suggestions.
>
> Always override 'equals()', 'hashCode()' and 'toString()' together or
> not at all; keep them consistent with each other. If 'Foo' implements
> 'Comparable<Foo>' then it must override those three, all consistent with
> 'compareTo(Foo)'.

There are very good reasons to override equals and hashCode together.

I can see many cases where overriding toString alone makes sense,
because its main purpose is to make developer logging easier. And
often you will want to log something without it having to be
comparable.

Arne

From: Michael Jung on
laredotornado <laredotornado(a)zipmail.com> writes:
> public boolean equals(Object obj) {
> SearchResultHotels hotel = (SearchResultHotels) obj;
> boolean ret = obj != null && ((SearchResultHotels) obj).getId() ==
> getId();
> log.debug("\t comparing " + hotel.getName() + " to " + getName() +
> ":" + ret);
> return ret;
> }
> I never see my log statement printed out when I call "map.put". What
> am I missing?

Did you override the hashCode() method?

Michael
From: Tom Anderson on
On Mon, 31 May 2010, Lew wrote:

> Tom Anderson wrote:
>> if ((obj == null) || !(obj instanceof SearchResultHotels)) return false;
>
> The '(obj == null)' clause is superfluous.

Good point.

My brain, for some reason, has a hard time remembering that:

SearchResultHotels x = (SearchResultHotels)null;

is okay but:

null instanceof SearchResultHotels

is false.

tom

--
YOU CANT TAKE AWAY HATGUYS HAT. THEN HE IS JUST GUY -- The_Toad