\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{Giang.nd2508} \pdfinfo{ /Title (java-intermediate-collection.pdf) /Creator (Cheatography) /Author (Giang.nd2508) /Subject (Java\_Intermediate: Collection 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}{8CA363} \definecolor{LightBackground}{HTML}{F7F9F5} \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\_Intermediate: Collection Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Giang.nd2508} via \textcolor{DarkBackground}{\uline{cheatography.com/134606/cs/30401/}}} \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}Giang.nd2508 \\ \uline{cheatography.com/giang-nd2508} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 8th January, 2022.\\ Updated 16th February, 2022.\\ 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} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Collection}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{- is a "bag" = unordered lists \newline % Row Count 1 (+ 1) - No direct implementation% Row Count 2 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Collection Methods}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Collection\textless{}String\textgreater{} names = new ArrayList\textless{}\textgreater{}(); \newline \newline - {\bf{Add one element}} \newline names.add("john"); \newline System.out.println(names); //{[}john{]} \newline \newline - {\bf{Add multiple elements}} \newline names.addAll(List.of("james, harry")); \newline System.out.println(names); //{[}james, harry{]} \newline \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline \newline Collection\textless{}String\textgreater{} names = new ArrayList\textless{}\textgreater{}(List.of("john", "james", "harry")); \newline \newline - {\bf{Remove one element}} \newline names.remove("john"); \newline System.out.println(names); //{[}james, harry{]} \newline \newline - {\bf{Remove multiple elements}} \newline names.removeAll(List.of("james", "harry")); \newline System.out.println(names); //{[}john{]} \newline \newline - {\bf{Remove based on a condition}} \newline names.removeIf(s -\textgreater{} s.startsWith("j")); \newline System.out.println(names); //{[}harry{]} \newline \newline - {\bf{Remove all elements}} \newline names.clear(); \newline System.out.println(names); //{[}{]} \newline \newline - {\bf{Remove the differences}} \newline Collection\textless{}String\textgreater{} names2 = new ArrayList\textless{}\textgreater{}(List.of("john", "james", "Steve")); \newline names.retainAll(names2); \newline System.out.println(names); //{[}john, james{]} \newline \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline \newline Collection\textless{}String\textgreater{} names = new ArrayList\textless{}\textgreater{}(List.of("john", "james", "harry")); \newline \newline - {\bf{Print each element}} \newline names.forEach(System.out::println); //john james harry \newline \newline - {\bf{Get size}} \newline System.out.println(names.size()); //3 \newline \newline - {\bf{Check empty}} \newline System.out.println(names.isEmpty()); //false \newline \newline - {\bf{Check contain one element}} \newline System.out.println(names.contains("harry")); //true \newline \newline - {\bf{Check contain multiple elements}} \newline System.out.println(names.containsAll(List.of("harry", "john"))); //true \newline \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline \newline Collection\textless{}String\textgreater{} names = new ArrayList\textless{}\textgreater{}(List.of("john", "james", "harry")); \newline \newline - {\bf{Convert to array with array type}} \newline String{[}{]} strings = names.toArray(new String{[}{]}\{\}); \newline System.out.println(Arrays.toString(strings)); //{[}john, james, harry{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Set}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{- No duplicates \newline % Row Count 1 (+ 1) - Order is not significant \newline % Row Count 2 (+ 1) - No more methods than Collection interface \newline % Row Count 3 (+ 1) • Don't care order → HashSet \newline % Row Count 4 (+ 1) • Keep insertion order → LinkedHashSet \newline % Row Count 5 (+ 1) • Store Enum in a Set → EnumSet \newline % Row Count 6 (+ 1) • When need multi-thread, size small, read more – less write → CopyOnWriteArraySet \newline % Row Count 8 (+ 2) • Auto sort element → TreeSet \newline % Row Count 9 (+ 1) • When need multi-thread + Auto sort → ConcurrentSkipListSet% Row Count 11 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Set Methods}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{- {\bf{Create a set}} \newline Set\textless{}String\textgreater{} uniqueNames = new HashSet\textless{}\textgreater{}(); \newline \newline - {\bf{Create an unmodified set}} \newline Set\textless{}String\textgreater{} uniqueNames = Set.of("b", "a", "c"); \newline System.out.println(uniqueNames); //{[}a, b, c{]} \newline \newline - {\bf{Create a set from a list}} \newline Set\textless{}String\textgreater{} uniqueNames = new HashSet\textless{}\textgreater{}(List.of("a", "b", "a", "c")); \newline System.out.println(uniqueNames); //{[}a, b, c{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{List}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{- A collection in which order is significant \newline % Row Count 1 (+ 1) - Allow duplicate elements \newline % Row Count 2 (+ 1) • Insert and get element fast → ArrayList \newline % Row Count 3 (+ 1) • Insert and remove fast → LinkedList \newline % Row Count 4 (+ 1) • When need multi-thread → CopyOnWriteArrayList% Row Count 6 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{List Methods}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{List contains all methods that defines in collection interface, \newline and some methods work with index \newline \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline \newline List\textless{}String\textgreater{} chars = new ArrayList\textless{}\textgreater{}(List.of("a", "b", "c")); \newline \newline - {\bf{Add an element at position}} \newline chars.add(2, "new"); \newline System.out.println(chars); //{[}a, b, new, c{]} \newline \newline - {\bf{Add multiple elements at position}} \newline chars.addAll(2, List.of("newA", "newB")); \newline System.out.println(chars); //{[}a, b, newA, newB, c{]} \newline \newline - {\bf{Edit an element at position}} \newline chars.set(2, "newA"); \newline System.out.println(chars); //{[}a, b, newA{]} \newline \newline - {\bf{Get an element at position}} \newline System.out.println(chars.get(0)); //a \newline \newline - {\bf{Check index of an element}} \newline System.out.println(chars.indexOf("b")); //1 \newline \newline - {\bf{Remove an element at position}} \newline chars.remove(1); \newline System.out.println(chars); //{[}a, c{]} \newline \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline \newline - {\bf{Create an unmodified list}} \newline List\textless{}String\textgreater{} chars = List.of("a", "b", "c"); \newline \newline - {\bf{Create list from an unmodified list}} \newline List\textless{}String\textgreater{} chars = new ArrayList\textless{}\textgreater{}(List.of("a", "b", "c")); \newline \newline - {\bf{Sort a list a -{}-\textgreater{} z}} \newline List\textless{}String\textgreater{} chars = new ArrayList\textless{}\textgreater{}(List.of("b", "a", "c")); \newline chars.sort(Comparator.naturalOrder()); \newline System.out.println(chars); //{[}a, b, c{]} \newline \newline - {\bf{Sort a list z -{}-\textgreater{} a}} \newline chars.sort(Comparator.reverseOrder()); \newline System.out.println(chars); //{[}c, b, a{]} \newline \newline - {\bf{Replace/Update all elements}} \newline List\textless{}String\textgreater{} chars = new ArrayList\textless{}\textgreater{}(List.of("a", "b", "c")); \newline chars.replaceAll(s -\textgreater{} s + "\_1"); \newline System.out.println(chars); //{[}a\_1, b\_1, c\_1{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Map}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{- key-value associations to store and retrieve elements \newline % Row Count 2 (+ 2) • Normal map → HashMap \newline % Row Count 3 (+ 1) • Keep insertion order → LinkedHashMap \newline % Row Count 4 (+ 1) • Key is Enum → EnumMap \newline % Row Count 5 (+ 1) • Auto sort → TreeMap \newline % Row Count 6 (+ 1) • When multi-thread → ConcurrentHashMap% Row Count 7 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Map Methods}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Some methods to work with map \newline \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline \newline - {\bf{Create an normal map}} \newline Map\textless{}Integer, String\textgreater{} names = new HashMap\textless{}\textgreater{}(); \newline Map\textless{}String, Person\textgreater{} names = new HashMap\textless{}\textgreater{}(); \newline \newline - {\bf{Create an unmodified map}} \newline Map\textless{}Integer, String\textgreater{} names = Map.of(1, "john", 2, "james"); \newline \newline - {\bf{Create a modified map from an unmodified map}} \newline Map\textless{}Integer, String\textgreater{} names = new HashMap\textless{}\textgreater{}(Map.of(1, "john", 2, "james")); \newline \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline \newline Map\textless{}Integer, String\textgreater{} names = new HashMap\textless{}\textgreater{}(); \newline \newline - {\bf{Add one element}} \newline names.put(1, "john"); \newline System.out.println(names); //\{1=john\} \newline \newline - {\bf{Add multiple elements}} \newline names.putAll(Map.of(1, "john", 2, "james")); \newline System.out.println(names); //\{1=john, 2=james\} \newline \newline - {\bf{Get value by key}} \newline String name = names.get(1); \newline System.out.println(name); //john \newline \newline - {\bf{Get value by key, if don't exist return default value}} \newline String name = names.getOrDefault(3, "NOT\_FOUND"); \newline System.out.println(name); //NOT\_FOUND \newline \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline \newline Map\textless{}Integer, String\textgreater{} names = new HashMap\textless{}\textgreater{}(Map.of(1, "john", 2, "james")); \newline \newline - {\bf{Get all keys}} \newline Set\textless{}Integer\textgreater{} keys = names.keySet(); \newline System.out.println(keys); //{[}1, 2{]} \newline \newline - {\bf{Get all values}} \newline Collection\textless{}String\textgreater{} values = names.values(); \newline System.out.println(values); //{[}john, james{]} \newline \newline - {\bf{Print key-value}} \newline names.forEach((k, v) -\textgreater{} System.out.printf("k=\%s, v=\%s \%n", k, v)); \newline //k=1, v=john \newline //k=2, v=james \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline \newline - {\bf{Update multiple values}} \newline Map\textless{}Integer, String\textgreater{} names = new HashMap\textless{}\textgreater{}(); \newline for (int i = 0; i \textless{} 4; i++) \{ \newline names.compute(i, (k, v) -\textgreater{} "john\_" + k); \newline \} \newline System.out.println(names); // \{0=john\_0, 1=john\_1, 2=john\_2, 3=john\_3\}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}