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

问题描述:

我有这些代码搜索的Array名录发生,但我的问题是,我怎么能得到导致 出在整数类型,因为我需要在出侧回路的这一边,可能是有另一种方式寻找 发生与使用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); 
} 
+0

也许一个临时变量 – Hydroid 2013-05-06 18:50:16

你应该申报的地图就像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 

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

亲爱的是正确的,但我需要countMap在int值 – Freeman 2013-05-06 19:04:04

设置独特=新的HashSet(名单);

Collections.frequency(列表中,密钥);

有太多的开销。

下面是我会做

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 

EDIT 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 

请注意,你将永远有一个集合,你需要提取物的从它计数为一个pa特别的词有这样或那样的方式。

+0

这是真的,但你能由一个没有单独一个得到像地图 – Freeman 2013-05-06 19:08:21

+0

@Freeman你的意思是...看到我的编辑...? – A4L 2013-05-06 19:14:09

+0

亲爱的A4L第二个代码是好的,但我怎么能得到这些entry.getkey()&entry.getValue()出于此循环的一侧 – Freeman 2013-05-06 19:26:54

该地图的答案工作,但你可以扩大这个答案来解决更多的问题。

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

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

我会先排序列表,以避免每次都通过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}