User Tools

Site Tools


java:jfc_intf_set

This is an old revision of the document!


Set

Το Set είναι ένα Collection το οποίο δεν επιτρέπει διπλές εγγραφές. Δείτε το παρακάτω παράδειγμα κώδικα

FindDups2.java
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);
  }
}

Ένα ενδεικτικό τρέξιμο είναι το παρακάτω

$> java FindDups2 me you me he he her see me you
Unique words:    [see, her]
Duplicate words: [you, me, he]
java/jfc_intf_set.1426525372.txt.gz · Last modified: 2015/03/16 17:02 by gthanos