title: How to Count Unique Values in an ArrayList?
body: <p>I have to count the number of unique words from a text document using Java. First I had to get rid of the punctuation in all of the words. I used the <code>Scanner</code> class to scan each word in the document and put in an String <code>ArrayList</code>.</p>
  
  <p>So, the next step is where I'm having the problem! How do I create a method that can count the number of unique Strings in the array?</p>
  
  <p>For example, if the array contains apple, bob, apple, jim, bob; the number of unique values in this array is 3.</p>
  
  <hr>
  
  <pre><code>public countWords() {
      try {
          Scanner scan = new Scanner(in);
          while (scan.hasNext()) {
              String words = scan.next();
              if (words.contains(".")) {
                  words.replace(".", "");
              }
              if (words.contains("!")) {
                  words.replace("!", "");
              }
              if (words.contains(":")) {
                  words.replace(":", "");
              }
              if (words.contains(",")) {
                  words.replace(",", "");
              }
              if (words.contains("'")) {
                  words.replace("?", "");
              }
              if (words.contains("-")) {
                  words.replace("-", "");
              }
              if (words.contains("‘")) {
                  words.replace("‘", "");
              }
              wordStore.add(words.toLowerCase());
          }
      } catch (FileNotFoundException e) {
          System.out.println("File Not Found");
      }
      System.out.println("The total number of words is: " + wordStore.size());
  }
  </code></pre>