I'm trying to see if HashSet would be the solution for my next project so i'm doing some very easy test to check functionalities.
I have a simple class Klant
:
public class Klant {
private int klantNummer;
public Klant(int nummer) {
this.klantNummer = nummer;
}
public int getKlantNummer() {
return this.klantNummer;
}
}
and a class with through composition uses a HashSet
public class MySet<Klant> {
private Collection<Klant> mySet = null;
public MySet() {
mySet=new HashSet<Klant>();
}
public void add(Klant elem) {
mySet.add(elem);
}
public void toon() {
Iterator<Klant> i = mySet.iterator();
while(i.hasNext()) {
Klant k = i.next();
System.out.println(k.);
}
}
}
The problem is in the method toon()
Basically even though i specify that the Iterator will contain Klant objects <Klant>
The local k
object does not provide me with the getKlantNummer()
mthod defined in Klant
The k
object its still an Object
instance, and even by casting it with:
Object k = (Klant)i.next();
it won't work. Down-casting is dangerous, but as far as i remember it is not prohibited.
Any advice?