0

I am working on android applications. I created two pages i.e Page1 and Page2. Page2 contains an arraylist with repeated values. I am passing a value from page1 and I want to find the number of occurences of the value(coming from page1) in the arraylist in page2. Please suggest what should I have been done to do that task.

Thanks in advance

user1448108
  • 467
  • 2
  • 10
  • 28

3 Answers3

2

below is the solution for your problem

    ArrayList<String> list = new ArrayList<String>();
Collections.frequency(list, "Key/value to search");

reference link >> how to count occurance in arraylist/list

Community
  • 1
  • 1
AAnkit
  • 27,299
  • 12
  • 60
  • 71
1

Collection.frequency() is what you needed,

Try something like,

ArrayList<Integer> intList // your list

Set<Integer> Values = new HashSet<Integer>;
Values.addAll(intList); // all duplicates removed.

for (Integer i : Values) {

 int occurrences = Collections.frequency(intList, i);
 Log.e(i ," occurs " + occurences + " times.");
}
user370305
  • 108,599
  • 23
  • 164
  • 151
1

Use Collections.frequency() method...

Eg:

    public class ArrFrq {

    public static void main(String[] args){
    ArrayList<String> s = new ArrayList<String>();


    s.add("Vivek");
    s.add("Vicky");
    s.add("Vivek");


    System.out.println(Collections.frequency(s, "Vivek"));

    }

}
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75