You can check if the an Iterable
is empty using iterator().hasNext()
.
Iterable<Type> i = /* assigned somehow */;
i.iterator().hasNext();
Question
Suppose a method returns Iterable<Type>
. Is there a more elegant and efficient way to check whether what is returned is empty (or of some given size) than what I'm doing now?
int i = 0;
for (Type dummy : method)
i++;
if (i == 0)
...
Solution
You can check if the an Iterable
is empty using iterator().hasNext()
.
Iterable<Type> i = /* assigned somehow */;
i.iterator().hasNext();
OTHER TIPS
You could use Guava's Iterables.isEmpty:
boolean empty = Iterables.isEmpty(method);
To find a size of an Iterable there is also Iterables.size:
int size = Iterables.size(method);
If you have Iterable
it provide iterator()
. Check hasNext()
.
And se the javadoc.
Iterator<String> itr = collectionObject.iterator();
while (itr.hasNext()) {
//process here with next method on itr object
}
if (element.equals("")) {
System.out.println("Empty...");
}