Let's take an empty List :
List<String> dummyList = new ArrayList<String>();
Now what is the difference between below two codes :
1. Using isEmpty() method
if(!dummyList.isEmpty()){
for(String dummy : dummyList){
System.out.println("Enhanced for loop for empty list : "+dummy);
}
}
2. Without Using isEmpty() and relying on for-each
for(String dummy : dummyList){
System.out.println("Enhanced for loop for empty list : "+dummy);
}
Manu times i have been asked to use the first approach rather than second.But even for-each also not traverse if list is empty.
Then what is the difference between two and which one is better to perform ?