ReversedBrain
ReversedBrain

Reputation: 3

How to correctly pass/add Strings from HashSet<String> in another, HashSet<String>?

I have the following problem:

I am parsing a column from a voluminous tab separated values files ("original file") in a hashSet depending on various parameters. I want to parse it once, and write it as a simplified file ("parsing result") where I will not need to re split/filter everything everytimes, but just have to read the "parsing result" file then built a second hashSet retrievedHS, as long as I launch the program with the right parameter.

When I checked that result where the same, I had a strange behavior. when I read things in a third file (phonebook) and try to check the content of the lines of this file for containing a name I know to be present in the original file and thus in originalHS, (originalHS.contains(knownName) is true, but retrievedHS.contains(knownName) is false while it is technically the same.

I tried again to make this question as clear and the code as simplified as I could,

Thanks for any help


    HashSet<String> originalHS =originalParser(Original.txt)
    //method that parse a voluminous original.txt file (a tsv file) retrieving the first column based upon //other criterias from the other columns.

    System.out.println ("Debug: Display name collection: "+originalHS.toString());
                    //Debug: Display name collection: [Smith, Johnson, Bates]

    String name="Smith";

    if(originalHS.contains(name)){ System.out.print("true")
      else { System.out.print("false");

    //test for presence of name from a third file in this set
    //executes the code as it is true.

    String recorder_txt=//my storage file path
    PrintWriter writer = new PrintWriter(Recorder_txt);
    String recordedNames = originalHS.toString();
    System.out.print("Writing recordedAccessions "+recordedNames);
    //Debug: Display Writing recordedAccessions [Smith, Johnson, Bates]

    writer.println (recordedNames);

    HashSet <String> retrievedHS =new HashSet <String>();

    HashSet <String> returnedHS= retrieve(Recorder_txt)     

//made in another class in my own code, see method code below //method that parse the HashSet written from original HS by writer in Recorder_txt // It opens the file, read the line [name1,name2,...], suppresses [], split the line, load //names in the HashSet

        retrievedHS=returnedHS
    //or retrievedHS.addAll(returnedHS) 

    if(retrievedHS.contains(name)){ System.out.print("true");} 
    else { System.out.print("false");} 
    //DOES NOT WORK; it always returns false

Upvotes: 0

Views: 441

Answers (1)

JB Nizet
JB Nizet

Reputation: 692081

The code is still not compiling, and thus hard to understand. I see two main potential causes of your problem:

  1. You're testing against originalHS at the beginning, but that's not what you're writing to the file. You're writing nameCollection.
  2. You split on ",", and don't trim the results. So a set containing "Smith", "Johnson", "Bates" will be written as [Smith, Johnson, Bates], and be read as a set containing "Smith", " Johnson", " Bates" (i.e. there will be a leading space before each name except the first one).

Upvotes: 1

Related Questions