tag:blogger.com,1999:blog-36773632.post3912514284381744219..comments2023-12-19T08:40:05.062-05:00Comments on Java Evangelist John Yeary: Type Safe Collection ConversionJohn Yearyhttp://www.blogger.com/profile/00461192445071361043noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-36773632.post-79541602237398974722011-09-16T20:24:26.740-05:002011-09-16T20:24:26.740-05:00The enhanced for loop protects the list values fro...The enhanced for loop protects the list values from being null since we are guaranteed to iterate over all objects in the list.<br /><br /> public static void main(String[] args) {<br /> List list = new ArrayList();<br /> for (int i = 0; i < 10; i++) {<br /> list.add("X=" + i);<br /> }<br /> System.out.println(list.size());<br /> list.remove(0);<br /> System.out.println(list.size());<br /> list.remove(5);<br /> System.out.println(list.size());<br /> for (int i = 0; i < list.size(); i++) {<br /> System.out.println("i: " + i<br /> + " value: " + list.get(i));<br /> }<br /> System.out.println("\n");<br /> int i = 0;<br /> for (String s : list) {<br /> System.out.println("i: " + i<br /> + " value: " + s);<br /> i++;<br /> }<br /> System.out.println(list.size());<br /> }John Yearyhttps://www.blogger.com/profile/00461192445071361043noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-87921007992978254602011-09-09T09:42:46.422-05:002011-09-09T09:42:46.422-05:00I was discussing your method with one of our other...I was discussing your method with one of our other developers, and notice that you may want to include a check to verify that none the elements in the list are null, to ensure that you don't through a NullPointerException at line 8.<br /><br />>> if (o != null && !clazz.isAssignableFrom(o.getClass())) { ...<br /><br />Other options that we looked at was: <br />=> if (clazz.instanceof(o)) {<br /><br />Appeared to work about as well.<br /><br />But in all cases, it definely provided an excellent learning opportunity.<br /><br />James..James C Bragghttps://www.blogger.com/profile/16635260064181688029noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-25248866692734904752011-08-25T16:44:56.682-05:002011-08-25T16:44:56.682-05:00Interesting blog posts and comments!
Immutable{Li...Interesting blog posts and comments!<br /><br />Immutable{List,Set,Map}, like you mention, would work great until you do want to change it. Unfortunately, I only see two non-desirable ways to do that. One would be to copy the data structure's objects that you want to keep and add the new objects and/or remove the unwanted objects as needed. This would be slow. Another approach would be to cast back to the mutable version and modify it. This would be even worse because it invalidates anything holding onto the data structure. (Ack, my immutable data structure mutated!)<br /><br />Many functional programming languages, and I think Clojure especially, has some interesting approaches to this. I'm hoping to discuss this at an upcoming GreenJUG meeting ;-)s450r1https://www.blogger.com/profile/15254179029831758075noreply@blogger.comtag:blogger.com,1999:blog-36773632.post-22253980072624954602011-08-25T08:38:41.151-05:002011-08-25T08:38:41.151-05:00Looks like we were both up half the night working ...Looks like we were both up half the night working on the same thing! I posted <a href="http://glenpeterson.blogspot.com/2011/08/looks-like-johns-comment-yesterday-had.html" rel="nofollow">my version</a> this morning as well because it's a little different.<br /><br />ImmutableList is an interesting concept. I think it should have been an interface, the same as the existing List interface, but without the methods: add(), addAll(), clear(), remove(), removeAll(), retainAll(), and set(). The existing List interface should be extended from ImmutableList in order to add those methods. Ditto Set and Map should be extended from ImmutableSet and ImmutableMap. If a function took an ImmutableList argument, the caller would know that it couldn't modify the list. No casting, no creating new lists, no UnsupportedOperationExceptions would be required. If the objects it contains are immutable, an ImmutableList parameter would be truly unmodifiable the way a primitive parameter is.<br /><br />It would make writing threadsafe code much easier. Create your list as mutable, then cast or pass it as immutable once it's constructed and as long as you don't change it, you never have to worry about its thread safety again.Anonymoushttps://www.blogger.com/profile/01199522537419481981noreply@blogger.com