0

我有以下问题:

我正在根据各种参数从 hashSet 中的大量制表符分隔值文件(“原始文件”)中解析一列。我想解析一次,并将其写为一个简化文件(“解析结果”),我不需要每次都重新拆分/过滤所有内容,而只需要读取“解析结果”文件然后构建第二个 hashSet 检索HS ,只要我使用正确的参数启动程序。

当我检查相同的结果时,我有一个奇怪的行为。当我在第三个文件(电话簿)中读取内容并尝试检查该文件行的内容是否包含我知道存在于原始文件和 originalHS 中的名称时,(originalHS.contains(knownName) 为真,但 retrievedHS.contains(knownName) 是假的,但在技术上是相同的。

我再次尝试使这个问题尽可能清晰,代码尽可能简化,

谢谢你的帮助


    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)     

//在我自己的代码中的另一个类中创建,请参见下面的方法代码 //解析由 Recorder_txt 中的 writer 从原始 HS 写入的 HashSet 的方法 // 它打开文件,读取行 [name1,name2,...],抑制 [],拆分行,在 HashSet 中加载 //names

        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
4

1 回答 1

1

代码仍未编译,因此难以理解。我看到您的问题的两个主要潜在原因:

  1. 您在originalHS开始时进行测试,但这不是您要写入文件的内容。你在写nameCollection
  2. 您拆分",",并且不修剪结果。因此,包含"Smith","Johnson"的集合"Bates"将被写为[Smith, Johnson, Bates], 并被读取为包含"Smith"," Johnson"的集合" Bates"(即除了第一个名称之外,每个名称之前都会有一个前导空格)。
于 2013-10-26T13:23:48.490 回答