ghap
ghap

Reputation: 1

get data frequency from specific variable-in-object arraylist

I have an object, named objectOne, which each of its row contain 3 variables (String var1, String var2, int var3). So for each row, the data should be var1;var2;var3

My question is how to count frequency based on its object and its variable-in-object ? because code like int occurrences = Collections.frequency(list, item); cannot get its occurrences for a specific variable-in-object.

For example i have

objectOne :
is;is;125
that;is;861
you;are;90
that;is;469

I want to get how many occurrence of is word in var2 column (which the answer should be 3) and how many occurrence of that word in var1 column (which the answer should be 2) ?

Any help will be appreciated :)

Upvotes: 0

Views: 734

Answers (2)

R K
R K

Reputation: 41

Wrap the data elements in nodes that have count and value. On insertion, either create a new one if none exists and set the count to 1, or increment count if it's already there. The node count will then only need to be examined instead of calculated.

Upvotes: 0

Dinakar
Dinakar

Reputation: 186

I would implement the logic for finding the frequency by myself in the class as no direct API that exists for this purpose from existing Collection framework.

public int findOccurances(Collection list, String column,String item) {
    int count = 0;
ObjectOne myObj;
String value="";
Iterator itr = list.iterator();
while(itr.hasNext()) {
        myObj = (ObjectOne)itr.next();
    if(column.equalsIgnoreCase("col1")) value = myObj.getCol1();
    if(column.equalsIgnoreCase("col2")) value = myObj.getCol2();
    if(value.equalsIgnoreCase(item)) {
       count++;
    }
}
return count;
}

Hope this would help and throw some light to improve much better.

Upvotes: 1

Related Questions