* @return true if the collections contain the same elements in the same
* order, false otherwise
*/
- public static boolean collectionsEquals(Collection collection1,
- Collection collection2) {
+ public static boolean collectionsEquals(Collection<?> collection1,
+ Collection<?> collection2) {
if (collection1 == null) {
return collection2 == null;
}
if (collection2 == null) {
return false;
}
- Iterator<Object> collection1Iterator = collection1.iterator();
- Iterator<Object> collection2Iterator = collection2.iterator();
+
+ if (collection1.size() != collection2.size()) {
+ return false;
+ }
+
+ Iterator<?> collection1Iterator = collection1.iterator();
+ Iterator<?> collection2Iterator = collection2.iterator();
while (collection1Iterator.hasNext()) {
- if (!collection2Iterator.hasNext()) {
- return false;
- }
Object collection1Object = collection1Iterator.next();
Object collection2Object = collection2Iterator.next();
if (collection1Object != collection2Object) {
return false;
}
}
- if (collection2Iterator.hasNext()) {
- return false;
- }
return true;
}