How to find the occurrences of a particular element in an ArrayList in Java

How to find the occurrences of a particular element in an ArrayList in Java

How to find the occurrences of a particular element in a Java List

Most of the Java developers are familiar with Collections Framework. Even I have written some of the blogs about Collections which includes List, Set, Linked List, How internally hashset works etc. Even though collection API is a very popular and powerful one, most of the developers will not massively use its power.

Few days back one of my team member, wrote a code in java to find the occurrence of a particular string in an ArrayList. What he did is, he iterated the arraylist and increment the counter variable and whenever he found that particular string in that arraylist. Being a fresher what he did is correct, but being a Java developer we should know the power of Java Collection Framework. Ok. Then what will be the best way for this? Do we have any methods for this? How to find the occurrences of a particular element in an ArrayList in Java in a most efficient way? In this blog we will see the along with some code snippet.

In Java we have a utility class called Collections, in which we have so many methods which will really help us to avoid our own unnecessary object manipulation. Even earlier I have written so many articles which cover important methods in Collections.

Read: Collections.unmodifiableList, Collections.unmodifiableSet, Collections.unmodifiableMap

Collections.frequency

In Collections class we have a static method called “frequency” which will get the occurrence of a particular element.
Employee.java

package com.learnfromexamples.model;

public class Employee {
	private String empno;
	private String empName;

	public Employee(String empno, String empName ) {
		this.empno = empno;
		this.empName = empName;
	}

	public String getEmpno() {
		return empno;
	}

	public void setEmpno(String empno) {
		this.empno = empno;
	}

	public String getEmpName() {
		return empName;
	}

	public void setEmpName(String empName) {
		this.empName = empName;
	}
	@Override
	public String toString() {
		return "Empno is "+ empno + " Empname is " + empName;
	}
	public Employee() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean equals(Object arg0) {
		// TODO Auto-generated method stub
		boolean flag = false;
		Employee employee = (Employee) arg0;
		if(null!= employee && employee.getEmpName().equalsIgnoreCase(empName) && employee.getEmpno().equalsIgnoreCase(empno)){
			flag = true;
		}
		return flag;
	}

}

ElementCountmplementation.java

package com.learnfromexamples.collections;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.learnfromexamples.model.Employee;

public class ElementCountImplementation {
	public static void main(String[] args) {
		List<String> stringLists = new ArrayList<String>();
		stringLists.add("one");
		stringLists.add("two");
		stringLists.add("three");
		stringLists.add("one");
		stringLists.add("one");

		//Get the occurrence of "one" from stringLists

		int oneCount = Collections.frequency(stringLists, "one");
		System.out.println("Count of one is:  "+ oneCount);

		List<Employee> empList = new ArrayList<Employee>();
		empList.add(new Employee("001", "Shidhar"));
		empList.add(new Employee("002", "Pathy"));
		empList.add(new Employee("003", "Sahana"));
		empList.add(new Employee("003", "Sahana"));
		empList.add(new Employee("004", "Sahana"));
		empList.add(new Employee("003", "Sahana"));

		//Get the occurrence of "new Employee("003", "Sahana")" from empLists
		int sahanaCount = Collections.frequency(empList, new Employee("003", "Sahana"));
		System.out.println("Count of sahana is : " +sahanaCount);

	}
}

Output

Count of one is:  3
Count of sahana is : 3

In the above code snippet we have also find the occurrence of Custom object, but we must have to override the equals then only we will come to the object equality.

Leave a comment

Your email address will not be published. Required fields are marked *