Skip to content

Conversation

@tusharsh8860
Copy link

Add countMatches methods to ArrayUtils for all array types with tests

  1. Implemented countMatches for all primitive arrays (boolean, byte, char, short, int, long, float, double) and generic object arrays, This method is especially useful for primitive arrays, where generic collection utilities like Collections.frequency do not apply.
  2. counts the number of occurrences of the target.
  3. Handles null arrays by returning 0.
  4. Added unit tests for each array type in ArrayUtilsTest to verify correctness(please check or try more cases).
  5. Matches the coding style, Javadoc format, and behavior of existing ArrayUtils methods.

@garydgregory
Copy link
Member

Why is this needed?

@tusharsh8860
Copy link
Author

tusharsh8860 commented Dec 27, 2025

Thanks for reviewing,

Counting occurrences in arrays is a very common task but ArrayUtil currently does not provide a utility for it even though it already offers related methods such as contains, indexOf, and removeAllOccurrences

this method removes the boilerplate for loop for counting and reduces code also I noticed that currently we use a normal for loop or we use Collections.frequency but they require us to first convert the primitive to Object Based collections(data type), however this aims to provide same functionality without using Collections or apache common collection

This method is to provide the same functionality directly for arrays, including primitive types without using Collections or Apache Commons Collections and without unnecessary conversions. This helps keep the data primitive and the code simpler and more efficient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants