• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Don't insert duplicate values

 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic