The question is confusingly stated. The fact is, you need to check if getContainers() returns null at some point, unless you will allow the NullPointerException, which in this case, you decided not to take the chance. In both examples, you are checking for null, so this operation takes exactly the same time.
What you are really interested in, is whether writing getContainers() twice will affect performance. If you understand that this method does not do anything more than return a stored object in the (perhaps private) local class, then you shouldn't worry about any performance as it would be unnoticeable. If it does do something more complex but could be cached by the other method, then you should look into the other method. It might not this method's fault for calling it twice. If in doubt, the second example guarantees it is only called once at the cost of declaring a variable for it.
It's also important to note that in the enhanced for loop,
for (Container container : getContainers() )
the getContainers() is only called once, and not every iteration of the for loop. It retrieves the list internally and gets an iterator from it which assigns a value to container each iteration. The only problem is that it doesn't check for NPEs.
getContainers()method; in other words, it gets (or fails to get, in case of anullresult) the list just once; but this has nothing to do with the performance impact of the check fornullitself.getContainers()to return aCollections.emptyList()instead ofnull.