{"id":304,"date":"2016-12-16T17:57:38","date_gmt":"2016-12-16T17:57:38","guid":{"rendered":"https:\/\/sidroniolima.com.br\/blog\/?p=304"},"modified":"2016-12-16T18:16:04","modified_gmt":"2016-12-16T18:16:04","slug":"stream-intermediate-operations","status":"publish","type":"post","link":"https:\/\/sidroniolima.com.br\/blog\/2016\/12\/16\/stream-intermediate-operations\/","title":{"rendered":"Stream intermediate operations"},"content":{"rendered":"\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=sidroniolima\" class=\"twitter-share-button\">Tweet<\/a><\/div>\n<h3><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter\" src=\"https:\/\/liamrainsford.files.wordpress.com\/2011\/06\/woodland-stream.jpg\" alt=\"Representation of Stream as a cascade\" width=\"140\" height=\"204\" \/><\/h3>\n<p>There are a few operations that processes a stream and output stream as source for terminal operations. Above, we can see some of these methods, yours descriptions and examples. Just short, just directly.<\/p>\n<p><!--more--><\/p>\n<h3>Filtering and Mapping<\/h3>\n<h4>discinct()<\/h4>\n<ul>\n<li>Returns a stream with no duplicate elements.<\/li>\n<\/ul>\n<h4>filter(Predicate p)<\/h4>\n<ul>\n<li>Returns a stream with only those elements that return true for the Predicate.<\/li>\n<\/ul>\n<h4>map(Function f)<\/h4>\n<ul>\n<li>Returns a stream where the given Function is applied to each element on the input stream.<\/li>\n<\/ul>\n<h4><strong>mapToInt(), mapToDouble(), mapToLong()<\/strong><\/h4>\n<ul>\n<li>Like map(), but producing streams of primitives rather than objects<\/li>\n<\/ul>\n<h4>Maps and FlatMaps<\/h4>\n<h5>Map:<\/h5>\n<ul>\n<li>Take a single value as input and generates a single value of output stream. 1-to-1 mapping.<\/li>\n<\/ul>\n<h5>FlatMap:<\/h5>\n<ul>\n<li>Streams of stream. Take the input stream, generates a stream for each element and join the result.<\/li>\n<\/ul>\n<p><em>Ex.:<\/em><\/p>\n<p style=\"padding-left: 30px;\"><em>List&lt;String&gt; output = reader.lines().flatMap(line -&gt; Stream.of(line.split(REGEXP))).filger(word -&gt; word.length() &gt; 0).collect(Collectors.toList());<\/em><\/p>\n<h3>Restricting the size of a stream<\/h3>\n<h4>skip(long n)<\/h4>\n<h4>limit(long n)<\/h4>\n<p style=\"padding-left: 30px;\"><em>Ex.: String output = bufferedReader.lines().skip(2).limit(2).collect(Collectors.joining());<\/em><\/p>\n<h3>Sorting and unsorting<\/h3>\n<h4>sorted(Comparator c)<\/h4>\n<ul>\n<li>sorted(): based on natural order.<\/li>\n<\/ul>\n<h4>unordered()<\/h4>\n<ul>\n<li>can improve efficiency of operations like distinct() and groupingby()<\/li>\n<\/ul>\n<h4>Observing Stream Elements<\/h4>\n<h4>Peek<\/h4>\n<ul>\n<li>Not modify the elements of the stream. Each of element is passed by the accpet() metodh of the Consumer.<\/li>\n<\/ul>\n\n<div class=\"twitter-share\"><a href=\"https:\/\/twitter.com\/intent\/tweet?via=sidroniolima\" class=\"twitter-share-button\">Tweet<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are a few operations that processes a stream and output stream as source for terminal operations. Above, we can see some of these methods, yours descriptions and examples. Just short, just directly.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[27],"tags":[57],"class_list":["post-304","post","type-post","status-publish","format-standard","hentry","category-java","tag-stream"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/posts\/304","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/comments?post=304"}],"version-history":[{"count":3,"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/posts\/304\/revisions"}],"predecessor-version":[{"id":307,"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/posts\/304\/revisions\/307"}],"wp:attachment":[{"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/media?parent=304"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/categories?post=304"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sidroniolima.com.br\/blog\/wp-json\/wp\/v2\/tags?post=304"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}