Java – how to add a map entry to the ArrayList?

I'm creating an ArrayList of map entry key value pairs In this way, I can store a large number of individual words (as keys) and save their total number of times (as values)

I need to be able to sort ArrayList by value so that I can sort words by their value

I have two questions:

>I don't know the syntax for adding mapping entries to ArrayList > I'm not sure how to sort ArrayList

Any help, thank you very much!

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<String,Integer> newEntry = null;

    for (int i = 0; i < 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<String,Integer> 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);
        }

    }

}

-Edit – working code Hi, I added new code to add map entries to ArrayList, which is very useful

I also updated my sorting code, which is also very effective:

private void sortMap() {            
    Collections.sort(mapList,new Comparator<Map.Entry<String,Integer>>() {
          @Override
          public int compare(Map.Entry<String,Integer> o1,Map.Entry<String,Integer> o2) {
            if (o1.getValue()>o2.getValue()){
                return 1;
            }else if (o1.getValue()<o2.getValue()){
                return -1;
            }else{
                return 0;
            }
          }
        });
}

Solution

List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>();
List<Map.Entry<String,String>> list = new ArrayList<Map.Entry<String,String>>();
list.add(new AbstractMap.SimpleEntry<String,String>("foo","bar"));

Collections.sort(list,String>>() {
  @Override
  public int compare(Map.Entry<String,String> o1,String> o2) {
    return 0;
  }
});

Contracts considering the comparison method:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>