简体   繁体   中英

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

Basically I am reading .eml file from a local folder and i want to measure the frequency of each string in all document. I mean How many times an word is found within all documents that I have. I want to use an 2d Array to store all the unique word and its count of occurrence throughout all the documents. My idea is read one document find out the unique word and then insert that word into an array , then read second document and then search for uniqueness in the array , then if the word found increase the no of occurrence , if not found word in the array list then add that word into array and increase count one for that word, then after that read gain the third file do same. I was using the help from here . But it is not checking uniqueness in the array... It is just adding unique words from a file to array. For example in first file the "word" has been occurred 3 times, so it is showing in array |word|3| , then in the second file "word" has been occurred 4 times , so it is showing |word|4| . But I am trying to get it as |word|7|...

The Code That I am taking help from

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

I suggest you store your data in HashMap where your word is key and value will be number of occurences

You can check whether key is present in map. If not insert it and if yes increment its value

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM