title: How can I sort Map values by key in Java?
body: <p>I have a Map that has strings for both keys and values.</p>
  <p>The data is like the following:</p>
  <blockquote>
  <p>&quot;question1&quot;, &quot;1&quot;<br />
  &quot;question9&quot;, &quot;1&quot;<br />
  &quot;question2&quot;, &quot;4&quot;<br />
  &quot;question5&quot;, &quot;2&quot;</p>
  </blockquote>
  <p>I want to sort the map based on its keys. So, in the end, I will have <code>question1, question2, question3</code>, and so on.</p>
  <p>Eventually, I am trying to get two strings out of this Map:</p>
  <ul>
  <li>First String: Questions (in order 1 .. 10)</li>
  <li>Second String: Answers (in the same order as the question)</li>
  </ul>
  <p>Right now I have the following:</p>
  <pre><code>Iterator it = paramMap.entrySet().iterator();
  while (it.hasNext()) {
      Map.Entry pairs = (Map.Entry) it.next();
      questionAnswers += pairs.getKey() + &quot;,&quot;;
  }
  </code></pre>
  <p>This gets me the questions in a string, but they are not in order.</p>