-
Notifications
You must be signed in to change notification settings - Fork 0
/
component---src-pages-content-articles-todo-cheatsheets-java-8-index-md-07e5c34900b894dc0076.js
2 lines (2 loc) · 102 KB
/
component---src-pages-content-articles-todo-cheatsheets-java-8-index-md-07e5c34900b894dc0076.js
1
2
(window.webpackJsonp=window.webpackJsonp||[]).push([[22],{Qncy:function(e,a,t){"use strict";t.r(a),t.d(a,"_frontmatter",(function(){return s})),t.d(a,"default",(function(){return m}));t("rGqo"),t("yt8O"),t("Btvt"),t("RW0V"),t("91GP"),t("q1tI");var n=t("7ljp");var s={templateKey:"article",published:!1,displayComments:!0,showAuthorInfo:!1,author:"narramadan",title:"JAVA 8 - Cheat Sheet",description:"Syntax cheatsheet for Java8",date:"2019-09-15T23:46:37.121Z",updated:"2019-09-15T23:46:37.121Z",cover:"../../../images/blog-banners/cheatsheet-banner-1200x690.png",category:"cheetsheets",tags:["java8","cheatsheets"],keywords:["java8","lamda","lamda expressions","Default Lambda in Java8","Java Time API","Collection API improvements","Concurrency API improvements","Java IO improvements","Functional Interfaces and Lambda Expressions","default and static methods in Interfaces","static methods","forEach()","Java IO improvements"]},c={_frontmatter:s};function m(e){var a=e.components,t=function(e,a){if(null==e)return{};var t,n,s={},c=Object.keys(e);for(n=0;n<c.length;n++)t=c[n],a.indexOf(t)>=0||(s[t]=e[t]);return s}(e,["components"]);return Object(n.mdx)("wrapper",Object.assign({},c,t,{components:a,mdxType:"MDXLayout"}),Object(n.mdx)("h2",null,"Lambda Expression"),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"int")," a",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," a ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"*")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"2"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// Calculate the double of a"),"\na ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," a ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"*")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"2"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// or simply without type")))),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"a",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," b",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," a ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"+")," b",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// Sum of 2 parameters")))),Object(n.mdx)("p",null,"If the lambda is more than one expression we can use ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"{ }")," and ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"return")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"x",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," y",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"{"),"\n\t",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"int")," sum ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," x ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"+")," y",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n\t",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"int")," avg ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," sum ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"/")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"2"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n\t",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"return")," avg",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"}")))),Object(n.mdx)("p",null,"A lambda expression cannot stand alone in Java, it need to be associated to a functional interface."),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"interface")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"MyMath")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"{"),"\n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"int")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"getDoubleOf"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"int")," a",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"}"),"\n\t\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"MyMath")," d ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," a ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," a ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"*")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"2"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// associated to the interface"),"\nd",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"getDoubleOf"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"4"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// is 8")))),Object(n.mdx)("hr",null),Object(n.mdx)("p",null,"All examples with “list” use :"),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"List"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," list ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"["),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Bohr"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Darwin"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Galilei"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Tesla"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Einstein"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Newton"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"]")))),Object(n.mdx)("h2",null,"Collections"),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"sort")," ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"sort(list, comparator)")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),"list",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"sort"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"a",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," b",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," a",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"length"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"-")," b",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"length"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),"\nlist",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"sort"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Comparator"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"comparing"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"length"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// same"),"\nlist",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"sort"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Comparator"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"comparing"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"::"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"length"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// same"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"//> [Bohr, Tesla, Darwin, Newton, Galilei, Einstein]")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"removeIf")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),"list",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"removeIf"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"w ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," w",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"length"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"<")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"6"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"//> [Darwin, Galilei, Einstein, Newton]")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"merge"),"\n",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"merge(key, value, remappingFunction)")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Map"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," names ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"new")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"HashMap"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">")),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\nnames",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"put"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"Albert"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"Ein?"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\nnames",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"put"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"Marie"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"Curie"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\nnames",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"put"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"Max"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"Plank"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),'// Value "Albert" exists'),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// {Marie=Curie, Max=Plank, Albert=Einstein}"),"\nnames",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"merge"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"Albert"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"stein"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"old",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," val",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," old",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"substring"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"0"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"3"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"+")," val",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),'// Value "Newname" don\'t exists'),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// {Marie=Curie, Newname=stein, Max=Plank, Albert=Einstein}"),"\nnames",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"merge"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"Newname"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"stein"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"old",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," val",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," old",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"substring"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"0"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"3"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"+")," val",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")))),Object(n.mdx)("h2",null,"Method Expressions ",Object(n.mdx)("code",Object.assign({parentName:"h2"},{className:"language-text"}),"Class::staticMethod")),Object(n.mdx)("p",null,"Allows to reference methods (and constructors) without executing them"),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// Lambda Form:"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"getPrimes"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"numbers",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," a ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"StaticMethod"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"isPrime"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"a",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// Method Reference:"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"getPrimes"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"numbers",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"StaticMethod"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"::"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"isPrime"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")))),Object(n.mdx)("table",null,Object(n.mdx)("thead",{parentName:"table"},Object(n.mdx)("tr",{parentName:"thead"},Object(n.mdx)("th",Object.assign({parentName:"tr"},{align:null}),"Method Reference"),Object(n.mdx)("th",Object.assign({parentName:"tr"},{align:null}),"Lambda Form"))),Object(n.mdx)("tbody",{parentName:"table"},Object(n.mdx)("tr",{parentName:"tbody"},Object(n.mdx)("td",Object.assign({parentName:"tr"},{align:null}),Object(n.mdx)("code",Object.assign({parentName:"td"},{className:"language-text"}),"StaticMethod::isPrime")),Object(n.mdx)("td",Object.assign({parentName:"tr"},{align:null}),Object(n.mdx)("code",Object.assign({parentName:"td"},{className:"language-text"}),"n -> StaticMethod.isPrime(n)"))),Object(n.mdx)("tr",{parentName:"tbody"},Object(n.mdx)("td",Object.assign({parentName:"tr"},{align:null}),Object(n.mdx)("code",Object.assign({parentName:"td"},{className:"language-text"}),"String::toUpperCase")),Object(n.mdx)("td",Object.assign({parentName:"tr"},{align:null}),Object(n.mdx)("code",Object.assign({parentName:"td"},{className:"language-text"}),"(String w) -> w.toUpperCase()"))),Object(n.mdx)("tr",{parentName:"tbody"},Object(n.mdx)("td",Object.assign({parentName:"tr"},{align:null}),Object(n.mdx)("code",Object.assign({parentName:"td"},{className:"language-text"}),"String::compareTo")),Object(n.mdx)("td",Object.assign({parentName:"tr"},{align:null}),Object(n.mdx)("code",Object.assign({parentName:"td"},{className:"language-text"}),"(String s, String t) -> s.compareTo(t)"))),Object(n.mdx)("tr",{parentName:"tbody"},Object(n.mdx)("td",Object.assign({parentName:"tr"},{align:null}),Object(n.mdx)("code",Object.assign({parentName:"td"},{className:"language-text"}),"System.out::println")),Object(n.mdx)("td",Object.assign({parentName:"tr"},{align:null}),Object(n.mdx)("code",Object.assign({parentName:"td"},{className:"language-text"}),"x -> System.out.println(x)"))),Object(n.mdx)("tr",{parentName:"tbody"},Object(n.mdx)("td",Object.assign({parentName:"tr"},{align:null}),Object(n.mdx)("code",Object.assign({parentName:"td"},{className:"language-text"}),"Double::new")),Object(n.mdx)("td",Object.assign({parentName:"tr"},{align:null}),Object(n.mdx)("code",Object.assign({parentName:"td"},{className:"language-text"}),"n -> new Double(n)"))),Object(n.mdx)("tr",{parentName:"tbody"},Object(n.mdx)("td",Object.assign({parentName:"tr"},{align:null}),Object(n.mdx)("code",Object.assign({parentName:"td"},{className:"language-text"}),"String[]::new")),Object(n.mdx)("td",Object.assign({parentName:"tr"},{align:null}),Object(n.mdx)("code",Object.assign({parentName:"td"},{className:"language-text"}),"(int n) -> new String[n]"))))),Object(n.mdx)("h2",null,"Streams"),Object(n.mdx)("p",null,"Similar to collections, but"),Object(n.mdx)("ul",null,Object(n.mdx)("li",{parentName:"ul"},"They don’t store their own data"),Object(n.mdx)("li",{parentName:"ul"},"The data comes from elsewhere (collection, file, db, web, …)"),Object(n.mdx)("li",{parentName:"ul"},Object(n.mdx)("em",{parentName:"li"},"immutable")," (produce new streams)"),Object(n.mdx)("li",{parentName:"ul"},Object(n.mdx)("em",{parentName:"li"},"lazy")," (only computes what is necessary !)")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),'// Will compute just 3 "filter"'),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," longNames ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," list\n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"filter"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"length"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),">")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"8"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),"\n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"limit"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"3"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"Create a new stream")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"Integer"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," stream ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"of"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"1"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"2"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"3"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"5"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"7"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"11"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," stream ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"of"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"Jazz"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"Blues"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"Rock"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," stream ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"of"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"myArray",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// or from an array"),"\nlist",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// or from a list"),"\n\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// Infinit stream [0; inf["),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"Integer"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," integers ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"iterate"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"0"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"+")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"1"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"Collecting results")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// Collect into an array (::new is the constructor reference)"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"["),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"]")," myArray ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," stream",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"toArray"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"["),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"]"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"::"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"new"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// Collect into a List or Set"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"List"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," myList ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," stream",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"collect"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Collectors"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"toList"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Set"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," mySet ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," stream",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"collect"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Collectors"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"toSet"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// Collect into a String"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"String")," str ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," list",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"collect"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Collectors"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"joining"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'", "'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"map")," ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"map(mapper)"),"\nApplying a function to each element"),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),'// Apply "toLowerCase" for each element'),"\nres ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," stream",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"map"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"w ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," w",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"toLowerCase"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\nres ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," stream",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"map"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"::"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"toLowerCase"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"//> bohr darwin galilei tesla einstein newton"),"\n\nres ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"of"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"1"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),","),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"2"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),","),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"3"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),","),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"4"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),","),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"5"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"map"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"x ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," x ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"+")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"1"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"//> 2 3 4 5 6")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"filter")," ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"filter(predicate)"),"\nRetains elements that match the predicate"),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),'// Filter elements that begin with "E"'),"\nres ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," stream",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"filter"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"substring"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"0"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"1"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"equals"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"E"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"//> Einstein"),"\n\nres ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"of"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"1"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),","),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"2"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),","),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"3"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),","),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"4"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),","),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"5"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"filter"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"x ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," x ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"<")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"3"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"//> 1 2")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"reduce"),"\nReduce the elements to a single value"),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"String")," reduced ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," stream\n\t",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"reduce"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'""'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"acc",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," el",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," acc ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"+")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"|"')," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"+")," el",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"//> |Bohr|Darwin|Galilei|Tesla|Einstein|Newton")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"limit")," ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"limit(maxSize)"),"\nThe n first elements"),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),"res ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," stream",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"limit"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"3"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"//> Bohr Darwin Galilei")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"skip"),"\nDiscarding the first n elements"),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),"res ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," strem",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"skip"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"2"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// skip Bohr and Darwin"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"//> Galilei Tesla Einstein Newton")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"distinct"),"\nRemove duplicated elemetns"),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),"res ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"of"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"1"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),","),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"0"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),","),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"0"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),","),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"1"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),","),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"0"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),","),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"1"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"distinct"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"//> 1 0")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"sorted"),"\nSort elements (must be ",Object(n.mdx)("em",{parentName:"p"},"Comparable"),")"),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),"res ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," stream",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"sorted"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"//> Bohr Darwin Einstein Galilei Newton Tesla ")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"allMatch")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),'// Check if there is a "e" in each elements'),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"boolean")," res ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," words",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"allMatch"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"contains"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'"e"'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")))),Object(n.mdx)("p",null,"anyMatch: Check if there is a “e” in an element\nnoneMatch: Check if there is no “e” in elements"),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"parallel"),"\nReturns an equivalent stream that is parallel"),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"findAny"),"\nfaster than findFirst on parallel streams"),Object(n.mdx)("h3",null,"Primitive-Type Streams"),Object(n.mdx)("p",null,"Wrappers (like ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"Stream<Integer>"),") are inefficients. It requires a lot of unboxing and boxing for each element. Better to use ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"IntStream"),", ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"DoubleStream"),", etc."),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"Creation")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"IntStream")," stream ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"IntStream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"of"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"1"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"2"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"3"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"5"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"7"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\nstream ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"IntStream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"of"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"myArray",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// from an array"),"\nstream ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"IntStream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"range"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"5"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"80"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// range from 5 to 80"),"\n\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Random")," gen ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"new")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Random"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"IntStream")," rand ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"gen"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"1"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"9"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// stream of randoms")))),Object(n.mdx)("p",null,"Use ",Object(n.mdx)("em",{parentName:"p"},"mapToX")," (mapToObj, mapToDouble, etc.) if the function yields Object, double, etc. values."),Object(n.mdx)("h3",null,"Grouping Results"),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"Collectors.groupingBy")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// Groupe by length"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Map"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"Integer"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"List"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," groups ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," stream\n\t",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"collect"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Collectors"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"groupingBy"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"w ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," w",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"length"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"//> 4=[Bohr], 5=[Tesla], 6=[Darwin, Newton], ...")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"Collectors.toSet")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// Same as before but with Set"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),".")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Collectors"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"groupingBy"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"\n\tw ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," w",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"substring"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"0"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"1"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Collectors"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"toSet"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),".")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"Collectors.counting"),"\nCount the number of values in a group"),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"Collectors.summing__"),"\n",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"summingInt"),", ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"summingLong"),", ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"summingDouble")," to sum group values"),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"Collectors.averaging__"),"\n",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"averagingInt"),", ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"averagingLong"),", … "),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// Average length of each element of a group"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Collectors"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"averagingInt"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"::"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"length"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")))),Object(n.mdx)("p",null,Object(n.mdx)("em",{parentName:"p"},"PS"),": Don’t forget Optional (like ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"Map<T, Optional<T>>"),") with some Collection methods (like ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"Collectors.maxBy"),")."),Object(n.mdx)("h3",null,"Parallel Streams"),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"Creation")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," parStream ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," list",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"parallelStream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," parStream ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Stream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"of"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"myArray",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"parallel"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")))),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"unordered"),"\nCan speed up the ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"limit")," or ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"distinct")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),"stream",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"parallelStream"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"unordered"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"distinct"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")))),Object(n.mdx)("p",null,Object(n.mdx)("em",{parentName:"p"},"PS"),": Work with the streams library. Eg. use ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"filter(x -> x.length() < 9)")," instead of a ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"forEach")," with an ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"if"),"."),Object(n.mdx)("h2",null,"Optional"),Object(n.mdx)("p",null,"In Java, it is common to use null to denote absence of result.\nProblems when no checks: ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"NullPointerException"),"."),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// Optional<String> contains a string or nothing"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Optional"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," res ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," stream\n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"filter"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"w ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," w",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"length"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),">")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"10"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),"\n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"findFirst"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),'// length of the value or "" if nothing'),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"int")," length ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"=")," res",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"orElse"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token string"}),'""'),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"length"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// run the lambda if there is a value"),"\nres",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"ifPresent"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"v ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"->")," results",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"add"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"v",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")))),Object(n.mdx)("p",null,"Return an Optional"),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Optional"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"Double"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"squareRoot"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"double")," x",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"{"),"\n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"if")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"x ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),">=")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token number"}),"0"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"{")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"return")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Optional"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"of"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Math"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"sqrt"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"x",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"}"),"\n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"else")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"{")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"return")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Optional"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"empty"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"}"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"}")))),Object(n.mdx)("hr",null),Object(n.mdx)("p",null,Object(n.mdx)("strong",{parentName:"p"},"Note on inferance limitations")),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token keyword"}),"interface")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Pair"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"A"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"B"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"))," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"{"),"\n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"A")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"first"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"B")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"second"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),";"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"}")))),Object(n.mdx)("p",null,"A steam of type ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"Stream<Pair<String, Long>>")," :"),Object(n.mdx)("ul",null,Object(n.mdx)("li",{parentName:"ul"},Object(n.mdx)("code",Object.assign({parentName:"li"},{className:"language-text"}),"stream.sorted(Comparator.comparing(Pair::first)) // ok")),Object(n.mdx)("li",{parentName:"ul"},Object(n.mdx)("code",Object.assign({parentName:"li"},{className:"language-text"}),"stream.sorted(Comparator.comparing(Pair::first).thenComparing(Pair::second)) // dont work"))),Object(n.mdx)("p",null,"Java cannot infer type for the ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),".comparing(Pair::first)")," part and fallback to Object, on which ",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),"Pair::first")," cannot be applied."),Object(n.mdx)("p",null,"The required type for the whole expression cannot be propagated through the method call (",Object(n.mdx)("code",Object.assign({parentName:"p"},{className:"language-text"}),".thenComparing"),") and used to infer type of the first part."),Object(n.mdx)("p",null,"Type ",Object(n.mdx)("em",{parentName:"p"},"must")," be given explicitly."),Object(n.mdx)("div",{className:"gatsby-highlight","data-language":"java"},Object(n.mdx)("pre",Object.assign({parentName:"div"},{className:"language-java"}),Object(n.mdx)("code",Object.assign({parentName:"pre"},{className:"language-java"}),"stream",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"sorted"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),"\n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Comparator"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token generics"}),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"Pair"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),"<"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"Long"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),",")," ",Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token class-name"}),"String"),Object(n.mdx)("span",Object.assign({parentName:"span"},{className:"token punctuation"}),">")),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"comparing"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Pair"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"::"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"first"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),"\n ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"."),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"thenComparing"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),"("),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token class-name"}),"Pair"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token operator"}),"::"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token function"}),"second"),Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")"),"\n",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token punctuation"}),")")," ",Object(n.mdx)("span",Object.assign({parentName:"code"},{className:"token comment"}),"// ok")))),Object(n.mdx)("hr",null),Object(n.mdx)("p",null,"This cheat sheet was based on the lecture of Cay Horstmann\n",Object(n.mdx)("a",Object.assign({parentName:"p"},{href:"http://horstmann.com/heig-vd/spring2015/poo/"}),"http://horstmann.com/heig-vd/spring2015/poo/")))}m.isMDXComponent=!0}}]);
//# sourceMappingURL=component---src-pages-content-articles-todo-cheatsheets-java-8-index-md-07e5c34900b894dc0076.js.map