tag:blogger.com,1999:blog-741750605858169835.post6336290670777992176..comments2024-01-24T14:53:02.919+00:00Comments on Stephen Colebourne's blog: Iterable in Java SE 8Stephen Colebournehttp://www.blogger.com/profile/01454237967846880639noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-741750605858169835.post-85998454950394490712018-09-26T15:28:08.725+01:002018-09-26T15:28:08.725+01:00Is the assertion here that these additions to Iter...Is the assertion here that these additions to Iterable violate the interface-segregation principle?jaco0646noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-19656767536720628122014-07-03T22:01:04.469+01:002014-07-03T22:01:04.469+01:00I fail to get your point: as I see it, a Spliterat...I fail to get your point: as I see it, a Spliterator is just an Iterator for parallel traversal, so if you don't have anything against a client being able to do summary.iterator(), than why do you have something against a client's ability to do summary.spliterator()?<br />And what's not to like about something as readable as:<br />summary.forEach(item -> System.out::println);<br />?Anthony Venoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-33908754739749299422014-07-01T15:02:04.554+01:002014-07-01T15:02:04.554+01:00I understand your criticism, but I think you have ...I understand your criticism, but I think you have the same problem when implementing the Iterable interface on a domain class. It exposes the iterator() method, which means for clients that you have an "iteration" sense on your domain class. Clients can invoke the iterator() method as they'd like. In Java, It means you have an implicitly domain class backed by a Collection.<br /><br />The majority of clients can invoke:<br /><br />FooSummary summary = ... <br />for (FooItem item : summary) { <br />... <br />} <br /><br />but the Iterable interface allows other clients to do: <br /><br />Iterator<FooItem> iterator = summary.iterator(); <br />while(iterator.hasNext()) { <br /> FooItem item = iterator.next(); <br />}<br /><br />I think you have the same issue. What do you think about it?Rafael Naufalhttp://rafaelnaufal.comnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-17465806865951237882014-07-01T07:06:19.224+01:002014-07-01T07:06:19.224+01:00If you are consuming an Iterable within a method t...If you are consuming an Iterable within a method then the extra default methods make perfect sense. It is the impact on classes which implement Iterable I'm annoyed by, because many of those are not collection classes, and do not warrant having the two extra methods. Now Java 8 is released, this is a permanent change.Stephen Colebournehttps://www.blogger.com/profile/01454237967846880639noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-27502641757570272122014-07-01T07:01:26.785+01:002014-07-01T07:01:26.785+01:00I don't want the domain class to *be* a collec...I don't want the domain class to *be* a collection, I just want to help it interact *with* collections.Stephen Colebournehttps://www.blogger.com/profile/01454237967846880639noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-69438096175694410552014-06-29T21:38:54.971+01:002014-06-29T21:38:54.971+01:00I think I don't share your thoughts about &quo...I think I don't share your thoughts about "losing" the iteration behavior with the change introduced by Java 8. I use the Iterable interface as a common role to domain objects, as you do. <br /><br />The default methods are based only on abstract types and clients are not tightly coupled with any implementation classes, so they can decide to supply any Consumer concrete class or receive an SplitIterator abstract type.<br /><br />If you have an Iterable type acting as a method argument or as a role on your class, you implicitly have a sequence of objects behind it, so I think clients would be interested in consuming it or splitting it. Moreover, clients could add the behavior they want for consuming the objects in the sequence without managing the iteration explicitly.<br /><br />Another solution would be to extend the Iterable interface and add the new default methods on it. What do you think about it?Rafael Naufalhttp://rafaelnaufal.comnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-64033018591394751612014-06-27T22:06:47.679+01:002014-06-27T22:06:47.679+01:00I too make frequent use of Iterable as a common in...I too make frequent use of Iterable as a common interface for domain objects, and share your criticism.<br /><br />As an experiment I created a dummy Iterator class and tried this for-each loop:<br /><br />for (final String s : SIterator::new)<br /> out.println(s);<br /><br />Ok, does not compile, but this does:<br /><br />for (final String s : (Iterable) SIterator::new)<br /> out.println(s);<br /><br />I was surprised the compiler did not work this out for itself.Brian Oxleyhttps://www.blogger.com/profile/06617364377560752378noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-29759258059996289332014-06-27T13:43:47.236+01:002014-06-27T13:43:47.236+01:00I also and still fail to understand.
> In my c...I also and still fail to understand.<br /><br />> In my cases, I definitely do not want those extra methods.<br /><br />Why not ?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-67310865889515537962014-06-27T11:30:16.982+01:002014-06-27T11:30:16.982+01:00Iterable is two things - a super-type of collectio...Iterable is two things - a super-type of collection and a type that produces an iterator. I didn't realise the distinction would matter until I started using Java 8, where only the former now works.<br /><br />As an analogy, imagine if default methods has been added to Comparable, say for isGreaterThan()/isLessThan(). Such a decision would have left date classes in a mess, as isBefore()/isAfter() are the correct terminology to use there. The point of the post is that these "common" interfaces exist to provide framework access to things (iteration/comparisons) and that Java 8 no longer has a "common" interface for iteration.Stephen Colebournehttps://www.blogger.com/profile/01454237967846880639noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-86444119536187527132014-06-27T11:24:40.195+01:002014-06-27T11:24:40.195+01:00Yes, its about clutter. A FooSummary is not a real...Yes, its about clutter. A FooSummary is not a real collection, its in that blury space where it can sometimes be thought of as a collection. It makes calling code less verbose to be able to call addItems(summary) rather than addItems(summary.getItems()). Its the difference between allowing FooSummary to operate nicely with collections and actually *being* a collection. As its the former, the forEach() and spliterator() methods just don't make sense.Stephen Colebournehttps://www.blogger.com/profile/01454237967846880639noreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-26258183783587445672014-06-27T10:47:54.565+01:002014-06-27T10:47:54.565+01:00I don't think I feel the same way.
If there...I don't think I feel the same way.<br />If there's little difference between forEach and the for(){...} construct, what's the harm in adding the default method ? As for spliterator, if Iterables are "things that can be iterated over", why wouldn't we want to use them as Streams ? Maybe that's because I still see streams as an improved way of iterating...<br /><br />Anyway, I fail to see how Iterable now imposes the property of "being a Collection". Stream contents could be generated on-the-fly, just like iterator contents.kwakeroninoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-83675997854331319682014-06-27T08:47:18.919+01:002014-06-27T08:47:18.919+01:00I'm with Oleg, what exactly is the problem wit...I'm with Oleg, what exactly is the problem with having these default methods defined? It's not as if they enable clients of your object to do anything they couldn't otherwise do. I guess maybe they clutter the interface?Michaelnoreply@blogger.comtag:blogger.com,1999:blog-741750605858169835.post-7597414085200708442014-06-27T00:21:00.334+01:002014-06-27T00:21:00.334+01:00i'm not sure i understood how those two additi...i'm not sure i understood how those two additional methods affect users of your apiOlegYchhttps://www.blogger.com/profile/16962595061587595984noreply@blogger.com