\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{AbhijayG} \pdfinfo{ /Title (java-collections.pdf) /Creator (Cheatography) /Author (AbhijayG) /Subject (Java Collections 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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 Collections Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{AbhijayG} via \textcolor{DarkBackground}{\uline{cheatography.com/194414/cs/46058/}}} \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}AbhijayG \\ \uline{cheatography.com/abhijayg} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 4th April, 2025.\\ 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*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Java Collections}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/abhijayg_1743742581_Java_Collections-768x660.jpg}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Arrays Class}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{{\bf{import java.util.Arrays;}} \newline % Row Count 1 (+ 1) // Imports Arrays class from java.util package \newline % Row Count 2 (+ 1) /*Key Points about java.util.Arrays: \newline % Row Count 3 (+ 1) 1. {\bf{Package: java.util}}\textasciitilde{}Contains utility classes for working with arrays\textasciitilde{} \newline % Row Count 5 (+ 2) 2. Arrays Class Features: \newline % Row Count 6 (+ 1) - \textasciitilde{}Provides static methods for array operations\textasciitilde{} \newline % Row Count 8 (+ 2) - \textasciitilde{}Common operations include\textasciitilde{}: \newline % Row Count 9 (+ 1) {\bf{ sort()}}- \textasciitilde{}Sorting arrays\textasciitilde{} \newline % Row Count 10 (+ 1) {\bf{ parallelSort()}} - \textasciitilde{}Parallel sorting for large arrays\textasciitilde{} \newline % Row Count 12 (+ 2) {\bf{ fill()}}- \textasciitilde{}Filling arrays with values\textasciitilde{} \newline % Row Count 13 (+ 1) {\bf{ binarySearch()}}- \textasciitilde{}Searching in sorted arrays\textasciitilde{} \newline % Row Count 15 (+ 2) {\bf{ copyOf()}}- \textasciitilde{}Creating copies of arrays\textasciitilde{} \newline % Row Count 16 (+ 1) {\bf{ equals()}}- \textasciitilde{}Comparing arrays\textasciitilde{} \newline % Row Count 17 (+ 1) {\bf{ toString()}} - \textasciitilde{}Converting array to String\textasciitilde{} \newline % Row Count 19 (+ 2) 3. Usage Examples: \newline % Row Count 20 (+ 1) {\bf{Arrays.sort(array)}} // \textasciitilde{}Sorting\textasciitilde{} \newline % Row Count 22 (+ 2) {\bf{Arrays.fill(array, val)}} // \textasciitilde{}Filling\textasciitilde{} \newline % Row Count 23 (+ 1) {\bf{Arrays.toString(array)}} // \textasciitilde{}String representation\textasciitilde{} \newline % Row Count 25 (+ 2) {\bf{Arrays.copyOf(array, len)}} // \textasciitilde{}Copying\textasciitilde{} \newline % Row Count 26 (+ 1) {\bf{Arrays.equals(arr1, arr2)}} // \textasciitilde{}Comparing\textasciitilde{} \newline % Row Count 27 (+ 1) 4. Benefits: \newline % Row Count 28 (+ 1) - Simplified array manipulation \newline % Row Count 29 (+ 1) - Optimized implementations \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Arrays Class (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{ - Consistent behavior across different array types*/% Row Count 2 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Arrays Class}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{//\textasciitilde{}Declare and initialize an array of integers with values from 1 to 10\textasciitilde{} \newline % Row Count 2 (+ 2) //\textasciitilde{}The array must be sorted in ascending order for binary search to work correctly\textasciitilde{} \newline % Row Count 4 (+ 2) {\bf{int{[}{]} numbers = \{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 \};}} \newline % Row Count 6 (+ 2) //\textasciitilde{} Perform binary search to find element 4 in the array\textasciitilde{} \newline % Row Count 8 (+ 2) //\textasciitilde{} Arrays.binarySearch() returns:\textasciitilde{} \newline % Row Count 9 (+ 1) //\textasciitilde{} Index of the element if found (zero-based)\textasciitilde{} \newline % Row Count 10 (+ 1) //\textasciitilde{} Negative value if element not found (-(insertion point) - 1)\textasciitilde{} \newline % Row Count 12 (+ 2) //\textasciitilde{} Note: Array must be sorted before using binarySearch\textasciitilde{} \newline % Row Count 14 (+ 2) {\bf{int index = \seqsplit{Arrays.binarySearch(numbers}, 4);}} \newline % Row Count 15 (+ 1) //\textasciitilde{} Print the result\textasciitilde{} \newline % Row Count 16 (+ 1) //\textasciitilde{} In this case, it will print index 3 because 4 is at position 3 (zero-based indexing)\textasciitilde{} \newline % Row Count 18 (+ 2) System.out.println("The index of element 4 in the array is " + index); \newline % Row Count 20 (+ 2) //\textasciitilde{}Output - {\bf{The index of element 4 in the array is 3}}\textasciitilde{} \newline % Row Count 22 (+ 2) /* \textasciitilde{}Additional Notes:\textasciitilde{} \newline % Row Count 23 (+ 1) \textasciitilde{}1. Binary search is much faster than linear search for large arrays\textasciitilde{} \newline % Row Count 25 (+ 2) \textasciitilde{}2. Time complexity is O(log n) where n is array length\textasciitilde{} \newline % Row Count 27 (+ 2) \textasciitilde{}3. If array is not sorted, results will be unpredictable\textasciitilde{} \newline % Row Count 29 (+ 2) \textasciitilde{}4. If element is not found, return value will be negative\textasciitilde{} \newline % Row Count 31 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Arrays Class (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{ */% Row Count 1 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Arrays Class}} \tn \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{\textasciitilde{}// Initialize an Integer array with 10 elements \newline % Row Count 1 (+ 1) {\bf{Integer{[}{]} numbers = \{ 10, 2, 32, 12, 15, 76, 17, 48, 79, 9 \}}}; \newline % Row Count 3 (+ 2) \textasciitilde{}// {\bf{Arrays.sort()}} - Sorts array in ascending order using quicksort algorithm \newline % Row Count 5 (+ 2) \textasciitilde{}// After sort: {[}2, 9, 10, 12, 15, 17, 32, 48, 76, 79{]} \newline % Row Count 7 (+ 2) {\bf{Arrays.sort(numbers);}} \newline % Row Count 8 (+ 1) \textasciitilde{}//{\bf{Arrays.parallelSort()}} - Sorts array using parallel sorting for large arrays (threshold 8192 elements) \newline % Row Count 11 (+ 3) \textasciitilde{}// Since array is small (\textless{}8192), it will use same algorithm as Arrays.sort() \newline % Row Count 13 (+ 2) \textasciitilde{}// After parallel sort: {[}2, 9, 10, 12, 15, 17, 32, 48, 76, 79{]} \newline % Row Count 15 (+ 2) {\bf{Arrays.parallelSort(numbers);}} \newline % Row Count 16 (+ 1) \textasciitilde{}// {\bf{Arrays.fill()}} - Fills entire array with specified value (12) \newline % Row Count 18 (+ 2) \textasciitilde{}// After fill: {[}12, 12, 12, 12, 12, 12, 12, 12, 12, 12{]} \newline % Row Count 20 (+ 2) {\bf{Arrays.fill(numbers, 12);}} \newline % Row Count 21 (+ 1) \textasciitilde{}// Print all elements of array using enhanced for loop \newline % Row Count 23 (+ 2) {\bf{for (int i : numbers) \{}} \newline % Row Count 24 (+ 1) {\bf{ System.out.print(i + " ");}} \newline % Row Count 25 (+ 1) {\bf{\}}} \newline % Row Count 26 (+ 1) \textasciitilde{}/* Output:\textasciitilde{} \newline % Row Count 27 (+ 1) \textasciitilde{}12 12 12 12 12 12 12 12 12 12\textasciitilde{} \newline % Row Count 28 (+ 1) \textasciitilde{}*/\textasciitilde{}% Row Count 29 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{LinkedList,ArrayList}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{import java.util.ArrayList; \newline /{\emph{ \newline 1. Package: java.util \newline 2. ArrayList class: \newline - Implements List interface \newline - Dynamic array implementation \newline - Good for random access and storing/accessing elements \newline - Not synchronized (not thread-safe) \newline - Common operations: \newline }} add() - O(1) amortized \newline {\emph{ get() - O(1) \newline }} remove() - O(n) \newline {\emph{ contains() - O(n) \newline }}/ \newline \newline import java.util.Iterator; \newline /{\emph{ \newline 1. Package: java.util \newline 2. Iterator interface: \newline - Provides methods to iterate through collections \newline - Key methods: \newline }} hasNext() - checks if more elements exist \newline {\emph{ next() - returns next element \newline }} remove() - removes last element returned \newline - Safer than for loop for modification during iteration \newline - Used in for-each loops internally \newline {\emph{/ \newline \newline import java.util.LinkedList; \newline /}} \newline 1. Package: java.util \newline 2. LinkedList class: \newline - Implements List and Deque interfaces \newline - Doubly-linked list implementation \newline - Good for frequent insertions/deletions \newline - Common operations: \newline {\emph{ add() - O(1) \newline }} get() - O(n) \newline {\emph{ remove() - O(1) if position known \newline }} contains() - O(n) \newline {\emph{/ \newline \newline import java.util.List; \newline /}} \newline 1. Package: java.util \newline 2. List interface: \newline - Ordered collection (sequence) \newline - Allows duplicates \newline - Main implementations: \newline {\emph{ ArrayList \newline }} LinkedList \newline {\emph{ Vector (legacy, thread-safe) \newline - Defines common list operations: \newline }} add() \newline {\emph{ remove() \newline }} get() \newline {\emph{ set() \newline }} etc. \newline */} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{LinkedList,ArrayList}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{// Create a new LinkedList to store Integer values \newline List\textless{}Integer\textgreater{} list = new LinkedList\textless{}\textgreater{}(); \newline \newline // Add elements to the LinkedList \newline list.add(1); \newline list.add(2); \newline list.add(3); \newline System.out.println(list); // Output: {[}1, 2, 3{]} \newline \newline list.add(4); // Add 4 at the end of the List \newline System.out.println(list); // Output: {[}1, 2, 3, 4{]} \newline \newline list.add(1, 50); // Add 50 at index 1 (shifts other elements right) \newline System.out.println(list); // Output: {[}1, 50, 2, 3, 4{]} \newline \newline // Create a new ArrayList to be added to original list \newline List\textless{}Integer\textgreater{} newList = new ArrayList(); \newline newList.add(150); \newline newList.add(160); \newline \newline list.addAll(newList); // Add all elements from newList to list \newline System.out.println(list); // Output: {[}1, 50, 2, 3, 4, 150, 160{]} \newline \newline System.out.println(list.get(1)); // Output: 50 (element at index 1) \newline \newline // Create a new ArrayList \newline List\textless{}Integer\textgreater{} list = new ArrayList\textless{}\textgreater{}(); \newline list.add(10); \newline list.add(20); \newline list.add(30); \newline list.add(40); \newline list.add(50); \newline list.add(60); \newline list.add(70); \newline list.add(80); \newline System.out.println(list); // Output: {[}10, 20, 30, 40, 50, 60, 70, 80{]} \newline \newline // Iterate using traditional for loop \newline for (int i = 0; i \textless{} list.size(); i++) \{ \newline System.out.println("the element is " + list.get(i)); \newline /{\emph{ Output: \newline the element is 10 \newline the element is 20 \newline the element is 30 \newline the element is 40 \newline the element is 50 \newline the element is 60 \newline the element is 70 \newline the element is 80 }}/ \newline \} \newline \newline // Iterate using foreach loop \newline for (Integer element: list) \{ \newline \seqsplit{System.out.println("foreach} element is " + element); \newline /{\emph{ Output: \newline foreach element is 10 \newline foreach element is 20 \newline foreach element is 30 \newline foreach element is 40 \newline foreach element is 50 \newline foreach element is 60 \newline foreach element is 70 \newline foreach element is 80 }}/ \newline \} \newline \newline // Iterate using Iterator \newline Iterator\textless{}Integer\textgreater{} it = list.iterator(); \newline while (it.hasNext()) \{ \newline \seqsplit{System.out.println("iterator} " + it.next()); \newline /{\emph{ Output: \newline iterator 10 \newline iterator 20 \newline iterator 30 \newline iterator 40 \newline iterator 50 \newline iterator 60 \newline iterator 70 \newline iterator 80 }}/ \newline \} \newline \newline list.set(2, 1000); // Replace element at index 2 with 1000 \newline System.out.println(list); // Output: {[}10, 20, 1000, 40, 50, 60, 70, 80{]} \newline \newline System.out.println(list.contains(500)); // Output: false (500 is not in the list) \newline \newline list.remove(1); // Remove element at index 1 (20) \newline System.out.println(list); // Output: {[}10, 1000, 40, 50, 60, 70, 80{]} \newline \newline list.remove(Integer.valueOf(30)); // Remove element 30 (if exists) \newline System.out.println(list); // Output: {[}10, 1000, 40, 50, 60, 70, 80{]} \newline \newline list.clear(); // Remove all elements from the list \newline System.out.println(list); // Output: {[}{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}