This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

HashSet with the same elements?  RSS feed

 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fill JComboBoxes with Customers I read from the database. I use a HashSet because "It will store unique values." However, looking at my logging I see the same customer twice.

First, proof of concept that a HashSet stores unique elements:
This results in:

Now my code

Now the logging:

And the database:


Can someone explain it to me?
 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daan Heuvelbeuk wrote:Now my code


I see I even first check that the customer name not exists in the HashSet before adding it to the HashSet. But trust me, directly adding customers without the check results in the same result.

 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The important code to show is probably the equals() and hashCode() methods in the customer class. That is the commonest location for such errors.
 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The important code to show is probably the equals() and hashCode() methods in the customer class. That is the commonest location for such errors.


You mean in the HashSet code?
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"ACME inc." is not equal to "ACME Inc."
 
Daan Heuvelbeuk
Ranch Hand
Posts: 85
MySQL Database Netscape Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:"ACME inc." is not equal to "ACME Inc."


 
J. Kevin Robbins
Bartender
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daan Heuvelbeuk wrote:
Paweł Baczyński wrote:"ACME inc." is not equal to "ACME Inc."



Welcome to the club. You just earned your FacePalm merit badge.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daan Heuvelbeuk wrote:. . . You mean in the HashSet code?
No, I mean in the Customer class. It should be possible to return inc. the same as Inc.

Don't worry; we have all done that sort of thing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!