Search code examples
javaclassobjectarraylistfrequency

How to Determine Frequency of String Array List Elements Using Classes in Java


For a Java code, I have a class Called Animal where I have variables + their getters and setters such as name and age. The main method (in the Launcher class) allows the user to "Drop Off" an animal using the addAnimal method and allows the user to "Pick Up" an animal using the removeAnimal method. There are three animals to utilize, where each of them has their own class: Dog, Cat, and Bird. I want to be able to count the frequency of each animal and display it as an option: "Display Frequency" which would display the frequency of each animal whether they have been "Dropped Off" or not.

I have used the static frequency-method in Collections Collections.frequency(). I found this from Stack Overflow and figured it would work. However, every time I add a Dog, Cat, or Bird, the frequency stays as zero. I have also tried using Map<Animal, Long> counts = animals.stream().collect(Collectors.groupingBy(e -> e, Collectors.counting())); which also did not provide the output I was expecting. It output something like this: Frequency of type 2 is {Cat@27d6c5e0=1, Bird@4f3f5b24=1} where type 2 is Cat (option 2 in which animal I`d like to "drop off").

Down below is my code for where I show the frequency. I dont really have a method for this, and Im not sure whether that would be more convenient.

ArrayList<Animal> animals = new ArrayList<>();

boolean addAnimal (int type, String name, int age) {		
	//int frequency = Collections.frequency(animals, type);		
	boolean isAdded = false;		
	switch (type) {		
	    case 1:	    	
	        animals.add(new Dog(name, age));	        
	        isAdded = true;        
	        System.out.println("Frequency of " + type + ": "+ Collections.frequency(animals, (new Dog(name, age))));	        
	    case 2:	    	
	        animals.add(new Cat(name, age)); 	        
	        isAdded = true;	        
	        System.out.println("Frequency of " + type + ": "+ Collections.frequency(animals, (new Cat(name, age))));	       
	    case 3:	    	
	        animals.add(new Bird(name, age));	        
	        isAdded = true;       
	        System.out.println("Frequency of " + type + ": "+ Collections.frequency(animals, (new Bird(name, age))));        
	    default:	        
	        isAdded = false;        
	 }//end of switch
	    return isAdded;
   }//end of addAnimal(type, name, age)

// here comes the method to remove an animal...

}// end of class Database

What I expect my program to do is that when I choose the option to display the frequency of each animal (let`s say this would be option 3), I would like it to output "The frequency of type Dog (type 1) is 3 because there are three Dogs" and something very similar for Cat (type 2) and Bird (type 3). Is there a simplistic way to do this? I am coding on Java Eclipse.


Solution

  • Here the problem is each time you call the function

    Collections.frequency(animals, (new Dog(name, age)))
    

    Collections.frequency checks the animals for the newly created Object instance which is unique. Therefore, it will return 0. For a simple solution you can define an equals method for your Animal class or create another ArrayList such as

    ArrayList<Integer> types =...
    case 1:         
            animals.add(new Dog(name, age));            
            isAdded = true;
            types.add(1);        
    

    from which you can extract the frequency of the specific type(1,2 or 3) using Collections.frequency()