Hello,

The below program has declared 2 variables of type Hashset and one of the
statements is trying to load duplicate names into variable dup.

Question: Doesn't add method of Set interface follow "no duplicate rule"?
Then what is the point of having statement uniques.removeAll(dup); Please
advice.

package sethashsetfinddup2;

import java.util.HashSet;
import java.util.Set;

public class Main {

    public static void main(String[] args) {

        // Set up test data
        String name[] = {
            new String("Sang"),
            new String("Shin"),
            new String("Boston"),
            new String("Shin")
        };

        Set uniques = new HashSet();
        Set dups = new HashSet();

        for (int i=0; i<name.length; i++)
            if (!uniques.add(name[i]))
                dups.add(name[i]);

        // Remove items that duplicates
        uniques.removeAll(dups);

        System.out.println("Unique words:    " + uniques);
        System.out.println("Duplicate words: " + dups);
    }

}
Regards
Dharmendran

-- 
To post to this group, send email to javaprogrammingwithpassion@googlegroups.com
To unsubscribe from this group, send email to 
javaprogrammingwithpassion+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to