0

我有这些代码用于在 Array-List 中搜索事件,但我的问题是如何在整数类型的 for 循环中获取结果,因为我需要在外部,可能有另一种方法可以在不使用 for 的情况下查找事件循环你能帮我吗?谢谢你...

List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");

Set<String> unique = new HashSet<String>(list);
for (String key : unique) {
 int accurNO = Collections.frequency(list, key);
    System.out.println(key + ": " accurNO);
}
4

5 回答 5

2

设置唯一 = new HashSet(list);

Collections.frequency(list, key);

开销太大。

这是我会怎么做

List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");

Map<String, Integer> countMap = new HashMap<>();


for (String word : list) {
    Integer count = countMap.get(word);
    if(count == null) {
        count = 0;
    }
    countMap.put(word, (count.intValue()+1));
}

System.out.println(countMap.toString());

输出

{aaa=2, bbb=1}

一个一个地编辑输出:迭代地图的条目集

for(Entry<String, Integer> entry : countMap.entrySet()) {
    System.out.println("frequency of '" + entry.getKey() + "' is "
          + entry.getValue());
}

输出

frequency of 'aaa' is 2
frequency of 'bbb' is 1

编辑 2无需循环

String word = null;
Integer frequency = null;

word = "aaa";
frequency = countMap.get(word);
System.out.println("frequency of '" + word + "' is " +
    (frequency == null ? 0 : frequency.intValue()));

word = "bbb";
frequency = countMap.get(word);
System.out.println("frequency of '" + word + "' is " + 
    (frequency == null ? 0 : frequency.intValue()));

word = "foo";
frequency = countMap.get(word);
System.out.println("frequency of '" + word + "' is " + 
    (frequency == null ? 0 : frequency.intValue()));

输出

frequency of 'aaa' is 2
frequency of 'bbb' is 1
frequency of 'foo' is 0

请注意,您将始终拥有一个集合,并且您需要以一种或另一种方式从中提取特定单词的计数。

于 2013-05-06T18:57:47.003 回答
2

您应该像循环之前一样声明一个映射Map<String, Integer> countMap = new HashMap<String, Integer>();,并在循环中填充它。

Map<String, Integer> countMap = new HashMap<String, Integer>();
for (String key : unique) {
    int accurNO = Collections.frequency(list, key);
    coutMap.put(key, accurNO);
    //...
}
//now you have a map with keys and their frequencies in the list
于 2013-05-06T18:50:24.167 回答
1
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("aaa");
Map<String,Integer> countMap = new HashMap();

Set<String> unique = new HashSet<String>(list);
for (String key : unique) {
  int accurNO = Collections.frequency(list, key);
  countMap.put(key,accurNO);
  System.out.println(key + ": " accurNO);
}
于 2013-05-06T18:50:47.667 回答
0

地图答案有效,但您可以扩展此答案以解决更多问题。

您创建一个具有所需字段值的类,并将该类放入列表中。

import java.util.ArrayList;
import java.util.List;

public class WordCount {

    private String word;
    private int count;

    public WordCount(String word) {
        this.word = word;
        this.count = 0;
    }

    public void addCount() {
        this.count++;
    }

    public String getWord() {
        return word;
    }

    public int getCount() {
        return count;
    }

}

class AccumulateWords {
    List<WordCount> list    = new ArrayList<WordCount>();

    public void run() {
        list.add(new WordCount("aaa"));
        list.add(new WordCount("bbb"));
        list.add(new WordCount("ccc"));

        // Check for word occurrences here

        for (WordCount wordCount : list) {
            int accurNO = wordCount.getCount();
            System.out.println(wordCount.getWord() + ": " + accurNO);
        }
    }
}
于 2013-05-06T19:06:11.427 回答
0

我会先对列表进行排序,以避免每次都使用 Collections.frequency 遍历整个列表。代码会更长但效率更高

    List<String> list = new ArrayList<String>();
    list.add("aaa");
    list.add("bbb");
    list.add("aaa");
    Map<String, Integer> map = new HashMap<String, Integer>();
    Collections.sort(list);
    String last = null;
    int n = 0;
    for (String w : list) {
        if (w.equals(last)) {
            n++;
        } else {
            if (last != null) {
                map.put(last, n);
            }
            last = w;
            n = 1;
        }
    }
    map.put(last, n);
    System.out.println(map);

输出

{aaa=2, bbb=1}
于 2013-05-06T19:14:10.223 回答