[英]More efficient way of getting frequency of words
我想通過單詞的開頭來計算ArrayList中每個單詞的頻率。 例如[cat,cog,mouse]將表示以c開頭的兩個單詞和以m開頭的一個單詞。 我的代碼工作正常,但是字母表中有26個字母, 如果 s則需要更多。 還有其他方法嗎?
public static void countAlphabeticalWords(ArrayList<String> arrayList) throws IOException
{
int counta =0, countb=0, countc=0, countd=0,counte=0;
String word = "";
for(int i = 0; i<arrayList.size();i++)
{
word = arrayList.get(i);
if (word.charAt(0) == 'a' || word.charAt(0) == 'A'){ counta++;}
if (word.charAt(0) == 'b' || word.charAt(0) == 'B'){ countb++;}
}
System.out.println("The number of words begining with A are: " + counta);
System.out.println("The number of words begining with B are: " + countb);
}
public static void countAlphabeticalWords(List<String> arrayList) throws IOException {
Map<Character,Integer> counts = new HashMap<Character,Integer>();
String word = "";
for(String word : list) {
Character c = Character.toUpperCase(word.charAt(0));
if (counts.containsKey(c)) {
counts.put(c, counts.get(c) + 1);
}
else {
counts.put(c, 1);
}
}
for (Map.Entry<Character, Integer> entry : counts.entrySet()) {
System.out.println("The number of words begining with " + entry.getKey() + " are: " + entry.getValue());
}
public static void countAlphabeticalWords(List<String> arrayList) throws IOException {
Map<Character,AtomicInteger> counts = new HashMap<Character,AtomicInteger>();
String word = "";
for(String word : list) {
Character c = Character.toUpperCase(word.charAt(0));
if (counts.containsKey(c)) {
counts.get(c).incrementAndGet();
}
else {
counts.put(c, new AtomicInteger(1));
}
}
for (Map.Entry<Character, AtomicInteger> entry : counts.entrySet()) {
System.out.println("The number of words begining with " + entry.getKey() + " are: " + entry.getValue());
}
切勿執行list.get(i)
,而應使用for(element : list)
。 並且永遠不要在簽名中使用ArrayList
,而應使用Interface List
這樣您可以更改實現。
這個怎么樣? 考慮到單詞僅以[a-zA-Z]
開頭:
public static int[] getCount(List<String> arrayList) {
int[] data = new int[26];
final int a = (int) 'a';
for(String s : arrayList) {
data[((int) Character.toLowerCase(s.charAt(0))) - a]++;
}
return data;
}
編輯:
出於好奇 ,我做了一個非常簡單的測試,將我的方法和Steph的方法與map進行了比較。 列出236個項目,進行10000000次迭代(不打印結果):我的代碼花費了大約10000ms,Steph花費了大約65000ms。
測試: http : //pastebin.com/HNBgKFRk
數據: http : //pastebin.com/UhCtapZZ
現在,每個字符都可以轉換為整數,表示ASCII十進制。 例如, (int)'a'
是97. 'z'
的ASCII十進制是122 http://www.asciitable.com/
您可以為字符創建一個查找表:
int characters = new int[128]
然后在算法循環中,使用ASCII十進制作為索引並增加值:
word = arrayList.get(i);
characters[word.charAt(0)]++;
最后,您可以打印字符的出現情況:
for (int i = 97; i<=122; i++){
System.out.println(String.format("The number of words beginning with %s are: %d", (char)i, characters[i]));
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.