Voglio sapere perché l'implementazione di HashSet, LinkedHashSet e TreeSet non consente elementi null? Ogni volta che provo a compilare il seguente codice, lancia un puntatore nullo.Perché l'interfaccia Set non consente elementi null?
public static void main(String[] args) {
HashSet<Integer> hashSet = new HashSet<Integer>();
hashSet.add(2);
hashSet.add(5);
hashSet.add(1);
// hashSet.add(null); will throw null pointer
hashSet.add(999);
hashSet.add(10);
hashSet.add(10);
hashSet.add(11);
hashSet.add(9);
hashSet.add(10);
hashSet.add(000);
hashSet.add(999);
hashSet.add(0);
Iterator<Integer> it = hashSet.iterator();
while(it.hasNext()){
int i = it.next();
System.out.print(i+" ");
}
}
Per favore guidami.
titolo ingannevole. Per esempio. la documentazione 'HashSet' afferma esplicitamente * Questa classe consente l'elemento nullo. * – Abdull