• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

HashSet with the same elements?

 
Ranch Hand
Posts: 89
Netscape MySQL Database Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 89
Netscape MySQL Database Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 76861
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 89
Netscape MySQL Database Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"ACME inc." is not equal to "ACME Inc."
 
Daan Heuvelbeuk
Ranch Hand
Posts: 89
Netscape MySQL Database Windows XP
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paweł Baczyński wrote:"ACME inc." is not equal to "ACME Inc."



 
Bartender
Posts: 1810
28
jQuery Netbeans IDE Eclipse IDE Firefox Browser MySQL Database Chrome Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76861
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Yes, my master! Here is the tiny ad you asked for:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic