Java 如何将 Map 条目添加到 ArrayList?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20458666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I add an Map Entry to an ArrayList?
提问by Alistair Colling
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).
我正在创建一个 Map Entry 键值对的 ArrayList。这样我就可以存储大量单个单词(作为键)并保存它们的总使用次数(作为值)。
I need to be able to sort the ArrayList by value so I can order the words by their values.
我需要能够按值对 ArrayList 进行排序,以便我可以按它们的值对单词进行排序。
I have 2 problems:
我有两个问题:
- I don't know the syntax to add a map entry to the ArrayList
- I'm not sure how to sort the ArrayList
- 我不知道将地图条目添加到 ArrayList 的语法
- 我不知道如何对 ArrayList 进行排序
Any help much appreciated!
非常感谢任何帮助!
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've added the new code to add map entries to the ArrayList and that is working great.
- 编辑 - 工作代码 您好,我添加了新代码以将地图条目添加到 ArrayList 并且效果很好。
I have also updated my sort code and this is working great too:
我还更新了我的排序代码,这也很好用:
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;
}
}
});
}
采纳答案by Marcel St?r
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;
}
});
Consider the contract of the compare
method:
考虑compare
方法的契约:
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.
比较它的两个参数的顺序。当第一个参数小于、等于或大于第二个参数时,返回一个负整数、零或正整数。