• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Don't insert duplicate values

 
Mala Sharma
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I'm reading from a text file that looks like this:
001ASD034 | B21D43 | 04/30/2005
001CAN061 | D32J45 | 09/03/2004
001CAN061| D32J45 | 09/03/2004

I need to read the words in the line seperated by | which i did by:

i only need to store the unique lines. So, in the above case, i would only store the first and second line and leave out the third because it's same as the second. This the step, i'm stuck on. I was thinking to store the each word in a line in a string array and add that to the HashSet. But this would not work, as A new object of array class is created with every line so they will all appear different to the Hashset. Could someone please suggest how to approach this part of the problem. I'll be really grateful.

Thanks,
Mala
 
Mala Sharma
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think i know what to do. I'll try it tomorrow and ask for help if there's any problem. Thanks.

Mala
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you could create the Array for a line, and add the entire Array as an Object to your HashSet.

Mark
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can think of two options:

1) Use an ArrayList instead of an array. ArrayList overloads the equals() and hashCode() methods so that it will behave as you expect with HashSet.
2) Create a custom class to store the data in each line and overload equals() and hashCode() yourself.

I don't know for sure which is better. It mostly depends on what you need to do with the data after you read it from the file.

Layne
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic