Link to home
Start Free TrialLog in
Avatar of sunda2010
sunda2010

asked on

to find the word occurence in a file

hello experts
                   could you please tell me how to find the number of occurence of words that are in a file so that all words should be compared and all the words will be assigned count for their occurence
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

1) Add all the items (words in a file) into a list
2) Create the Set object, it will remove the duplicates.
3) Use Collection.frequency() to get the frequency of any item of a set in the list.

Create an object with two parameters as word text and number of occurence.
Have two TreeSet with Comparator that compares based on word text and based on number of occurence.

Flow is like this.

1. Read every word and create the Object with count as 1.
2. Add it in the WordTreeSet, if it already exists then increment the count by 1.
3. Add incremented object in the CountTreeSet.

If you would like to search based on word use WordTreeSet and if you would like to search based on count, then use CountTreeSet. Basically the idea is creating only one object and creating two indices based on word and count.

ASKER CERTIFIED SOLUTION
Avatar of rumi78
rumi78

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial