Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to prevent duplicate objects in HashMap???

Reply
Thread Tools

how to prevent duplicate objects in HashMap???

 
 
pembed2003
Guest
Posts: n/a
 
      04-02-2004
Hi all,
I have been trying to do something really simple but I could find any
solutions to it! I have the following:

class P{

int id;

P(int i){id = i;}

public int hashCode(){return id;}

public boolean equals(P o){return id = o.id;}

public static void main(String[] args){
HashMap m = new HashMap();
m.put(new P(1),new Integer(1));
m.put(new P(1),new Integer(2));
Set k = m.entrySet();
Iterator i = k.iterator();
while(i.hasNext()){
System.out.println(i.next());
}
}
}

The program prints out 2 lines which I am not sure I understand. My P
class has a hashCode method which simply returns the id of the object.
I put 2 'new P(1)' instance into m but why doesn't HashMap reject the
second one or overwrite the first one becasue they have the same hash
code??? I though the purpose of hashCode is to let you reject or
detect dup objects, no?

In generaly, I found it difficult to do something like:

//
// insert a bunch of objects into a HashMap
//

later:

//
// how to check if HashMap has a P object with id 1???
//

this:

if(m.containsKey(new P(1)){
}

ALWAYS return false even I have a lot of P objects with id 1! How do I
solve that? Thanks!
 
Reply With Quote
 
 
 
 
Steve W. Jackson
Guest
Posts: n/a
 
      04-02-2004
In article < >,
(pembed2003) wrote:

>:Hi all,
>:I have been trying to do something really simple but I could find any
>:solutions to it! I have the following:
>:
>:class P{
>:
>: int id;
>:
>: P(int i){id = i;}
>:
>: public int hashCode(){return id;}
>:
>: public boolean equals(P o){return id = o.id;}
>:
>: public static void main(String[] args){
>: HashMap m = new HashMap();
>: m.put(new P(1),new Integer(1));
>: m.put(new P(1),new Integer(2));
>: Set k = m.entrySet();
>: Iterator i = k.iterator();
>: while(i.hasNext()){
>: System.out.println(i.next());
>: }
>: }
>:}
>:
>:The program prints out 2 lines which I am not sure I understand. My P
>:class has a hashCode method which simply returns the id of the object.
>:I put 2 'new P(1)' instance into m but why doesn't HashMap reject the
>:second one or overwrite the first one becasue they have the same hash
>:code??? I though the purpose of hashCode is to let you reject or
>:detect dup objects, no?
>:
>:In generaly, I found it difficult to do something like:
>:
>://
>:// insert a bunch of objects into a HashMap
>://
>:
>:later:
>:
>://
>:// how to check if HashMap has a P object with id 1???
>://
>:
>:this:
>:
>:if(m.containsKey(new P(1)){
>:}
>:
>:ALWAYS return false even I have a lot of P objects with id 1! How do I
>:solve that? Thanks!


I can't say with any certainty since I'm not expert at the bit shifting
aspects of Java (or any other language, for that matter). But the
source code for HashMap is included in your J2SDK and shows how
containsKey is implemented. It does not simply use hashCode(), but
instead starts with it and then applies what the comments call a
"supplemental hash function". The results of this action are then
looked up in the internal table of entries. The best guess I could make
is that the address in memory is somehow being brought into play, so
that each of these are indeed unique from the others.

= Steve =
--
Steve W. Jackson
Montgomery, Alabama
 
Reply With Quote
 
 
 
 
Marco Schmidt
Guest
Posts: n/a
 
      04-02-2004
pembed2003:

[...]

>ALWAYS return false even I have a lot of P objects with id 1! How do I
>solve that? Thanks!


(I'm just guessing Try to override equals(Object), not create
equals(P).

Regards,
Marco
--
Please reply in the newsgroup, not by email!
Java programming tips: http://jiu.sourceforge.net/javatips.html
Other Java pages: http://www.geocities.com/marcoschmidt.geo/java.html
 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      04-03-2004
pembed2003 wrote:

> The program prints out 2 lines which I am not sure I understand. My P
> class has a hashCode method which simply returns the id of the object.
> I put 2 'new P(1)' instance into m but why doesn't HashMap reject the
> second one or overwrite the first one becasue they have the same hash
> code??? I though the purpose of hashCode is to let you reject or
> detect dup objects, no?


That's not really true. The purpose of the hash code is as a *hint* to let the
implementation find object that are equal. It would be perfectly possible to
write a conforming implementation that didn't actually use the hash code at
all, but just looped through the collection trying equals() on everything.
(That implementation would be too slow for general use, but would probably be
more efficient for very small collections)

The real implementation does make use of the hash code and it depends crucially
on the assumptions that if two object have different hash codes then they are
not equal(), and that if they *are* equal then they have the same hash codes.

What it does *not* require is that two object with the same hash code are
equal(). In fact it is perfectly legal for *every* object to have the same
hash code (33, say). That would make hashed collections very inefficient, but
they'd still work correctly.

In your example code you *seem* to be correctly implementing hashCode() and
equals() to work together in the way that hashed collections require, but
actually there's a small bug Your definition of equals() takes a P as
parameter, and so it doesn't override the definition in Object (which takes an
Object parameter). Hence the HashMap is using your definition of hashCode(),
but the definition of equals() from Object (which uses identity comparison).
So it doesn't quite work...

-- chris


 
Reply With Quote
 
pembed2003
Guest
Posts: n/a
 
      04-04-2004
"Chris Uppal" <> wrote in message news:<>...

[snip]

>
> In your example code you *seem* to be correctly implementing hashCode() and
> equals() to work together in the way that hashed collections require, but
> actually there's a small bug Your definition of equals() takes a P as
> parameter, and so it doesn't override the definition in Object (which takes an
> Object parameter). Hence the HashMap is using your definition of hashCode(),
> but the definition of equals() from Object (which uses identity comparison).
> So it doesn't quite work...
>
> -- chris


Your explaination makes sense. I will correct the bug and give it
another try. Thanks Chris!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to prevent storing duplicate values dotpro2008 ASP .Net 2 02-15-2008 10:32 PM
How to prevent duplicate title attribute in calendar control hummh ASP .Net 0 08-09-2007 10:31 AM
Newbie - Need to prevent duplicate entries.. YMPN ASP .Net 3 01-14-2007 05:57 PM
Prevent duplicate entry to MS Access database mmazid@googlemail.com ASP .Net 0 02-08-2006 08:40 PM
How to prevent duplicate posting on a form w/ refresh? D. Shane Fowlkes ASP .Net 3 03-10-2005 09:05 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57