You have to specify the generic type for the Iterator because without this type the Iterator holds type Object :
Iterator itr = details.iterator();// This holds type Object.
for that it is required to cast every Object.
another reference :
it.next() Returns the next object. If a generic list is being accessed, the iterator will return something of the list's type.
Pre-generic Java iterators always returned type Object, so a downcast
was usually required.
it is not required if you set the type for the Iterator :
Iterator<UserId> itr = details.iterator();
// ^^--------------------------
So you can use without casting :
while (itr.hasNext()) {
UserId ui = itr.next();
//-------------^