473,382 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

.equals(o) and Set membership

i have a class, in which i have override the Object.equals(Object o)
method... and then i try to make two equal objects (under the new
definition) and place them into a HashSet... which should only accept one
of them, as in the following code. the class holds two objects (a pair of
objects) and if another member of the class contains the same two objects
(in either place of the ordered pair), it is considered equal.

Integer key1 = new Integer(1);
Integer key2 = new Integer(2);
HashSet x = new HashSet();
// demonstrate that the equals() method works...
System.out.println((new MyClass(key1, key2)).equals(new myClass(key2,
key1)));
System.out.println(x.add(new MyClass(key1, key2)));
System.out.println(x.add(new MyClass(key2, key1)));
if i'm correct in reading the docs about how Set classes work, the output
of this should be:

true
true
false

but instead i'm getting:

true
true
true

and indeed the Set x has two elements after this code.

i thought the Set code checks for membership by applying the .equals()
method, which at runtime should bind to my overridden method, no?

thanks for any help,

murat
--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #1
4 6124
Murat Tasan wrote:

i thought the Set code checks for membership by applying the .equals()
method, which at runtime should bind to my overridden method, no?


No, that is not correct. Whenever equals() is overloaded, hashCode()
must be overloaded as well. The Collections classes depend on
hashCode() and equals(). Read the javadoc for java.lang.Object
carefully and implement hashCode() for your classes for which you have
overridden equals() and you will see better results.

HTH,
Ray

Jul 17 '05 #2
Murat Tasan <ta***@eecs.cwru.edu> wrote in message news:<Pine.SOL.4.53.0312041520470.18529@homer>...
i have a class, in which i have override the Object.equals(Object o)
method... and then i try to make two equal objects (under the new
definition) and place them into a HashSet... which should only accept one
of them, as in the following code. the class holds two objects (a pair of
objects) and if another member of the class contains the same two objects
(in either place of the ordered pair), it is considered equal.

Integer key1 = new Integer(1);
Integer key2 = new Integer(2);
HashSet x = new HashSet();
// demonstrate that the equals() method works...
System.out.println((new MyClass(key1, key2)).equals(new myClass(key2,
key1)));
System.out.println(x.add(new MyClass(key1, key2)));
System.out.println(x.add(new MyClass(key2, key1)));
if i'm correct in reading the docs about how Set classes work, the output
of this should be:

true
true
false

but instead i'm getting:

true
true
true

and indeed the Set x has two elements after this code.

i thought the Set code checks for membership by applying the .equals()
method, which at runtime should bind to my overridden method, no?

thanks for any help,

murat


My guess is: your equals() method is not equal to the Object#equals()
method in its signature. I also made the error in my Java beginner
days, numerous times may be!
Jul 17 '05 #3
Murat Tasan <ta***@eecs.cwru.edu> wrote in message news:<Pine.SOL.4.53.0312041520470.18529@homer>...
i have a class, in which i have override the Object.equals(Object o)
method... and then i try to make two equal objects (under the new
definition) and place them into a HashSet... which should only accept one
of them, as in the following code. the class holds two objects (a pair of
objects) and if another member of the class contains the same two objects
(in either place of the ordered pair), it is considered equal.

Integer key1 = new Integer(1);
Integer key2 = new Integer(2);
HashSet x = new HashSet();
// demonstrate that the equals() method works...
System.out.println((new MyClass(key1, key2)).equals(new myClass(key2,
key1)));
System.out.println(x.add(new MyClass(key1, key2)));
System.out.println(x.add(new MyClass(key2, key1)));
if i'm correct in reading the docs about how Set classes work, the output
of this should be:

true
true
false

but instead i'm getting:

true
true
true

and indeed the Set x has two elements after this code.

i thought the Set code checks for membership by applying the .equals()
method, which at runtime should bind to my overridden method, no?

thanks for any help,

murat


Sorry, my previous reply is wrong. Java.util.HashXxx data structure
classes check hashCode() of the added/stored objects. See javadoc for
Object#hashCode() and source files HashSet.java and HashMap.java. The
former depends on the latter in logic reuse.
Jul 17 '05 #4
thanks much, i totally forgot about that!

murat

On Fri, 5 Dec 2003, Raymond DeCampo wrote:
Murat Tasan wrote:

i thought the Set code checks for membership by applying the .equals()
method, which at runtime should bind to my overridden method, no?


No, that is not correct. Whenever equals() is overloaded, hashCode()
must be overloaded as well. The Collections classes depend on
hashCode() and equals(). Read the javadoc for java.lang.Object
carefully and implement hashCode() for your classes for which you have
overridden equals() and you will see better results.

HTH,
Ray


--
Murat Tasan
mx**@po.cwru.edu
ta***@eecs.cwru.edu
mu*********@cwru.edu
http://genomics.cwru.edu

Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Paul Keegstra | last post by:
Hi, I am currently working on an asp.net 2.0 web site that is a replacement of a classic asp web site. The current web site uses a Commerce Server 2002 database for storing user information. ...
4
by: Pony Tsui | last post by:
I was install the starter kits CLUB, and created a CLUB WEB SITE, this application use the MemberInfo table in club.mdf to store the membership'data, but i can not find out where to define or...
2
by: Balaji | last post by:
Hi All, Can I use more than one membership provider for a given website? I understand only one of them could be default one. If yes, then how to programmatically access the other membership...
3
by: ryan.mclean | last post by:
Hello everyone, I am wondering, can the membership provider be changed at runtime? Perhaps the connectionStringName? I would like to use a different database based on the server the site is...
4
by: =?Utf-8?B?Q2hyaXMgQ2Fw?= | last post by:
I have been having some trouble with implementing a custom Membership Provider. We have a custom data store and business logic that pulls user information. I need some level of functionality...
3
by: Glenn | last post by:
My current classic-ASP site has users, projects, roles and the 2.0 membership looks like a perfect fit, but I'm having trouble finding examples of how to have users that belong to different...
1
by: =?Utf-8?B?ZVByaW50?= | last post by:
Asp.Net v2.0 I have created a web application and I am using it from a single website and database. The web application has different ‘portals’ – each independent and I am using the...
1
by: =?Utf-8?B?ZVByaW50?= | last post by:
Asp.Net v2.0 I have created a web application and I am using it from a single website and database. The web application has different ‘portals’ – each independent and I am using the...
8
by: Nick | last post by:
Hi there, Membership.GetNumberOfUsersOnline() works great the first time, then jumps up to the number of users registered in the system. I have tried enumerating through each user individually...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.