If I have a collection, such as Collection
, how can I get the first item out? I could just call an Iterator
, take its first
You can do a casting. For example, if exists one method with this definition, and you know that this method is returning a List:
Collection getStrings();
And after invoke it, you need the first element, you can do it like this:
List listString = (List) getStrings();
String firstElement = (listString.isEmpty() ? null : listString.get(0));