Java‎ > ‎

collections

Compare/Diff two ArrayLists - Java

package info.compute.example

import java.util.ArrayList;
import java.util.Collection;
import org.apache.commons.collections.CollectionUtils;

public class DiffLists {
    public static void main(String[] args) {
        ArrayList<String> al1 = new ArrayList<String>();
        al1.add("same1");
        al1.add("same2");
        al1.add("al1");
        ArrayList<String> al2 = new ArrayList<String>();
        al2.add("same1");
        al2.add("same2");
        al2.add("al2");
        Collection<String> disJointList = CollectionUtils.disjunction(al1, al2); 
        for (String djStr : disJointList) { 
            System.out.println(djStr);  
        }
        //will print:
        //al1
        //al2
    }
}

Java HashSet collection type, allows no duplicates

import java.util.*; 
HashSet<String> collection = new HashSet<String>();





Comments