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))
                dups.add(a);
 
	// Destructive set-difference
        uniques.removeAll(dups);
 
        System.out.println("Unique words:    " + uniques);
        System.out.println("Duplicate words: " + dups);
    }
}
Category >> Collections  
If you want someone to read your code, please put the code inside <pre><code> and </code></pre> tags. For example:
<pre><code> 
String foo = "bar";
</code></pre>

Leave a comment

*