Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Collections - Set to prevent duplicating items
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Stefan  
View profile  
 More options Jul 12 2010, 2:38 pm
Newsgroups: comp.lang.java.programmer
From: Stefan <mstefa...@gmail.com>
Date: Mon, 12 Jul 2010 11:38:38 -0700 (PDT)
Local: Mon, Jul 12 2010 2:38 pm
Subject: Collections - Set to prevent duplicating items
Hello,
I guess my problem is "no-brainer" to some of you, but for now I fell
completely helpless. Here is an easiest example:

package test;
import java.util.*;

class Vertex {
 int number;

 public Vertex(int number) {
  this.number = number;
 }

 public String toString() {
  return number + "";
 }

 @Override
 public boolean equals(Object obj) {
  return this.number == ((Vertex) obj).number;
 }

}

public class SetTest {
 public static void main(String[] args) {
  Set vertices = new HashSet();

  Vertex a = new Vertex(2);
  Vertex b = new Vertex(3);
  Vertex c = new Vertex(3);

  System.out.println(b.equals(c));

  vertices.add(a);
  vertices.add(b);
  vertices.add(c);

  System.out.println(vertices);
 }

}

Console prints:
true (b equals c)
[3, 2, 3] (Vertex was added to set althought it equals another Vertex)

Some refertence:
http://java.sun.com/j2se/1.4.2/docs/api/java/util/HashSet.html#contai...)
http://java.sun.com/j2se/1.4.2/docs/api/java/util/Set.html

Thank you in advance


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Sosman  
View profile  
 More options Jul 12 2010, 2:48 pm
Newsgroups: comp.lang.java.programmer
From: Eric Sosman <esos...@ieee-dot-org.invalid>
Date: Mon, 12 Jul 2010 14:48:28 -0400
Local: Mon, Jul 12 2010 2:48 pm
Subject: Re: Collections - Set to prevent duplicating items
On 7/12/2010 2:38 PM, Stefan wrote:

     "I observed immediately that the malefactor had made one crucial
error in carrying out his fiendish plan: He forgot to override the
hashCode() method when overriding equals().  As any student of the art
of detection knows well, these two are inseparable: Override both, or
override neither, or invoke chaos upon yourself -- as many a resident
of Her Majesty's Gaols can testify tearfully."

     "Astounding, Holmes!"

     "Elementary, my dear Watson.  I also note that this perpetrator is
a particularly clumsy example of the species, having implemented an
equals() that fails miserably if given an argument that is `null', say,
or a reference to anything other than a `Vertex' instance.  Like so many
of the criminal underclass, he fails to consider the consequences of his
actions in a wider context than his immediate plot."

     "The criminal `underclass', Holmes?  Surely you meant `subclass'."

     "You're starting to get on my nerves, Watson.  Must I uncase my
violin again?"

     "I say, Holmes, I believe I'll go out for a bit of a stroll."

--
Eric Sosman
esos...@ieee-dot-org.invalid


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
markspace  
View profile  
 More options Jul 12 2010, 2:49 pm
Newsgroups: comp.lang.java.programmer
From: markspace <nos...@nowhere.com>
Date: Mon, 12 Jul 2010 11:49:53 -0700
Local: Mon, Jul 12 2010 2:49 pm
Subject: Re: Collections - Set to prevent duplicating items

Stefan wrote:
>   Set vertices = new HashSet();
> [3, 2, 3] (Vertex was added to set althought it equals another Vertex)

Yeah, hashes require that you override hashcode() when you override
equals(Object).  So what happened was your HashSet hasted to find the
"3" that was already there, didn't find it because the hashcode was
different, and added the second "3".

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Patricia Shanahan  
View profile  
 More options Jul 12 2010, 5:10 pm
Newsgroups: comp.lang.java.programmer
From: Patricia Shanahan <p...@acm.org>
Date: Mon, 12 Jul 2010 14:10:15 -0700
Local: Mon, Jul 12 2010 5:10 pm
Subject: Re: Collections - Set to prevent duplicating items

As has already been pointed out, you have inconsistent equals and
hashCode. More specifically, you are not conforming to their contract as
described in the Object documentation at e.g.
http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/l...

In general, when overriding a method inherited from a superclass you
should examine the superclass documentation to find the requirements for
the method. The equals documentation says "Note that it is generally
necessary to override the hashCode method whenever this method is
overridden, so as to maintain the general contract for the hashCode
method, which states that equal objects must have equal hash codes."

Patricia


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Stefan  
View profile  
 More options Jul 12 2010, 5:21 pm
Newsgroups: comp.lang.java.programmer
From: Stefan <mstefa...@gmail.com>
Date: Mon, 12 Jul 2010 14:21:54 -0700 (PDT)
Local: Mon, Jul 12 2010 5:21 pm
Subject: Re: Collections - Set to prevent duplicating items
Brilliant, Holmes, brilliant! :D

Thank you :)

On 12 Lip, 20:48, Eric Sosman <esos...@ieee-dot-org.invalid> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »