/** * Returns a {@link Collection} containing the exclusive disjunction * (symmetric difference) of the given {@link Iterable}s. * <p> * The cardinality of each element <i>e</i> in the returned * {@link Collection} will be equal to * <tt>max(cardinality(<i>e</i>,<i>a</i>),cardinality(<i>e</i>,<i>b</i>)) - min(cardinality(<i>e</i>,<i>a</i>), * cardinality(<i>e</i>,<i>b</i>))</tt>. * <p> * This is equivalent to * {@code {@link #subtract subtract}({@link #union union(a,b)},{@link #intersection intersection(a,b)})} * or * {@code {@link #union union}({@link #subtract subtract(a,b)},{@link #subtract subtract(b,a)})}. * @param a the first collection, must not be null * @param b the second collection, must not be null * @param <O> the generic type that is able to represent the types contained * in both input collections. * @return the symmetric difference of the two collections */ public static <O> Collection<O> disjunction(final Iterable<? extends O> a, final Iterable<? extends O> b) { final SetOperationCardinalityHelper<O> helper = new SetOperationCardinalityHelper<O>(a, b); for (final O obj : helper) { helper.setCardinality(obj, helper.max(obj) - helper.min(obj)); } return helper.list(); }
/** * Returns a {@link Collection} containing the intersection of the given * {@link Iterable}s. * <p> * The cardinality of each element in the returned {@link Collection} will * be equal to the minimum of the cardinality of that element in the two * given {@link Iterable}s. * * @param a the first collection, must not be null * @param b the second collection, must not be null * @param <O> the generic type that is able to represent the types contained * in both input collections. * @return the intersection of the two collections * @see Collection#retainAll * @see #containsAny */ public static <O> Collection<O> intersection(final Iterable<? extends O> a, final Iterable<? extends O> b) { final SetOperationCardinalityHelper<O> helper = new SetOperationCardinalityHelper<O>(a, b); for (final O obj : helper) { helper.setCardinality(obj, helper.min(obj)); } return helper.list(); }
/** * Returns a {@link Collection} containing the union of the given * {@link Iterable}s. * <p> * The cardinality of each element in the returned {@link Collection} will * be equal to the maximum of the cardinality of that element in the two * given {@link Iterable}s. * * @param a the first collection, must not be null * @param b the second collection, must not be null * @param <O> the generic type that is able to represent the types contained * in both input collections. * @return the union of the two collections * @see Collection#addAll */ public static <O> Collection<O> union(final Iterable<? extends O> a, final Iterable<? extends O> b) { final SetOperationCardinalityHelper<O> helper = new SetOperationCardinalityHelper<O>(a, b); for (final O obj : helper) { helper.setCardinality(obj, helper.max(obj)); } return helper.list(); }