title: How to get Second Most repeated Word in String using Maps
body: <p>I am trying to get the second most repeated word in the sentence. </p>
  
  <p>eg: </p>
  
  <p><strong><code>String paraString = "This is a paragraph with multiple strings. Get the second most repeated word from the paragraph text and print the words with count".</code></strong></p>
  
  <p>Here 'the' is repeated for thrice and 'paragraph' &amp; 'with' are repeated twice.</p>
  
  <p>I need to print the second most repeated words 'paragraph' &amp; 'with'.</p>
  
  <p>Here is the program which I wrote to get the First Most Repeated Words.</p>
  
  <pre><code>public Set&lt;String&gt; getMostRepeatedWords(Map&lt;String, Integer&gt; sortedMap) {
      Set&lt;String&gt; mostRepeatedWords = new HashSet&lt;String&gt;();
      int mostrepeatedWord = Collections.max(sortedMap.values());
      for (Map.Entry&lt;String, Integer&gt; entry : sortedMap.entrySet()) {
          if (mostrepeatedWord == entry.getValue()) {
              mostRepeatedWords.add(entry.getKey());
          }
      }
  
      return mostRepeatedWords;
  }
  </code></pre>
  
  <p>Please help me out. </p>
  
  <p>The one option which I have is below. Let me know if there are any other ways.</p>
  
  <pre><code>int mostrepeatedWord = Collections.max(sortedMap.values())-1;
  </code></pre>