Java examples for java.util:Collection Compare
Check if two collection are Equal
import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Set; public class Main{ public static <T> Boolean areEqual(final Collection<T> collection1, final Collection<T> collection2, Comparator<T> comparator) { if (!areSameSize(collection1, collection2)) return Boolean.FALSE; ArrayList<T> sortedList1 = new ArrayList<T>(collection1.size()); sortedList1.addAll(collection1); Collections.sort(sortedList1, comparator); ArrayList<T> sortedList2 = new ArrayList<T>(collection2.size()); sortedList2.addAll(collection2); Collections.sort(sortedList2, comparator); return collection1.equals(collection2); }/*from ww w . j ava 2 s . c o m*/ public static <T> Boolean areSameSize(final Collection<T> collection1, final Collection<T> collection2) { if (isEmpty(collection1) && isEmpty(collection2)) return Boolean.TRUE; if (collection1 == collection2) return Boolean.TRUE; if (isNotEmpty(collection1) && isNotEmpty(collection2) && (collection1.size() - collection2.size()) == 0) return Boolean.TRUE; return Boolean.FALSE; } public static <T> Boolean isEmpty(Collection<T> collection) { if (GeneralUtil.isNull(collection)) return Boolean.TRUE; return collection.isEmpty(); } public static <T> Boolean isNotEmpty(Collection<T> collection) { return !isEmpty(collection); } }