import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

public class Test {

    List<String> knownWordsArrayList = new ArrayList<String>();
    List<String> wordsArrayList = new ArrayList<String>();

    public void readKnownWordsFile()
    {
        try {
            FileInputStream fstream2 = new FileInputStream("knownWords.txt");
            BufferedReader br2 = new BufferedReader(new InputStreamReader(fstream2));
            String strLine;
            while ((strLine = br2.readLine()) != null) {
                knownWordsArrayList.add(strLine);
            }
        } catch (Exception e) {
        }

    }

    public void readFile() {
        try {
            FileInputStream fstream = new FileInputStream("newWords.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;
            String numberedLineRemoved = "";
            String strippedInput = "";
            String[] words;
            String trimmedString = "";
            while ((strLine = br.readLine()) != null) {
                numberedLineRemoved = numberedLine(strLine);
                strippedInput = numberedLineRemoved.replaceAll("\\p{Punct}", "");
                if ((strippedInput.trim().length() != 0) || (!strippedInput.contains("")) || (strippedInput.contains(" "))) {
                    words = strippedInput.split("\\s+");
                    for (int i = 0; i < words.length; i++) {
                        if (words[i].trim().length() != 0) {
                            wordsArrayList.add(words[i]);
                        }
                    }
                }
            }

            for (int i = 0; i < knownWordsArrayList.size(); i++) {
                wordsArrayList.add(knownWordsArrayList.get(i));
            }
            HashSet h = new HashSet(wordsArrayList);
            wordsArrayList.clear();
            wordsArrayList.addAll(h);
            for (int i = 0; i < wordsArrayList.size(); i++) {
                System.out.println(wordsArrayList.get(i));
            }
            System.out.println(wordsArrayList.size());
            in.close();
        } catch (Exception e) {// Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }

    public String numberedLine(String string) {
        if (string.matches(".*\\d.*")) {
            return "";
        } else {
            return string;
        }
    }

    public static void main(String[] args) {
        Test test = new Test();
        test.readKnownWordsFile();
        test.readFile();

    }

}


从文件添加到knownWordsArrayList。然后我转到另一个文件,并将单词放入wordsArrayList中。然后,我创建一个hashSet来删除重复的单词,但是它们仍然存在。例如,“ Mrs”在knownWordsArrayList中,但是当我打印wordsArrayList时,仍然看到“ Mrs”。我不明白为什么重复的单词没有被删除。可能与字符集有关吗?

最佳答案

例如,“ Mrs”在knownWordsArrayList中,但是当我打印wordsArrayList时,仍然看到“ Mrs”。


好,是的。您将所有值从knownWordsArrayList显式添加到wordsArrayList

 for (int i = 0; i < knownWordsArrayList.size(); i++) {
      wordsArrayList.add(knownWordsArrayList.get(i));
 }


目前尚不清楚您的代码将要做什么(使用整体集合操作和增强的for循环在清晰度方面会有所帮助),这就是knownWordsArrayList中的所有内容也都包含在wordsArrayList中的原因。

重要的是,此语句:


  然后我进行hashSet删除重复的单词


...只是意味着每个单词只会出现一次。这就是它的全部工作。

我怀疑您应该删除上面引用的代码,而是这样做:

HashSet h = new HashSet(wordsArrayList);
h.removeAll(knownWordsArrayList);
wordsArrayList = new ArrayList<String>(h);

10-07 14:54