How to Count Repetition of Words in Array List?

匿名 (未验证) 提交于 2019-12-03 01:08:02

问题:

I've these code for searching occurrence in Array-List but my problem is how I can get result out side of this for loop in integer type cause I need in out side , may be there is another way for finding occurrence with out using for loop can you help me ? thank you...

List list = new ArrayList(); list.add("aaa"); list.add("bbb"); list.add("aaa");  Set unique = new HashSet(list); for (String key : unique) {  int accurNO = Collections.frequency(list, key);     System.out.println(key + ": " accurNO); } 

回答1:

You should declare a map like Map countMap = new HashMap(); before the loop, and populate it within the loop.

Map countMap = new HashMap(); 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 


回答2:

Set unique = new HashSet(list);

and

Collections.frequency(list, key);

are too much overhead.

Here is how i would do it

List list = new ArrayList(); list.add("aaa"); list.add("bbb"); list.add("aaa");  Map 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()); 

Output

{aaa=2, bbb=1} 

EDIT output one by one: iterate over the set of entries of the map

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

Output

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

EDIT 2 No need for looping

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

Output

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

Note that you will always have a collection and you need extract the count from it for a particular word one way or another.



回答3:

List list = new ArrayList(); list.add("aaa"); list.add("bbb"); list.add("aaa"); Map countMap = new HashMap();  Set unique = new HashSet(list); for (String key : unique) {   int accurNO = Collections.frequency(list, key);   countMap.put(key,accurNO);   System.out.println(key + ": " accurNO); } 


回答4:

The Map answers work, but you can extend this answer to solve more problems.

You create a class that has the field values you need, and put the class in a 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 list    = new ArrayList();      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);         }     } } 


回答5:

I would sort the list first to avoid going thru the whole list with Collections.frequency every time. The code will be longer but much more efficient

    List list = new ArrayList();     list.add("aaa");     list.add("bbb");     list.add("aaa");     Map map = new HashMap();     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); 

output

{aaa=2, bbb=1} 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!