{"id":209,"date":"2008-09-09T13:54:42","date_gmt":"2008-09-09T17:54:42","guid":{"rendered":"http:\/\/www.efsavage.com\/blog\/?p=209"},"modified":"2011-06-06T07:08:49","modified_gmt":"2011-06-06T11:08:49","slug":"java-goofs-enhanced-for-loop","status":"publish","type":"post","link":"https:\/\/efsavage.com\/blog\/posts\/java-goofs-enhanced-for-loop\/","title":{"rendered":"Java Goofs: Enhanced For Loop"},"content":{"rendered":"<p>One of the &#8220;big deals&#8221; in Java 5 was the enhanced for-loop.  Basically you could trim this:<\/p>\n<p><code>for(int i=0;i&lt;collection.size();i++) {<br \/>\n     Object object = collection.get(i);<br \/>\n     doSomething(object);<br \/>\n}<br \/>\n<\/code><br \/>\ndown to this:<br \/>\n<code>for(Object object:collection) {<br \/>\n     doSomething(object);<br \/>\n}<\/code><\/p>\n<p>I don&#8217;t think such an improvement was really necessary, but combine this with generics and I have to admit your code does look a little cleaner, so I use them when possible.  However, the for-loop is basically a half-implemented hack with two major goofs.<\/p>\n<p>1. It works with anything implementing Iterable, and arrays.  Despite working with anything Iterable, it does not work actual iterators, which seems just silly to me.<br \/>\n2. If the collection is null, it throws a NullPointerException.  The only defense I&#8217;ve seen for this is that &#8220;a null collection is not the same as an empty collection&#8221;.  Agreed, and those people clearly missed the point.  The point was to clean up the code and now we have to add the same silly if(collection==null) around the loop. If your code really did need to know if the collection was null (e.g. lazy-loading), it&#8217;s going to check anyways, but this would save at least one more repetitive line if the loop just treated null as empty and did nothing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the &#8220;big deals&#8221; in Java 5 was the enhanced for-loop. Basically you could trim this: for(int i=0;i&lt;collection.size();i++) { Object object = collection.get(i); doSomething(object); } down to this: for(Object object:collection) { doSomething(object); } I don&#8217;t think such an improvement was really necessary, but combine this with generics and I have to admit your code&hellip; <\/p>\n<p class=\"simppeli-read-more\"><a href=\"https:\/\/efsavage.com\/blog\/posts\/java-goofs-enhanced-for-loop\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Java Goofs: Enhanced For Loop<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[222,223],"series":[],"class_list":["post-209","post","type-post","status-publish","format-standard","hentry","category-java","tag-java-5","tag-npe"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1AkJt-3n","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/posts\/209","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/comments?post=209"}],"version-history":[{"count":2,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/posts\/209\/revisions"}],"predecessor-version":[{"id":593,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/posts\/209\/revisions\/593"}],"wp:attachment":[{"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/media?parent=209"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/categories?post=209"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/tags?post=209"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/efsavage.com\/blog\/wp-json\/wp\/v2\/series?post=209"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}