Android examples for java.util:Collection Contains
Check if one collection contains None from another collection
/*//from w ww .j a v a 2 s .co m ******************************************************************************* * Copyright (C) 1996-2015, International Business Machines Corporation and * * others. All Rights Reserved. * ******************************************************************************* */ import java.util.Collection; import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; import java.util.regex.Matcher; import android.icu.text.UTF16; import android.icu.text.UnicodeSet; import android.icu.text.UnicodeSetIterator; public class Main{ public static boolean containsNone(Collection a, Collection b) { return !containsSome(a, b); } /** * @param a * @param b * @return */ public static boolean containsSome(Collection a, Collection b) { // fast paths if (a.size() == 0 || b.size() == 0) return false; if (a == b) return true; // must test after size test. if (a instanceof SortedSet && b instanceof SortedSet) { SortedSet aa = (SortedSet) a; SortedSet bb = (SortedSet) b; Comparator bbc = bb.comparator(); Comparator aac = aa.comparator(); if (bbc == null && aac == null) { Iterator ai = aa.iterator(); Iterator bi = bb.iterator(); Comparable ao = (Comparable) ai.next(); // these are ok, since the sizes are != 0 Comparable bo = (Comparable) bi.next(); while (true) { int rel = ao.compareTo(bo); if (rel < 0) { if (!ai.hasNext()) return false; ao = (Comparable) ai.next(); } else if (rel > 0) { if (!bi.hasNext()) return false; bo = (Comparable) bi.next(); } else { return true; } } } else if (bbc.equals(a)) { Iterator ai = aa.iterator(); Iterator bi = bb.iterator(); Object ao = ai.next(); // these are ok, since the sizes are != 0 Object bo = bi.next(); while (true) { int rel = aac.compare(ao, bo); if (rel < 0) { if (!ai.hasNext()) return false; ao = ai.next(); } else if (rel > 0) { if (!bi.hasNext()) return false; bo = bi.next(); } else { return true; } } } } for (Iterator it = a.iterator(); it.hasNext();) { if (b.contains(it.next())) return true; } return false; } /** * Get the size of an iterator (number of items in it). * @param source * @return */ public static int size(Iterator source) { int result = 0; while (source.hasNext()) { source.next(); ++result; } return result; } /** * Compare, allowing nulls * @param a * @param b * @return */ public static <T> boolean equals(T a, T b) { return a == null ? b == null : b == null ? false : a.equals(b); } /** * Compare, allowing nulls and putting them first * @param a * @param b * @return */ public static <T extends Comparable> int compare(T a, T b) { return a == null ? b == null ? 0 : -1 : b == null ? 1 : a .compareTo(b); } /** * Compare iterators * @param iterator1 * @param iterator2 * @return */ public static <T extends Comparable> int compare(Iterator<T> iterator1, Iterator<T> iterator2) { int diff; while (true) { if (!iterator1.hasNext()) { return iterator2.hasNext() ? -1 : 0; } else if (!iterator2.hasNext()) { return 1; } diff = CollectionUtilities.compare(iterator1.next(), iterator2.next()); if (diff != 0) { return diff; } } } /** * Compare, with shortest first, and otherwise lexicographically * @param a * @param b * @return */ public static <T extends Comparable, U extends Collection<T>> int compare( U o1, U o2) { int diff = o1.size() - o2.size(); if (diff != 0) { return diff; } Iterator<T> iterator1 = o1.iterator(); Iterator<T> iterator2 = o2.iterator(); return compare(iterator1, iterator2); } /** * Compare, with shortest first, and otherwise lexicographically * @param a * @param b * @return */ public static <T extends Comparable, U extends Set<T>> int compare( U o1, U o2) { int diff = o1.size() - o2.size(); if (diff != 0) { return diff; } Collection<T> x1 = SortedSet.class.isInstance(o1) ? o1 : new TreeSet<T>(o1); Collection<T> x2 = SortedSet.class.isInstance(o2) ? o2 : new TreeSet<T>(o2); return compare(x1, x2); } /** * Compare, allowing nulls and putting them first * @param a * @param b * @return */ public static <K extends Comparable, V extends Comparable, T extends Entry<K, V>> int compare( T a, T b) { if (a == null) { return b == null ? 0 : -1; } else if (b == null) { return 1; } int diff = compare(a.getKey(), b.getKey()); if (diff != 0) { return diff; } return compare(a.getValue(), b.getValue()); } }