Image

Imagekegsay wrote in Imagejava_dev

How to check the number of occurances of a variable/object in an ArrayList?

Hey guys, bit of a problem here and I thought you could help me out.

Wordy explanation:
I have an ArrayList which contains about 10 "Member" objects, and I want to check which one of these objects occur the most. I have tried using Collections "frequency" method but that simply returned 1, stating that there are no duplicates since they don't point to the same place in memory. I have accessor methods such as getMemberID() which I could use instead of comparing the objects, but I'm still pretty stuck on how to do it. Any advice?

Codey explanation:

import java.util.ArrayList;

public class Main {

    public static void main(String[] args) {

       ArrayList<String> list;
       list = new ArrayList<String>();
      
       list.add("Hello");
       list.add("Cats");
       list.add("How");
       list.add("Are");
       list.add("You ");
       list.add("Gentleman");
       list.add("Hello");
       list.add("Hello");
      
       //Prints out [Hello, Cats, How, Are, You , Gentleman, Hello, Hello]
       System.out.println(list);
      
       //How do I find the most frequently occuring String and then display it?
       //In this example it's "Hello".
      
    }

}