リスト内の同じ要素と異なる要素Demoを取得


import java.util.*;

public class CollectionUtil {
    private CollectionUtil() {
    }

    /**
     *             
     *
     * @param collmax
     * @param collmin
     * @return
     */
    public static Collection getDifferent(Collection collmax, Collection collmin) {
        //  LinkedList       ,    
        Collection csReturn = new LinkedList();
        Collection max = collmax;
        Collection min = collmin;
        //     ,       map if    
        if (collmax.size() < collmin.size()) {
            max = collmin;
            min = collmax;
        }
        //      ,     
        Map map = new HashMap(max.size());
        for (Object object : max) {
            map.put(object, 1);
        }
        for (Object object : min) {
            if (map.get(object) == null) {
                csReturn.add(object);
            } else {
                map.put(object, 2);
            }
        }
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getValue() == 1) {
                csReturn.add(entry.getKey());
            }
        }
        return csReturn;
    }

    /**
     *             
     *
     * @param collmax
     * @param collmin
     * @return
     */
    public static Collection getSame(Collection collmax, Collection collmin) {
        //  LinkedList       ,    
        Collection csReturn = new LinkedList();
        Collection max = collmax;
        Collection min = collmin;
        //     ,       map if    
        if (collmax.size() < collmin.size()) {
            max = collmin;
            min = collmax;
        }
        //      ,     
        Map map = new HashMap(max.size());
        for (Object object : max) {
            map.put(object, 1);
        }
        for (Object object : min) {
            if (map.get(object) != null) {
                csReturn.add(object);
            }
        }
        return csReturn;
    }

    /**
     *            ,    
     *
     * @param collmax
     * @param collmin
     * @return
     */
    public static Collection getDiffentNoDuplicate(Collection collmax, Collection collmin) {
        return new HashSet(getDifferent(collmax, collmin));
    }

    public static void main(String[] args) throws Exception {
        List num1 = new ArrayList();
        List num2 = new ArrayList();
        num1.add(1);
        num1.add(7);
        num1.add(9);
        num1.add(10);

        num2.add(1);
        num2.add(7);
        num2.add(9);
        num2.add(10);
        num2.add(12);




        Collection different = CollectionUtil.getDifferent(num1, num2);

        System.out.println(different);
    }
}