簡體   English   中英

如何在java中使用二維數組從一堆eml文件中添加唯一字符串和該字符串的計數

[英]How to add unique string and the no of count of that string from a bunch of eml file using 2D Array in java

基本上我正在從本地文件夾讀取 .eml 文件,我想測量所有文檔中每個字符串的頻率。 我的意思是在我擁有的所有文檔中找到一個單詞的次數。 我想使用 2d 數組來存儲所有唯一的單詞及其在所有文檔中的出現次數。 我的想法是讀取一個文檔找出唯一的單詞,然后將該單詞插入一個數組,然后讀取第二個文檔,然后在數組中搜索唯一性,如果找到的單詞增加出現次數,如果在數組中找不到單詞。數組列表然后將該單詞添加到數組中並為該單詞增加計數,然后在讀取后獲得第三個文件。 我正在使用這里的幫助。 但它不是檢查數組中的唯一性......它只是將文件中的唯一單詞添加到數組中。 例如,在第一個文件中,“word”出現了 3 次,所以它顯示在數組 |word|3| 中。 , 那么在第二個文件中 "word" 已經出現了 4 次,所以它顯示 |word|4| . 但我想把它當作 |word|7|...

我正在尋求幫助的代碼

public static String[][] dupWords (String str) {
    String [] stringArray = str.split(" ");
    int countWords = 0;
    int index = 0;
    HashMap<String, String> indexMap = new HashMap<String, String>();
    HashMap<String, Integer> countMap = new HashMap<String, Integer>();

    //int indexx = 0;
    for (int i = 0; i < stringArray.length; i++) {
       String s = stringArray[i];
       if (!indexMap .containsKey(s)) {
         indexMap.put(s, s);
         countMap.put(s, 1);
       }
       else {
         int cnt = countMap.get(s);
         countMap.put(s, cnt+1);
       }
       index += s.length() + 1;
    }

    String [][] retArr = new String[stringArray.length][2];

    for (int i = 0; i < stringArray.length; i++) {
       String s = stringArray[i];
       retArr[i][0] = indexMap.get(s);
       retArr[i][1] = Integer.toString(countMap.get(s));
       System.out.println(retArr[i][0]);
       System.out.println(retArr[i][1]);
    }

    return retArr;
  }

我建議您將數據存儲在HashMap ,其中您的單詞是鍵,值將是出現次數

您可以檢查地圖中是否存在密鑰。 如果不插入它,如果是增加它的值

Foo value = map.get(key);
if (value != null) {
   //increment my value
} else {
   //insert me
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM