Android examples for java.util:List
Compares the two given Lists and returns true, if they are equal and false, if they are not.
import com.android.cartloading.models.CartProduct; import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main{ /**//from ww w. ja v a 2 s. com * Compares the two given Lists and returns true, if they are equal and * false, if they are not. Optionally, it is able to compare the order, too. * * @param a one of the two lists to compare * @param b the other one of the two lists to compare * @param compareOrder if the order of the lists should be compared, too (optional) * @return if the two given lists are equal. */ public static <T> boolean areListsEqual(List<T> a, List<T> b, boolean compareOrder) { if ((a == null && b == null)) return true; if ((a == null && b != null) || (a != null && b == null)) return false; int aSize = a.size(); int bSize = b.size(); if (aSize != bSize) return false; if (compareOrder) { for (int i = 0; i < aSize; i++) { if (a.get(i) == null) { if (b.get(i) != null) return false; } else { if (!a.get(i).equals(b.get(i))) return false; } } } else { if (!internalAreCollectionsEqual(a, b, aSize)) return false; } return true; } /** * Calls <T> boolean areCollectionsEqual(List<T> a, List<T> b) with the * given parameters a and b. * * @param a one of the two lists to compare * @param b the other one of the two lists to compare * @return if the two given lists are equal. */ public static <T> boolean areListsEqual(List<T> a, List<T> b) { return areCollectionsEqual(a, b); } private static <T> boolean internalAreCollectionsEqual(Collection<T> a, Collection<T> b, int size) { List<T> aCopy, bCopy; aCopy = new ArrayList<T>(a); bCopy = new ArrayList<T>(b); for (int i = 0; i < size; i++) { T item = aCopy.get(i); if (!bCopy.remove(item)) return false; } if (!bCopy.isEmpty()) return false; return true; } /** * Compares two collections without giving credit to the order. * * @param a one of the two lists to compare * @param b the other one of the two lists to compare * @return if the two given lists are equal. */ public static <T> boolean areCollectionsEqual(Collection<T> a, Collection<T> b) { if ((a == null && b == null)) return true; if ((a == null && b != null) || (a != null && b == null)) return false; int aSize = a.size(); int bSize = b.size(); if (aSize != bSize) return false; return internalAreCollectionsEqual(a, b, aSize); } }