/** * Returns {@code true} iff the given {@link Collection}s contain * exactly the same elements with exactly the same cardinalities. * <p> * That is, iff the cardinality of <i>e</i> in <i>a</i> is * equal to the cardinality of <i>e</i> in <i>b</i>, * for each element <i>e</i> in <i>a</i> or <i>b</i>. * * @param a the first collection, must not be null * @param b the second collection, must not be null * @return <code>true</code> iff the collections contain the same elements with the same cardinalities. */ public static boolean isEqualCollection(final Collection<?> a, final Collection<?> b) { if(a.size() != b.size()) { return false; } final CardinalityHelper<Object> helper = new CardinalityHelper<Object>(a, b); if(helper.cardinalityA.size() != helper.cardinalityB.size()) { return false; } for( final Object obj : helper.cardinalityA.keySet()) { if(helper.freqA(obj) != helper.freqB(obj)) { return false; } } return true; }
/** * Returns {@code true} iff <i>a</i> is a sub-collection of <i>b</i>, * that is, iff the cardinality of <i>e</i> in <i>a</i> is less than or * equal to the cardinality of <i>e</i> in <i>b</i>, for each element <i>e</i> * in <i>a</i>. * * @param a the first (sub?) collection, must not be null * @param b the second (super?) collection, must not be null * @return <code>true</code> iff <i>a</i> is a sub-collection of <i>b</i> * @see #isProperSubCollection * @see Collection#containsAll */ public static boolean isSubCollection(final Collection<?> a, final Collection<?> b) { final CardinalityHelper<Object> helper = new CardinalityHelper<Object>(a, b); for (final Object obj : a) { if (helper.freqA(obj) > helper.freqB(obj)) { return false; } } return true; }
/** * Returns the frequency of this object in collection A. * @param obj the object * @return the frequency of the object in collection A */ public int freqA(final Object obj) { return getFreq(obj, cardinalityA); }
/** * Returns the frequency of this object in collection B. * @param obj the object * @return the frequency of the object in collection B */ public int freqB(final Object obj) { return getFreq(obj, cardinalityB); }