CollectionUtils isEqualCollection() Example in Java

The org.apache.commons.collections4.CollectionUtils isEqualCollection() method is used to check, two collections(List, Set, etc.) are equal or not. It checks for null as well as the size of collections. The CollectionUtils isEqualCollection() is a static method, which accepts Collection as a parameter.

Note – The order of the elements doesn’t matter to check equality i.e it will check for the same elements, the order can be different.

The isEqualCollection() method is defined as below.

public static boolean isEqualCollection(Collection<?> coll1, Collection<?> coll2)

Let’s see an example of org.apache.commons.collections4.CollectionUtils isEqualCollection() method.

package com.javatute;

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

import org.apache.commons.collections4.CollectionUtils;

public class CollectionsUtilIsEqualCollection {

	public static void main(String[] args) {

		// Create first list
		List<String> list1 = new ArrayList<>();
		// add some element
		list1.add("John");
		list1.add("Mark");
		list1.add("Smith");
		list1.add("Twin");

		// create second list
		List<String> list2 = new ArrayList<>();
		// add some element
		list2.add("John");
		list2.add("Mark");
		list2.add("Smith");
		list2.add("Twin");

		System.out.println(CollectionUtils.isEqualCollection(list1, list2));//true
	}
}

Output :- true

Note – Both lists element can be in any order. The below example will also return true.

package com.javatute;

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

import org.apache.commons.collections4.CollectionUtils;

public class CollectionsUtilIsEqualCollection {

	public static void main(String[] args) {

		// Create first list
		List<String> list1 = new ArrayList<>();
		// add some element
		list1.add("John");
		list1.add("Mark");
		list1.add("Smith");
		list1.add("Twin");

		// create second list
		List<String> list2 = new ArrayList<>();
		// add some element
		list2.add("John");
		list2.add("Mark");
		list2.add("Twin");
		list2.add("Smith");
		

		System.out.println(CollectionUtils.isEqualCollection(list1, list2));//true
	}
}

Output:- true

CollectionUtils isEqualCollection() example using Equator.

There is another overloaded version of isEqualCollection() method that accepts the Equator as the third parameter.

public isEqualCollection(Collection<? extends E> a, Collection<? extends E> b, Equator<? super E> equator)

Let’s see an isEqualCollection() method example where we will pass Equator as the third parameter.

package com.javatute;

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

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Equator;

class Employee {
	int id;
	String name;

	public Employee(int id, String name) {
		super();
		this.id = id;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}


public class CollectionsUtilIsEqualCollection {

	public static void main(String[] args) {
		List<Employee> emplist1 = new ArrayList<>();
		emplist1.add(new Employee(1, "Jack"));
		emplist1.add(new Employee(2, "Twin"));
		emplist1.add(new Employee(3, "Arya"));
		emplist1.add(new Employee(4, "Stark"));

		List<Employee> emplist2 = new ArrayList<>();
		emplist2.add(new Employee(1, "Jack"));
		emplist2.add(new Employee(2, "Twin"));
		emplist2.add(new Employee(3, "Arya"));
		emplist2.add(new Employee(4, "Stark"));
			
		boolean isValue = CollectionUtils.isEqualCollection(emplist1, emplist2, new Equator<Employee>() {

			@Override
			public boolean equate(Employee employee1, Employee employee2) {
				
				return employee1.getName().equals(employee2.getName());
			}

			@Override
			public int hash(Employee o) {
				return 0;
			}
		});
				
	     System.out.println(isValue);
	}
}

That’s all about CollectionUtils isEqualCollection() Example in Java.

See CollectionUtils isEqualCollection() Java docs for more details.

Summary – We have seen how to use org.apache.commons.collections4.CollectionUtils isEqualCollection() method to check whether two collections are equal or not. While using isEqualCollection() order doesn’t matter. It also compares null elements. We also learn how to use the equator.