0

長いテキスト ファイルがあります。

次に、ファイルから重複を削除します。問題は、検索パラメーターがリストの最初の単語であり、「:」で区切られていることです。

例えば:

ファイル行:

11234567:229283:29833204:2394803
11234567:4577546765:655776:564456456
43523:455543:54335434:53445
11234567:43455:544354:5443

今、私はこれをここに持っています:

11234567:229283:29833204:2394803
43523:455543:54335434:53445

重複から最初の行を取得する必要があります。他は無視されます。

私はこれを試しました:

Set<String> lines11;
try (BufferedReader reader11 = new BufferedReader(new FileReader("test.txt"))) {
    lines11 = new HashSet<>(10000); // maybe should be bigger
    String line11;
    while ((line11 = reader11.readLine()) != null) {
        lines11.add(line11);
    }
} // maybe should be bigger
try (BufferedWriter writer11 = new BufferedWriter(new FileWriter("test.txt"))) {
    for (String unique : lines11) {
        writer11.write(unique);
        writer11.newLine();
    }
}

それは機能していますが、完全な行が複製された場合にのみ削除されます。

すべての行の最初の単語を探し、ここで重複をチェックするように変更するにはどうすればよいですか。重複が見つからない場合は、行全体を保存します。重複する場合は、行を無視しますか?

4

3 に答える 3

0

HashMapを使用してリストに追加するセクションを書きます

    String tmp[] = null;
    HashMap<String, String> lines = new HashMap<String, String>();
    String line11 = "";

    while ((line11 = reader11.readLine()) != null) {
        tmp = line11.split(":");
        if(!lines.containsKey(tmp[0])){
            lines.put(tmp[0], line11);
        }
    }

そのため、ループは最初の単語をキーとして使用して、新しい行のみを追加します

于 2015-04-06T14:53:56.523 に答える
0
    You can add the data in list and take one more set in which you will add first word in that set and try add every time first of new line if it is in set, then it will not be added and return false. On that basis you can add data in list or directly in you new bufferreader.


List<String> lines11;
     Set<String> uniqueRecords;
                try (BufferedReader reader11 = new BufferedReader(new FileReader("test.txt"))) {
                    lines11 = new ArrayList<>(); // no need to give size it will increase dynamically
    uniqueRecords = new HashSet<>();
                    String line11;
                    while ((line11 = reader11.readLine()) != null) {
                           String firstWord = line11.substring(0, firstWord.firstIndexOf(" "));
                           if(uniqueRecords.add(firstWord )){
                               lines11.add(line11);
                                  }



                    }
                } // maybe should be bigger
                try (BufferedWriter writer11 = new BufferedWriter(new FileWriter("test.txt"))) {
                    for (String unique : lines11) {
                        writer11.write(unique);
                        writer11.newLine();

                    }
                }
于 2015-04-06T14:54:44.960 に答える
0

Set<String>各行の最初の単語のみを保持する を維持する必要があります。

List<String> lines11;
Set<String> dups;
try (BufferedReader reader11 = new BufferedReader(new FileReader("test.txt"))) {
    lines11 = new ArrayList<>();
    dups = new HashSet<>();
    String line11;
    while ((line11 = reader11.readLine()) != null) {
        String first = line11.split(":")[0]; // assuming your separator is :
        if (!dups.contains(first)) {
            lines11.add(line11);
            dups.add(first);
        }
    }
}
try (BufferedWriter writer11 = new BufferedWriter(new FileWriter("test.txt"))) {
    for (String unique : lines11) {
        writer11.write(unique);
        writer11.newLine();
    }
}
于 2015-04-06T14:52:23.863 に答える