This is an old revision of the document!
Το Set είναι ένα Collection το οποίο δεν επιτρέπει διπλές εγγραφές. Δείτε το παρακάτω παράδειγμα κώδικα που εξηγεί την λειτουργία του Set.
import java.util.*; public class FindDups { public static void main(String args[]) { Collection<Student> students = new ArrayList<>(); students.add(new Student("John", "Smith")); students.add(new Student("Stanley", "Peters")); students.add(new Student("Edgar", "Bloch")); students.add(new Student("Suzan", "Miles")); students.add(new Student("Mary", "Poppins")); students.add(new Student("John", "Smith")); students.add(new Student("Stanley", "Peters")); Collection <Student> uniques = new HashSet<Student>(); Collection <Student> dups = new HashSet<Student>(); for(Student st : students) { if(uniques.contains(st)) dups.add(st); else uniques.add(st); } uniques.removeAll(dups); System.out.println("-- Uniques --"); print(uniques); System.out.println("-- Dups --"); print(dups); } public static void print(Collection<Student> collection) { Iterator<Student> it = collection.iterator(); while(it.hasNext()) System.out.println(it.next()); } }
Βασικές υλοποιήσεις του Set interface είναι οι παρακάτω:
| Προηγούμενο: Interface java.util.Collections | Περιεχόμενα | Επόμενο: Interface java.util.List |