1
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”。我不明白为什么没有删除重复的单词。会不会跟字符集有关?

4

2 回答 2

3

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

是的,会的。您明确地将所有值添加knownWordsArrayListwordsArrayList

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

目前还不清楚您的代码要做什么(使用整个集合操作和增强的 for 循环将有助于清晰),但这就是为什么 in 中的所有knownWordsArrayList内容也在wordsArrayList.

重要的是,这个声明:

然后我做一个 hashSet 来删除重复的单词

...只是意味着每个单词只会出现一次。这就是它要做的一切。

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

HashSet h = new HashSet(wordsArrayList);
h.removeAll(knownWordsArrayList);
wordsArrayList = new ArrayList<String>(h);
于 2013-04-12T16:20:02.747 回答
1
    String[] arr = { "kitty", "mitty", "kitty" };
    Set<String> hs = new HashSet<String>(Arrays.asList(arr));
    System.out.println(Arrays.asList(arr));
    System.out.println(hs);

输出是:

[kitty, mitty, kitty]
[mitty, kitty]

这是你所期望的吗?

于 2013-04-12T16:29:12.853 回答