Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Iterating Over a TreeMap - incompatible types

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();
                }
like image 729
gazrolo4 Avatar asked Sep 21 '26 19:09

gazrolo4


1 Answers

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
like image 162
Alexis C. Avatar answered Sep 24 '26 08:09

Alexis C.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!