如何计算数组列表中的重复单词?

时间:2013-05-06 18:46:44

标签: java arraylist count counter

这些代码用于在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);
}

5 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

  

设置unique = 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

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

答案 2 :(得分: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);
}

答案 3 :(得分:0)

地图回答工作,但您可以扩展此答案以解决更多问题。

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

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

答案 4 :(得分: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}