title: How can I add an Map Entry to an ArrayList?
body: <p>I'm creating an ArrayList of Map Entry key-value pairs. This is so I can store lots of individual words (as a key) and save the total number of times they are used (as the value).</p>
  
  <p>I need to be able to sort the ArrayList by value so I can order the words by their values.</p>
  
  <p>I have 2 problems:</p>
  
  <ol>
  <li>I don't know the syntax to add a map entry to the ArrayList</li>
  <li>I'm not sure how to sort the ArrayList</li>
  </ol>
  
  <p>Any help much appreciated!</p>
  
  <pre><code>protected void addKeywords(Status status) {
      // get tweet
      String str = status.getText();
      // split into an array remove punctuation and make lower case
      String[] splited = str.replaceAll("[^a-zA-Z ]", "").toLowerCase()
              .split("\\s+");
      // vars used in loop
      String thisStr;
      int wordTot;
      Entry&lt;String, Integer&gt; newEntry = null;
  
      for (int i = 0; i &lt; splited.length; i++) {
          // get word from array
          thisStr = splited[i].toLowerCase();
          thisStr = "hi";
  
          // if this is the first word to be added
          if (mapList.size() == 0) {
              //this is the syntax that I don't know!
              newEntry.key = theStr;
              newEntry.value = 1;
              mapList.add(newEntry);
          } else {
              boolean alreadyExists = false;
              //iterate through mapList
              for (Entry&lt;String, Integer&gt; entry : mapList) {
                  // already exists
                  if (entry.getKey() == thisStr) {
                      wordTot = entry.getValue();
                      //increment the value
                      wordTot++;
                      entry.setValue(wordTot);
                      break; 
                  } 
  
              }
              //if we have reached here the value must not be in the arraylist so add it
  
              //again - this is the syntax that I don't know!
              newEntry.key = theStr;
              newEntry.value = 1;
              mapList.add(newEntry);
          }
  
      }
  
  }
  </code></pre>
  
  <p>-EDIT - working code
  Hi, I've added the new code to add map entries to the ArrayList and that is working great. </p>
  
  <p>I have also updated my sort code and this is working great too:                   </p>
  
  <pre><code>private void sortMap() {            
      Collections.sort(mapList, new Comparator&lt;Map.Entry&lt;String, Integer&gt;&gt;() {
            @Override
            public int compare(Map.Entry&lt;String, Integer&gt; o1, Map.Entry&lt;String, Integer&gt; o2) {
              if (o1.getValue()&gt;o2.getValue()){
                  return 1;
              }else if (o1.getValue()&lt;o2.getValue()){
                  return -1;
              }else{
                  return 0;
              }
            }
          });
  }
  </code></pre>