加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何将一个Map Entry添加到ArrayList?

发布时间:2020-12-15 04:16:05 所属栏目:Java 来源:网络整理
导读:我正在创建Map Entry键值对的ArrayList.这样我就可以存储大量单个单词(作为键)并保存它们的总次数(作为值). 我需要能够按值对ArrayList进行排序,以便我可以按其值排序单词. 我有两个问题: 我不知道将映射条目添加到ArrayList的语法 我不确定如何对ArrayList
我正在创建Map Entry键值对的ArrayList.这样我就可以存储大量单个单词(作为键)并保存它们的总次数(作为值).

我需要能够按值对ArrayList进行排序,以便我可以按其值排序单词.

我有两个问题:

>我不知道将映射条目添加到ArrayList的语法
>我不确定如何对ArrayList进行排序

任何帮助非常感谢!

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 – 工作代码
嗨,我添加了新的代码,将地图条目添加到ArrayList,这很有用.

我还更新了我的排序代码,这也很有效:

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;
            }
          }
        });
}

解决方法

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;
  }
});

考虑比较方法的合同:

Compares its two arguments for order. Returns a negative integer,zero,or a positive integer as the first argument is less than,equal to,or greater than the second.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读