• Post Reply Bookmark Topic Watch Topic
  • New Topic

like and array that does not allow duplicates?  RSS feed

 
Erik Davies
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anything in java like an ArrayList that doesn't allow duplicates of entries?


Thank you,
Erik Davies
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
java.util.Set
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the Java™ Tutorials and you will find there are several set implementations. There is even one which will remember which order you inserted the first instance of something.

Beware: You must override the equals and hashCode methods correctly for most sets to work. If you enter mutable reference types and their hash codes change, you can put them into a set and not find them again. Even if they are still there.
 
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
Campbell Ritchie wrote:
Beware: You must override the equals and hashCode methods correctly for most sets to work. If you enter mutable reference types and their hash codes change, you can put them into a set and not find them again. Even if they are still there.

Might be getting off-topic, but can you expand on this? I rarely use Sets, but I just wrote an app that reads records from a file for database insertion, and I decided to read them into a Set to eliminate duplicates. I did not do anything with hashcode and equals. How should I do this and what are the ramifications if I don't?
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:
Campbell Ritchie wrote:
Beware: You must override the equals and hashCode methods correctly for most sets to work. If you enter mutable reference types and their hash codes change, you can put them into a set and not find them again. Even if they are still there.

Might be getting off-topic, but can you expand on this? I rarely use Sets, but I just wrote an app that reads records from a file for database insertion, and I decided to read them into a Set to eliminate duplicates. I did not do anything with hashcode and equals. How should I do this and what are the ramifications if I don't?

In your database you most likely have one or more columns that make up a unique key for a given row. As you load your row into an class object designed to hold the data for one row you'll need a unique key in your object based on the same fields. Then you need to override equals() and hashCode() to work based on that key.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:Might be getting off-topic, but can you expand on this? I rarely use Sets, but I just wrote an app that reads records from a file for database insertion, and I decided to read them into a Set to eliminate duplicates. I did not do anything with hashcode and equals. How should I do this and what are the ramifications if I don't?


By default, equals() works so that every instance is only equal to itself. This means that since reading your entries from a file or database likely produces a new instance every time, no two instances will be equal, and using a Set will be no different from using a List.

TreeSets and HashSets store elements based on the compareTo() and hashCode() methods respectively. If you store an element, and then change a property of the element, the compareTo() and hashCode() methods will likely return a different result, and the element will not be found, even though it still is in the Set, and still equal to itself.

This is no problem for values in a Map, but keys (including set elements) should never be mutable. Instead of storing your entities in a Set, store them in a List or in a Map using their ID (or any other unique key) as the map key.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!