Reputation: 161
I have the following collection:
private Map <String, Staff> staff;
Implemented as a TreeMap:
staff = new TreeMap <String, Staff> ();
I need to iterate over the values in this map, but when I try the following code I'm getting an incompatible types compilation error. I can't understand why this is; the values in my map are Staff objects and
it.HasNext()
should be returning them to be stored in the staffMember variable, which should be fine to my knowledge?? Help much appreciated.
Collection <Staff> staffList = staff.values();
Iterator it = staffList.iterator ();
while ((isJobAssigned = false) ||it.hasNext())
{
Staff staffMember = it.next();
if ((staffMember instanceof Typist) && (jobType.equalsIgnoreCase("Typist")))
{
newJob.setJobState ("Assigned");
staffMember.setState("Working");
return newJon.getJobNo() + " Staff allocated: " + staffMember.getName () + ", ID: " + staffMember.getId();
}
Upvotes: 3
Views: 284
Reputation: 4899
Why aren't you using:
for (Staff st : staff.values()){
// do your stuff
if(st instanceof Typist) break;
}
Upvotes: 3
Reputation: 93842
You are using a raw Iterator
. Either you need to cast to Staff
the Object
returned by it.next()
or use a generic Iterator
.
Using a raw iterator :
Staff staffMember = (Staff)it.next();
Using a generic iterator (I recommend this version) :
Iterator<Staff> it = staffList.iterator();
Staff staffMember = it.next(); //you can keep this
Upvotes: 6