Java tutorial
//package com.java2s; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; public class Main { /** Returns two ArrayLists, containing the elements only present in the first and second argument, respectively. */ @SuppressWarnings("unchecked") public static ArrayList<String>[] getUniqueElements(Collection<? extends String> list1, Collection<? extends String> list2) { final ArrayList<String> onlyInOne = new ArrayList<String>(list1); onlyInOne.removeAll(list2); Collections.sort(onlyInOne); final ArrayList<String> onlyInTwo = new ArrayList<String>(list2); onlyInTwo.removeAll(list1); Collections.sort(onlyInTwo); return new ArrayList[] { onlyInOne, onlyInTwo }; } public static ArrayList<String>[] getUniqueElements(String[] array1, String[] array2) { return getUniqueElements(Arrays.asList(array1), Arrays.asList(array2)); } }