Java tutorial
//package com.java2s; /* ============== HelperTools ============== * Initial developer: Lukas Diener <lukas.diener@hotmail.com> * * ===== * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE - Version 2 * * Everyone is permitted to copy and distribute verbatim or modified * copies of this license document, and changing it is allowed as long * as the name is changed. * * DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE * TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION * * 0. You just DO WHAT THE FUCK YOU WANT TO. * * ===== * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND. * */ import java.util.Collection; import java.util.HashSet; import java.util.Set; public class Main { /** * This method performs a delta operation on two collections of elements; i.e. it returns a collection with elements that * are not found in the collection returns by the CollectionHelper.intersect() method. * The resulting collection guarantees unique membership as per implementation of the Java {@link java.util.Set} interface. * This is a null-safe operation. * @param firstGroup Collection to be used. * @param secondGroup Collection to be used. * @return The resulting collection which represents the delta of the two groups. */ public static <T> Collection<T> delta(Collection<T> groupOne, Collection<T> groupTwo) { if (groupOne == null) return groupTwo; else if (groupTwo == null) return groupOne; Set<T> delta = new HashSet<T>(); delta.addAll(findGroupOneDelta(groupOne, groupTwo)); delta.addAll(findGroupTwoDelta(groupTwo, groupOne)); return delta; } private static <T> Set<T> findGroupOneDelta(Collection<T> groupOne, Collection<T> groupTwo) { Set<T> delta = new HashSet<T>(); for (T groupOneItem : groupOne) if (!groupTwo.contains(groupOneItem)) delta.add(groupOneItem); return delta; } private static <T> Set<T> findGroupTwoDelta(Collection<T> groupOne, Collection<T> groupTwo) { Set<T> delta = new HashSet<T>(); for (T groupOneItem : groupOne) if (!groupTwo.contains(groupOneItem)) delta.add(groupOneItem); return delta; } }