public Set<B> getBs(){
Iterator<A> iterator = myFunc.iterator();
Set<B> result = new HashSet<B>();
while (iterator.hasNext()) {
result.add((B) iterator.next();
}
return result;
}
But, of course, it will not work if all Areturned by the iterator are not Bs.
If you want to filter the iterator, use instanceof:
public Set<B> getBs(){
Iterator<A> iterator = myFunc.iterator();
Set<B> result = new HashSet<B>();
while (iterator.hasNext()) {
A a = iterator.next();
if (a instanceof B) {
result.add((B) iterator.next();
}
}
return result;
}
Using Guava, the above can be reduced to
return Sets.newHashSet(Iterators.filter(myFunc.iterator(), B.class));
source
share