不打印重复列表元素的Java方法

不打印重复列表元素的Java方法,java,Java,我试图打印存储对象的arraylist的元素。我只是想让wordIndexer方法打印出给定的单词在对象标题中出现的次数。在本例中,我试图打印“we”,出现了3次“we”,但不幸的是,我只能打印一次 public class RedditThingIndexer { static List<String> words = new ArrayList<>(); public static String wordIndexer(List<Reddit

我试图打印存储对象的arraylist的元素。我只是想让wordIndexer方法打印出给定的单词在对象标题中出现的次数。在本例中,我试图打印“we”,出现了3次“we”,但不幸的是,我只能打印一次

public class RedditThingIndexer {

    static List<String> words = new ArrayList<>();

    public static String wordIndexer(List<RedditThing> list, String word) {
        int count = 0;
        for (RedditThing thing : list) {
            if (thing.getTitle().toLowerCase().contains(word.toLowerCase())) {
                count++;
                words.add(word);
            }
        }
        return word + " appears: " + count + "\n"
                + "New list: " + words;
    }
}
以下是我试图摘录的文本:

[85kaz5 Why do we use pillows now when we sleep? Did we need this during the prehistoric/ancient age? What changed?  askscience 18563 1521502020 ]
公共静态void wordIndexer(){
String text=“85kaz5为什么我们现在睡觉时使用枕头?在史前/古代我们需要枕头吗?发生了什么变化?askscience 18563 1521502020”;
String[]words=text.split(“”);
字符串wordToMatch=“we”;
整数计数=0;

对于(int i=0;i检查列表的大小。它必须是1,即列表仅包含1个元素,即整个字符串。它可以工作,但不幸的是在我的情况下不工作。我可能做错了什么。我将继续查看它,感谢您的帮助
[85kaz5 Why do we use pillows now when we sleep? Did we need this during the prehistoric/ancient age? What changed?  askscience 18563 1521502020 ]
public static void wordIndexer() {

    String text = "85kaz5 Why do we use pillows now when we sleep? Did we need this during the prehistoric/ancient age? What changed?  askscience 18563 1521502020";
    String[] words = text.split(" ");
    String wordToMatch = "we";
    int count = 0;
    for (int i=0;i<words.length;i++) {
      if (words[i].toLowerCase().contains(wordToMatch.toLowerCase())) {
        count++;
      }
    }
  }