{"id":747,"date":"2015-03-06T15:05:29","date_gmt":"2015-03-06T14:05:29","guid":{"rendered":"http:\/\/benjiweber.co.uk\/blog\/?p=747"},"modified":"2015-03-06T15:05:52","modified_gmt":"2015-03-06T14:05:52","slug":"adding-tolist-to-java-streams","status":"publish","type":"post","link":"https:\/\/benjiweber.co.uk\/blog\/2015\/03\/06\/adding-tolist-to-java-streams\/","title":{"rendered":"Adding toList() to Java Streams"},"content":{"rendered":"<p class=\"lead\">The Java Streams API is lovely, but there are a few operations that One repeats over and over again which could be easier.<\/p>\n<p>One example of this is Collecting to a List. <\/p>\n<pre lang=\"java\">\r\nList<String> input = asList(\"foo\", \"bar\");\r\nList<String> filtered = input\r\n  .stream()\r\n  .filter(s -> s.startsWith(\"f\"))\r\n  .collect(Collectors.toList());\r\n<\/pre>\n<p>There are a few other annoyances too. For example I find myself using flatMap almost exclusively with methods that return a Collection not a Stream. Which means we have to do<\/p>\n<pre lang=\"java\">\r\nList<String> result = input\r\n  .stream()\r\n  .flatMap(s -> Example.threeOf(s).stream())\r\n  .collect(Collectors.toList());\r\n\r\n\/\/...\r\n\r\npublic static List<String> threeOf(String input) {\r\n  return asList(input, input, input);\r\n}\r\n<\/pre>\n<p>It would be nice to have a convenience method to allow us to use a method reference in this very common scenario<\/p>\n<pre lang=\"java\">\r\nList<String> result = input\r\n  .stream()\r\n  .flatMap(Example::threeOf) \/\/ Won't compile, threeOf returns a collection, not a stream \r\n  .collect(Collectors.toList());\r\n<\/pre>\n<p>Benjamin Winterberg has written a good guide to <a href=\"http:\/\/winterbe.com\/posts\/2015\/03\/05\/fixing-java-8-stream-gotchas-with-intellij-idea\/\">getting intellij to generate these for us <\/a>. But we could extend the API ourselves.<\/p>\n<p>Extending the Streams API is a little tricky as it is chainable, but it is possible. We can use the <a href=\"http:\/\/benjiweber.co.uk\/blog\/2014\/04\/14\/java-forwarding-interface-pattern\/\">Forwarding Interface Pattern<\/a> to add methods to List, but we want to add a new method to Stream. <\/p>\n<p>What we can end up with is <\/p>\n<pre lang=\"java\">\r\nEnhancedList<String> input = () -> asList(\"foo\",\"bar\");\r\nList<String> filtered = list\r\n  .stream()\r\n  .filter(s -> s.startsWith(\"f\"))\r\n  .toList();\r\n\r\n<\/pre>\n<p>To do this we can first use the Forwarding Interface Pattern to create an enhanced Stream with our toList method. Starting with a ForwardingStrem<\/p>\n<pre lang=\"java\">\r\ninterface ForwardingStream<T> extends Stream<T> {\r\n\r\n  Stream<T> delegate();\r\n\r\n  default Stream<T> filter(Predicate<? super T> predicate) {\r\n    return delegate().filter(predicate);\r\n  }\r\n  \r\n  \/\/ Other methods omitted for brevity\r\n        \r\n}\r\n<\/pre>\n<p>Now, since Stream provides a chainable api, with methods that return Streams &#8211; when we implement our EnhancedStream we need to change the subset of methods that return a Stream to return our EnhancedStream. <\/p>\n<pre lang=\"java\">\r\ninterface EnhancedStream<T> extends ForwardingStream<T> {\r\n\r\n  default EnhancedStream<T> filter(Predicate<? super T> predicate) {\r\n    return () -> ForwardingStream.super.filter(predicate);\r\n  }\r\n  \r\n  \/\/ Other methods omitted for brevity\r\n\r\n}\r\n<\/pre>\n<p>Note that the filter method already exists on Stream and ForwardingStream, but as Java supports <a href=\"https:\/\/blogs.oracle.com\/sundararajan\/entry\/covariant_return_types_in_java\">covariant return types<\/a> we can override it and change the returntype to a more specific type (In this case from Stream to EnhancedStream).<\/p>\n<p>You may also notice the lambda returned from the filter() method. Since ForwardingStream is a single method interface with just a Stream<T> delegate() method it is compatible with a lambda that supplies a delegate Stream. Equivalent to a Supplier<Stream>. EnhancedStream extends ForwardingStream and doesn&#8217;t declare any additional abstract methods, so we can return lambdas from each method that needs to return a Stream, delegating to the ForwardingStream. The ForwardingStream.super.filter() syntax allows us to call the implementation already defined in ForwardingStream explicitly.<\/p>\n<p>Now we can add our additional behaviour to the EnhancedStream. Let&#8217;s add the toList() method, and a new flatMapCollection<\/p>\n<pre lang=\"java\">\r\ninterface EnhancedStream<T> extends ForwardingStream<T> {\r\n  default List<T> toList() {\r\n    return collect(Collectors.toList());\r\n  }\r\n\r\n  default <R> EnhancedStream<R> flatMapCollection(Function<? super T, ? extends Collection<? extends R>> mapper) {\r\n    return flatMap(mapper.andThen(Collection::stream));\r\n  }\r\n\r\n  \/\/ Other methods omitted for brevity\r\n\r\n}\r\n<\/pre>\n<p>Finally, we&#8217;ll need to hook up the Stream to our List. We can use the same technique of a forwarding interface which overrides a method and specifies a more specific return type.<\/p>\n<pre lang=\"java\">\r\n\r\ninterface EnhancedList<T> extends ForwardingList<T> {\r\n  default EnhancedStream<T> stream() {\r\n    return () -> ForwardingList.super.stream();\r\n  }\r\n}\r\n\r\ninterface ForwardingList<T> extends List<T> {\r\n  List<T> delegate();\r\n\r\n  default int size() {\r\n    return delegate().size();\r\n  }\r\n        \r\n  \/\/ All the other methods omitted for brevity \r\n}\r\n<\/pre>\n<p>So now we can put it all together. EnhancedList is a single method interface so compatible with any existing List in our code, we just wrap it in a lambda, and then we can flatMapCollection() and toList(). The following prints &#8220;foo&#8221; three times.<\/p>\n<pre lang=\"java\">\r\nEnhancedList<String> input = () -> asList(\"foo\", \"bar\");\r\nList<String> result = input\r\n  .stream()\r\n  .flatMapCollection(Example::threeOf) \r\n  .toList();\r\n  \r\nresult.forEach(System.out::println);\r\n\r\n\/\/ ...\r\n\r\npublic static List<String> threeOf(String input) {\r\n  return asList(input, input, input);\r\n}\r\n<\/pre>\n<p><a href=\"https:\/\/github.com\/benjiman\/scratch\/blob\/master\/src\/main\/java\/scratch\/EnhancedStreams.java\">Here&#8217;s the unabbreviated code.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Java Streams API is lovely, but there are a few operations that One repeats over and over again which could be easier. One example of this is Collecting to a List. List input = asList(&#8220;foo&#8221;, &#8220;bar&#8221;); List filtered = input .stream() .filter(s -> s.startsWith(&#8220;f&#8221;)) .collect(Collectors.toList()); There are a few other annoyances too. For example&#8230;  <a href=\"https:\/\/benjiweber.co.uk\/blog\/2015\/03\/06\/adding-tolist-to-java-streams\/\" class=\"more-link\" title=\"Read Adding toList() to Java Streams\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/benjiweber.co.uk\/blog\/2015\/03\/06\/adding-tolist-to-java-streams\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Adding toList() to Java Streams - Benji&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"The Java Streams API is lovely, but there are a few operations that One repeats over and over again which could be easier. One example of this is Collecting to a List. List input = asList(&quot;foo&quot;, &quot;bar&quot;); List filtered = input .stream() .filter(s -&gt; s.startsWith(&quot;f&quot;)) .collect(Collectors.toList()); There are a few other annoyances too. For example... Read more &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/benjiweber.co.uk\/blog\/2015\/03\/06\/adding-tolist-to-java-streams\/\" \/>\n<meta property=\"og:site_name\" content=\"Benji&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-03-06T14:05:29+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2015-03-06T14:05:52+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/\",\"name\":\"Benji&#039;s Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/benjiweber.co.uk\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2015\/03\/06\/adding-tolist-to-java-streams\/#webpage\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/2015\/03\/06\/adding-tolist-to-java-streams\/\",\"name\":\"Adding toList() to Java Streams - Benji&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\"},\"datePublished\":\"2015-03-06T14:05:29+00:00\",\"dateModified\":\"2015-03-06T14:05:52+00:00\",\"author\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/benjiweber.co.uk\/blog\/2015\/03\/06\/adding-tolist-to-java-streams\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\",\"name\":\"benji\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/05fb47b31a0b329e1b790074a9b624ef?s=96&d=mm&r=g\",\"caption\":\"benji\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/747"}],"collection":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=747"}],"version-history":[{"count":9,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/747\/revisions"}],"predecessor-version":[{"id":756,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/747\/revisions\/756"}],"wp:attachment":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=747"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=747"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=747"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}