Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-02-2007, 10:44 AM
Member
 
Join Date: Aug 2007
Posts: 3
haridharna is on a distinguished road
how does the remove method work for sets and hashsets
Hello,
I am hari a resident of india.
I have been to this form many times. but this is my first posting.

I have a little confusion with the remove method of Set and HashSet.
Can any one tell me how does this method work exactly.
See the code and the result given below. I am confused aboud when the hashcode and the equals methods will be called.

Code:
import java.util.*; class KeyMaster { public int i; public KeyMaster(int i) { this.i = i; } public boolean equals(Object o) { System.out.println("equal:"+ (i == ((KeyMaster) o).i)); return i == ((KeyMaster) o).i; } public int hashCode() { System.out.println("Hashcode:"+i); return i; } public String toString() { return ""+i; } } public class MapIt { public static void main(String[] args) { Set<KeyMaster> set = new HashSet<KeyMaster>(); KeyMaster k1 = new KeyMaster(1); KeyMaster k2 = new KeyMaster(2); set.add(k1); set.add(k2); k1.i=2; set.remove(k1); System.out.println(set.contains(k2)); System.out.println(set); } }
The ouput for the above code is :
Hashcode:1
Hashcode:2
Hashcode:2
equal:true
Hashcode:2
false
[2]

Code:
public class MapIt { public static void main(String[] args) { Set<KeyMaster> set = new HashSet<KeyMaster>(); KeyMaster k1 = new KeyMaster(1); KeyMaster k2 = new KeyMaster(2); set.add(k1); set.add(k2); // k1.i=2; set.remove(k1); System.out.println(set.contains(k2)); System.out.println(set); } }
if we comment the k1.i=2 the following is the output.
Hashcode:1
Hashcode:2
Hashcode:1
Hashcode:2
true
[2]
---------------

Code:
public class MapIt { public static void main(String[] args) { Set<KeyMaster> set = new HashSet<KeyMaster>(); KeyMaster k1 = new KeyMaster(1); KeyMaster k2 = new KeyMaster(2); set.add(k1); set.add(k2); k1.i=2; set.remove(k1); System.out.println(set.contains(k1)); System.out.println(set.contains(k2)); System.out.println(set); } }
for the above code the following is the output:

Hashcode:1
Hashcode:2
Hashcode:2
equal:true
Hashcode:2
false
Hashcode:2
false
[2]

I am confused with when the equals method will be called and why the contains method is giving false.

Last edited by levent : 08-02-2007 at 11:11 AM. Reason: Codes are placed inside [code] tag.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-02-2007, 02:46 PM
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
A little intro to Hash tables: When inserting an object into a hashtable the hash code is retrieved from that object. Based on the hash code the object is placed in one of several buckets. (the number of buckets varies) The bucket is just a linked list of objects. Now when trying to find an object the hash code is used to locate the bucket and then the equals is called for each object in the bucket to find the exact match. So think of hash code as fuzzy match and equals() as exact match.

One thing you have done here is changed the hash code for an object. That should not be done once the object is placed within the hash table. The reason is you just change what bucket the object should be in without actually moving the object.

Also your equals is comparing the same as your hash code. This will not work well. Remember all objects in the same bucket have the same hash code and you can have multiple objects in a hash table that have the same hash code. The .equals() method is supposed to be able to tell them apart. In some of your examples you set k1 and k2 to have the same i value. Now you cannot tell them apart.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-03-2007, 01:34 PM
Member
 
Join Date: Aug 2007
Posts: 3
haridharna is on a distinguished road
Brainhks thanks for the reply.
But I still have some doubts.
1)-- In the above code, equals method is called sometimes and sometimes it is not called. Can you or any one tell the reason for that.
2)-- When I set K2.i =1 the equals method is called and when I set k2.i=2 or with some other number the equals method is not called.
3)-- When i call the contains method after removing k2 both set.contains(k2) and set.contains(k1) are returning false. why is it so even when there is a value in the hashset.
4)-- Is the result same for the Hashtable and HashSet both same.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-03-2007, 03:05 PM
Senior Member
 
Join Date: Jul 2007
Posts: 134
brianhks will become famous soon enough
Hashtable, HashSet, HashMap; they all use a hash to store the objects.

Code:
k1.i=2;
Just so you understand, the above line of code is evil. You are changing the hash value of an object that has already been placed in the Set. If you place an object in a HashSet and then change its hash code the object will get lost. Because later when you try to remove it the HashSet will go looking in the wrong bucket for the object and not find it.

Equals is only called if the bucket into which the object is being placed already contains an object. Now as for why equals is called intermittently. My suspicion is that when comparing objects they first use the '=' to see if they are in fact the same object before calling .equals(). Notice that the equals is only called when you change the value of k1 to equal k2 and then try to remove k1. Because k1 has the value of k2 it will remove k2 from the Set but k1 = k2 is not true so it will call k1.equals(k2).
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-06-2007, 01:48 PM
Member
 
Join Date: Aug 2007
Posts: 3
haridharna is on a distinguished road
Thank you for the reply Brianhks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Printing the union and instersection of two sets Java Tip java.lang 0 04-15-2008 08:35 PM
No duplicates allowed in Sets Java Tip Java Tips 0 01-21-2008 05:33 PM
Java Collection Framework (Sets) Java Tutorial Java Tutorials 0 12-07-2007 08:50 PM
how to remove whitespaces in a text christina New To Java 2 08-03-2007 06:24 PM
how to remove an old version of JDK tommy New To Java 2 07-30-2007 09:59 AM


All times are GMT +3. The time now is 02:48 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org