Java: Switch to using an iterator

To avoid raw types / unchecked conversion warnings.