1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class SetSansDoublon
{
public static void main(String args[])
{
Personne p = new Personne("Dupont","Eric");
Personne p1 = new Personne("Durand","Sebastien");
Personne p2 = new Personne("Pepere","Noel");
Personne p3 = new Personne("Dupont","Eric");
Personne p4 = new Personne("Durand","Sebastien");
Set<Personne> monHashSet=new HashSet<Personne>(); // on crée notre Set
monHashSet.add(p);
monHashSet.add(p1);
monHashSet.add(p2);
monHashSet.add(p3);
monHashSet.add(p4);
Iterator it=monHashSet.iterator();
for(int i=0;i<monHashSet.size();i++)
{
Personne pef=(Personne)it.next();
System.out.println("Monsieur " + pef.nom + " " + pef.prenom);
}
}
} |
Partager