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();
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With