【问题标题】:How can I add an Map Entry to an ArrayList?如何将地图条目添加到 ArrayList?
【发布时间】:2013-12-08 20:31:08
【问题描述】:

我正在创建一个 Map Entry 键值对的 ArrayList。这样我就可以存储大量单个单词(作为键)并保存它们被使用的总次数(作为值)。

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

我有两个问题:

  1. 我不知道向 ArrayList 添加映射条目的语法
  2. 我不确定如何对 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;
            }
          }
        });
}

【问题讨论】:

  • 编写自己的类来存储这些键值如何?或this(实现 Map.Entry 接口)。可以使用自定义比较器对其进行排序(请参阅Collections.sort)。
  • 与其用 Map.Entries 的 ArrayList 模拟地图,不如直接使用地图?
  • @user949300 我也是这么问自己的。
  • @user949300 你好,这样我就可以根据值对列表进行排序
  • 你仍然可以这样做。在 Map 中完成所有操作后,调用 ArrayList&lt;Map.Entry&lt;String, Integer&gt;&gt; foo = new ArrayList(myMap.entrySet());,然后根据 Marcel 的帖子对该数组进行排序

标签: java dictionary arraylist


【解决方案1】:
List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>();
list.add(new AbstractMap.SimpleEntry<String, String>("foo", "bar"));

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

考虑compare方法的契约:

比较它的两个参数的顺序。返回一个负整数, 零或正整数,因为第一个参数小于、等于 到或大于秒。

【讨论】:

  • 嗨,我似乎无法进行比较。你能检查我更新的问题吗?我最后添加了新的东西。谢谢
  • compare 应该是 return o1.getKey().compareTo(o2.getKey());,而不是 return 0;
  • @Radiodef,是的,它显然不应该总是返回 0。我不知道 OP 的真正要求是什么。因此,我将他指向compare的合同。
  • 嗨 @Radiodef 和 Marcel 我想根据价值订购列表。首先具有最小值的映射条目,大小增加。那有意义吗?谢谢。
  • 在这种情况下,您可能需要return o1.getValue().compareTo(o2.getValue());。 @MarcelStör 我想这对 OP 来说并不明显。 :P
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-14
  • 1970-01-01
  • 2017-09-26
  • 1970-01-01
  • 2015-03-30
  • 1970-01-01
相关资源
最近更新 更多