Junit assertCollection?

Posted on 2011-03-14
Last Modified: 2012-05-11
What's the best way to unit test that 2 collections are the same.
(Not the same object, but each element value of one collection is in the other, and vice versa)
Question by:allelopath
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
LVL 47

Expert Comment

ID: 35132060

Are all objects in your collections unique?
Or you may have two equal options present in any of your two collections?

if they are unique then if the sizes of both collections are equal,
and then you just traverse one collection and check that
all elements of this coll;ection are present in the second collection then
tyour collections are the same in the sense you mentioned
LVL 47

Expert Comment

ID: 35132107

if my question above has the anser "yes" then this method should work:

boolean areTheyEqual(ArrayList a1, ArrayList a2){
if(a1.size() != a2.size() ) return false;

for(int j=0; j<a1.size(); j++){
return false;
return true;


LVL 47

Accepted Solution

for_yan earned 500 total points
ID: 35132225

I think this should work even if each collection
has identical items:

boolean areTheyEqual(ArrayList a1, ArrayList a2){
if(a1.size() != a2.size() ) return false;
for(int j =0; j<a1.size(); j++){
int count1 = 0;
for(int k=0; k<a1.size(); k++){

int count2 = 0;
for(int k=0; k<a2.size(); k++){

if(count1 != count2)return false;

retrurn true;

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI


Author Comment

ID: 35132239
Looks like I could also use Apache Commons CollectionsUtils
CollectionUtils.isEqualCollection(actual, expected);
LVL 47

Expert Comment

ID: 35132266
Yes, you are right - that probably would be better.
But it is nice to invent the wheel also :)
LVL 86

Expert Comment

ID: 35132479
boolean identical = coll1.size() == 0;

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

628 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question