Is there any way by which I can avoid using get(0) in the list iteration ?
Its going to be always risky using get(0) while iterating over a list.
I know for sure that in this list I just have one object.
(P.S. I remember my last manager always saying to me to avoid using get(0) on list iteration.)
It’s not really clear what you mean by “risky” but you might consider using Guava’s
Iterables.getOnlyElement:This makes it clear that you expect there to be one and only one element. If you think there may be no elements, you can use the overload which allows you to specify a default value.
That way your expectations are checked when you ask for the element… but it’s not obvious what else you’re looking for. (You remember your last manager warning you about this – but do you remember why he warned you?)