查詢Set中重複的元素

weixin_34290000發表於2018-08-01

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

public class FindDuplicateWords {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    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);
}

}
Console:
Unique words: []
Duplicate words: []

相關文章