title: Creating a LinkedList containing unique wordnodes of a text source
body: <p>I want to create a <code>LinkedList</code> of nodes containing unique words of a text and for each node create another list containing the words that follow them in the text.<br>
  I have come up with this code, but it is not correct. It seems that method <code>contains</code> always produces <code>false</code>. Any suggestions?</p>
  
  <pre><code>public void train(String sourceText){
      String[] words=sourceText.split("\\s+");
      List&lt;String&gt; textList=Arrays.asList(words);
      starter=textList.get(0);
      System.out.println(starter);
      ListNode prevWord= new ListNode(starter);
      wordList = new LinkedList&lt;ListNode&gt;();
      wordList.add(prevWord);
      for (int i=1;i&lt;textList.size();i++){
          if (wordList.contains(prevWord)){
              prevWord.addNextWord(textList.get(i));
          }
          else{
              wordList.add(prevWord);
              prevWord.addNextWord(textList.get(i));
              }
          prevWord=new ListNode(textList.get(i));
      }
  }
  
  class ListNode
  {
      private String word;
  
      private List&lt;String&gt; nextWords;
  
      ListNode(String word)
      {
          this.word = word;
          nextWords = new LinkedList&lt;String&gt;();
      }
  
      public String getWord()
      {
          return word;
      }
  
      public void addNextWord(String nextWord)
      {
          nextWords.add(nextWord);
      }
  }
  </code></pre>