Solved

Junit assertCollection?

Posted on 2011-03-14
6
427 Views
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)
0
Comment
Question by:allelopath
[X]
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
6 Comments
 
LVL 47

Expert Comment

by:for_yan
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
0
 
LVL 47

Expert Comment

by:for_yan
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++){
if(!a2.contains(a1.get(j)){
return false;
}
}
return true;

}

0
 
LVL 47

Accepted Solution

by:
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++){
if(a1.get(j).equals(a1.get(k))count1++;

}
int count2 = 0;
for(int k=0; k<a2.size(); k++){
if(a1.get(j).equals(a2.get(k))count2++;

}
if(count1 != count2)return false;

}
retrurn true;

}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

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

Expert Comment

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

Expert Comment

by:CEHJ
ID: 35132479
coll1.removeAll(coll2);
boolean identical = coll1.size() == 0;
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 116
Java Restore security prompts not working 10 260
ArrayList Adding/settign data 3 37
Setup Eclipse for Andriod development 2 19
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

734 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