\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{carlmig} \pdfinfo{ /Title (java-8-streams.pdf) /Creator (Cheatography) /Author (carlmig) /Subject (Java 8 Streams Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{A3D4A5} \definecolor{LightBackground}{HTML}{F3F9F3} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Java 8 Streams Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{carlmig} via \textcolor{DarkBackground}{\uline{cheatography.com/67226/cs/16854/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}carlmig \\ \uline{cheatography.com/carlmig} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 2nd September, 2020.\\ Updated 30th August, 2018.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{x{2.88 cm} x{5.12 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Specialized Streams}} \tn % Row 0 \SetRowColor{LightBackground} IntStream & for `int` elements \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} DoubleStream & for `double` elements \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} LongStream & for `long` elements \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{It has better performance to use these specialized streams when using numeric data types, because there is no boxing/unboxing} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{1.04 cm} x{6.96 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Suppress elements}} \tn % Row 0 \SetRowColor{LightBackground} limit & `.limit(5)`\{\{nl\}\}will limit the result to the first 5 elements \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} skip & `.skip(5)`\{\{nl\}\}will skip the first 5 elements \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \seqsplit{filter} & `.filter(e -\textgreater{} e.getSalary() \textgreater{} 200000)`\{\{nl\}\}will keep the elements that satisfy the given predicate. In this case, all elements that have salary above 200000 \tn % Row Count 9 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.36 cm} x{6.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Comparing elements}} \tn % Row 0 \SetRowColor{LightBackground} \seqsplit{distinct} & `.distinct()` \{\{nl\}\} will compare the elements in the stream using equals() and eliminate duplicates \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \seqsplit{sorted} & `.sorted((e1, e2) -\textgreater{} \seqsplit{e1.getName().compareTo(e2.getName()))`} \{\{nl\}\} will sort the elements with the given comparator. Elements must be Comparable. \tn % Row Count 9 (+ 5) % Row 2 \SetRowColor{LightBackground} min & Similar to sorted, but it will find the min element according to the given comparator \tn % Row Count 12 (+ 3) % Row 3 \SetRowColor{white} max & Similar to sorted, but it will find the max element according to the given comparator \tn % Row Count 15 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Apply a function to each element}} \tn % Row 0 \SetRowColor{LightBackground} map & `.map(employeeRepository::findById)`\{\{nl\}\} will apply the given function and substitute the elements in the stream for new elements. In this case, it received a stream of employee IDs and returned a stream of Employee objects \tn % Row Count 12 (+ 12) % Row 1 \SetRowColor{white} \{\{nobreak\}\}mapToDouble \{\{nl\}\} mapToInt \{\{nl\}\} mapToLong & similar to {\emph{map}}, but the function converts the element to the specified primitive type, resulting in a specialized stream IntStream, DoubleStream or LongStream \tn % Row Count 21 (+ 9) % Row 2 \SetRowColor{LightBackground} flatMap & similar to map, but the number of elements resulting may be different. It's normally used to convert a List of List into a single list with all the elements \tn % Row Count 29 (+ 8) % Row 3 \SetRowColor{white} peek & `.peek(e -\textgreater{} \seqsplit{e.salaryIncrement(10.0))`} \{\{nl\}\} will apply the give function to all elements in the list, but doesn't substitute the elements in the list \tn % Row Count 37 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.44 cm} x{6.56 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Reduce elements to single value}} \tn % Row 0 \SetRowColor{LightBackground} reduce & `.reduce(0.0, Double::sum)` \{\{nl\}\} will return a single value. It starts with the identity value (0.0) and applies the given function to each element in the array. In this case it's summing all elements, one by one \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} \seqsplit{allMatch} & `.allMatch(i -\textgreater{} i \% 2 == 0);` \{\{nl\}\} will check if all elements match the given condition. If so, returns true, else returns false \tn % Row Count 12 (+ 5) % Row 2 \SetRowColor{LightBackground} \seqsplit{anyMatch} & `.anyMatch(i -\textgreater{} i \% 2 == 0);` \{\{nl\}\} will check if one of the elements match the given condition. If so, returns true, else returns false \tn % Row Count 17 (+ 5) % Row 3 \SetRowColor{white} \seqsplit{noneMatch} & `.noneMatch(i -\textgreater{} i \% 2 == 0);` \{\{nl\}\} will check if no elements match the given condition. If so, returns true, else returns false \tn % Row Count 22 (+ 5) % Row 4 \SetRowColor{LightBackground} \seqsplit{findFirst} & `.findFirst()` \{\{nl\}\} will return an Optional with the first element in the stream \tn % Row Count 25 (+ 3) % Row 5 \SetRowColor{white} \seqsplit{forEach} & `forEach(e -\textgreater{} \seqsplit{e.salaryIncrement(10.0))`} \{\{nl\}\} will apply the given function to each element in the stream, but it's a terminal operation and returns void \tn % Row Count 30 (+ 5) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{1.44 cm} x{6.56 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Reduce elements to single value (cont)}} \tn % Row 6 \SetRowColor{LightBackground} count & `.count()` \{\{nl\}\} outputs the number of elements in the stream \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2.08 cm} x{5.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Collect elements}} \tn % Row 0 \SetRowColor{LightBackground} toList & \seqsplit{`collect(Collectors.toList())`} \{\{nl\}\} gather all elements in the stream into a List \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} toSet & \seqsplit{`collect(Collectors.toSet())`} \{\{nl\}\} gather all elements in the stream into a Set \tn % Row Count 6 (+ 3) % Row 2 \SetRowColor{LightBackground} \seqsplit{toCollection} & \seqsplit{`collect(Collectors.toCollection(Vector::new))`} \{\{nl\}\} gather all elements in the list in an arbitrary Collection \tn % Row Count 10 (+ 4) % Row 3 \SetRowColor{white} joining & \seqsplit{`collect(Collectors.joining("}, ")).toString()` \{\{nl\}\} will join String elements with the given separator and return the aggregated String \tn % Row Count 15 (+ 5) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{summarizingDouble} \tn % Row Count 16 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{summaryStatistics} \tn % Row Count 17 (+ 1) % Row 6 \SetRowColor{LightBackground} \seqsplit{partitioningBy} & \seqsplit{`.collect(Collectors.partitioningBy(s} -\textgreater{} s.getGrade() \textgreater{}= PASS\_THRESHOLD))` \{\{nl\}\} will partition the data into 2 categories based on the given condition. The result will be a Map\textless{}Boolean, List\textless{}Student\textgreater{}\textgreater{} \tn % Row Count 24 (+ 7) % Row 7 \SetRowColor{white} \seqsplit{groupingBy} & \seqsplit{`.collect(Collectors.groupingBy(Employee::getDepartment));`} \{\{nl\}\} will group the elements into categories based on the function. The result will be a Map\textless{}Department, List\textless{}Employee\textgreater{}\textgreater{} \tn % Row Count 31 (+ 7) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{x{2.08 cm} x{5.92 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Collect elements (cont)}} \tn % Row 8 \SetRowColor{LightBackground} mapping & \seqsplit{`mapping(Person::getLastName}, toSet())` \{\{nl\}\} it receives a function to be applied to all elements and way of collecting downstream the elements. In this case, it will get the last name of all persons and add them to a set \tn % Row Count 8 (+ 8) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{reducing} \tn % Row Count 9 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}