Use Java Set to get a collection of unique data

To keep the unique value of a large data set, Set would be a good option. Every element in a set is unique, when a duplicate element is added, it is ignored.

Here is a sample code from oracle.

public class FindDups2 {
    public static void main(String[] args) {
        Set<String> uniques = new HashSet<String>();
        Set<String> dups    = new HashSet<String>();
        for (String a : args)
            if (!uniques.add(a))
	// Destructive set-difference
        System.out.println("Unique words:    " + uniques);
        System.out.println("Duplicate words: " + dups);

Leave a Comment