1+ <!DOCTYPE HTML>
2+ < html lang ="en " class ="navy sidebar-visible " dir ="ltr ">
3+ < head >
4+ <!-- Book generated using mdBook -->
5+ < meta charset ="UTF-8 ">
6+ < title > Hashsets - Montclair Robotics Programming</ title >
7+
8+
9+ <!-- Custom HTML head -->
10+
11+ < meta name ="description " content ="">
12+ < meta name ="viewport " content ="width=device-width, initial-scale=1 ">
13+ < meta name ="theme-color " content ="#ffffff ">
14+
15+ < link rel ="shortcut icon " href ="../favicon.png ">
16+ < link rel ="stylesheet " href ="../css/variables.css ">
17+ < link rel ="stylesheet " href ="../css/general.css ">
18+ < link rel ="stylesheet " href ="../css/chrome.css ">
19+
20+ <!-- Fonts -->
21+ < link rel ="stylesheet " href ="../FontAwesome/css/font-awesome.css ">
22+ < link rel ="stylesheet " href ="../fonts/fonts.css ">
23+
24+ <!-- Highlight.js Stylesheets -->
25+ < link rel ="stylesheet " href ="../highlight.css ">
26+ < link rel ="stylesheet " href ="../tomorrow-night.css ">
27+ < link rel ="stylesheet " href ="../ayu-highlight.css ">
28+
29+ <!-- Custom theme stylesheets -->
30+ < link rel ="stylesheet " href ="../theme/pagetoc.css ">
31+
32+
33+ <!-- Provide site root to javascript -->
34+ < script >
35+ var path_to_root = "../" ;
36+ var default_theme = window . matchMedia ( "(prefers-color-scheme: dark)" ) . matches ? "navy" : "navy" ;
37+ </ script >
38+ <!-- Start loading toc.js asap -->
39+ < script src ="../toc.js "> </ script >
40+ </ head >
41+ < body >
42+ < div id ="body-container ">
43+ <!-- Work around some values being stored in localStorage wrapped in quotes -->
44+ < script >
45+ try {
46+ var theme = localStorage . getItem ( 'mdbook-theme' ) ;
47+ var sidebar = localStorage . getItem ( 'mdbook-sidebar' ) ;
48+
49+ if ( theme . startsWith ( '"' ) && theme . endsWith ( '"' ) ) {
50+ localStorage . setItem ( 'mdbook-theme' , theme . slice ( 1 , theme . length - 1 ) ) ;
51+ }
52+
53+ if ( sidebar . startsWith ( '"' ) && sidebar . endsWith ( '"' ) ) {
54+ localStorage . setItem ( 'mdbook-sidebar' , sidebar . slice ( 1 , sidebar . length - 1 ) ) ;
55+ }
56+ } catch ( e ) { }
57+ </ script >
58+
59+ <!-- Set the theme before any content is loaded, prevents flash -->
60+ < script >
61+ var theme ;
62+ try { theme = localStorage . getItem ( 'mdbook-theme' ) ; } catch ( e ) { }
63+ if ( theme === null || theme === undefined ) { theme = default_theme ; }
64+ const html = document . documentElement ;
65+ html . classList . remove ( 'navy' )
66+ html . classList . add ( theme ) ;
67+ html . classList . add ( "js" ) ;
68+ </ script >
69+
70+ < input type ="checkbox " id ="sidebar-toggle-anchor " class ="hidden ">
71+
72+ <!-- Hide / unhide sidebar before it is displayed -->
73+ < script >
74+ var sidebar = null ;
75+ var sidebar_toggle = document . getElementById ( "sidebar-toggle-anchor" ) ;
76+ if ( document . body . clientWidth >= 1080 ) {
77+ try { sidebar = localStorage . getItem ( 'mdbook-sidebar' ) ; } catch ( e ) { }
78+ sidebar = sidebar || 'visible' ;
79+ } else {
80+ sidebar = 'hidden' ;
81+ }
82+ sidebar_toggle . checked = sidebar === 'visible' ;
83+ html . classList . remove ( 'sidebar-visible' ) ;
84+ html . classList . add ( "sidebar-" + sidebar ) ;
85+ </ script >
86+
87+ < nav id ="sidebar " class ="sidebar " aria-label ="Table of contents ">
88+ <!-- populated by js -->
89+ < mdbook-sidebar-scrollbox class ="sidebar-scrollbox "> </ mdbook-sidebar-scrollbox >
90+ < noscript >
91+ < iframe class ="sidebar-iframe-outer " src ="../toc.html "> </ iframe >
92+ </ noscript >
93+ < div id ="sidebar-resize-handle " class ="sidebar-resize-handle ">
94+ < div class ="sidebar-resize-indicator "> </ div >
95+ </ div >
96+ </ nav >
97+
98+ < div id ="page-wrapper " class ="page-wrapper ">
99+
100+ < div class ="page ">
101+ < div id ="menu-bar-hover-placeholder "> </ div >
102+ < div id ="menu-bar " class ="menu-bar sticky ">
103+ < div class ="left-buttons ">
104+ < label id ="sidebar-toggle " class ="icon-button " for ="sidebar-toggle-anchor " title ="Toggle Table of Contents " aria-label ="Toggle Table of Contents " aria-controls ="sidebar ">
105+ < i class ="fa fa-bars "> </ i >
106+ </ label >
107+ < button id ="theme-toggle " class ="icon-button " type ="button " title ="Change theme " aria-label ="Change theme " aria-haspopup ="true " aria-expanded ="false " aria-controls ="theme-list ">
108+ < i class ="fa fa-paint-brush "> </ i >
109+ </ button >
110+ < ul id ="theme-list " class ="theme-popup " aria-label ="Themes " role ="menu " style ="z-index:20000 !important; ">
111+ < li role ="none "> < button role ="menuitem " class ="theme " id ="light "> Light</ button > </ li >
112+ < li role ="none "> < button role ="menuitem " class ="theme " id ="catppuccinLatte "> C. Latte</ button > </ li >
113+ < li role ="none "> < button role ="menuitem " class ="theme " id ="rust "> Rust</ button > </ li >
114+ < li role ="none "> < button role ="menuitem " class ="theme " id ="coal "> Coal</ button > </ li >
115+ < li role ="none "> < button role ="menuitem " class ="theme " id ="navy "> Navy</ button > </ li >
116+ < li role ="none "> < button role ="menuitem " class ="theme " id ="ayu "> Ayu</ button > </ li >
117+ < li role ="none "> < button role ="menuitem " class ="theme " id ="new2025 "> Modern</ button > </ li >
118+ < li role ="none "> < button role ="menuitem " class ="theme " id ="catppuccinFrappe "> C. Frappe</ button > </ li >
119+ < li role ="none "> < button role ="menuitem " class ="theme " id ="catppuccinMacchiato "> C. Macchiato</ button > </ li >
120+ < li role ="none "> < button role ="menuitem " class ="theme " id ="catppuccinMocha "> C. Mocha</ button > </ li >
121+ < li role ="none "> < button role ="menuitem " class ="theme " id ="emo "> Emo</ button > </ li >
122+ </ ul >
123+ < button id ="search-toggle " class ="icon-button " type ="button " title ="Search. (Shortkey: s) " aria-label ="Toggle Searchbar " aria-expanded ="false " aria-keyshortcuts ="S " aria-controls ="searchbar ">
124+ < i class ="fa fa-search "> </ i >
125+ </ button >
126+ </ div >
127+
128+ < h1 class ="menu-title "> Montclair Robotics Programming</ h1 >
129+
130+ < div class ="right-buttons ">
131+ < a href ="https://github.com/MontclairRobotics/FRC-Programming-Guide " title ="Git repository " aria-label ="Git repository ">
132+ < i id ="git-repository-button " class ="fa fa-github "> </ i >
133+ </ a >
134+ < a href ="https://github.com/MontclairRobotics/FRC-Programming-Guide/edit/main/src/Advanced-Java/Hashsets.md " title ="Suggest an edit " aria-label ="Suggest an edit ">
135+ < i id ="git-edit-button " class ="fa fa-edit "> </ i >
136+ </ a >
137+
138+ </ div >
139+ </ div >
140+
141+ < div id ="search-wrapper " class ="hidden ">
142+ < form id ="searchbar-outer " class ="searchbar-outer ">
143+ < input type ="search " id ="searchbar " name ="searchbar " placeholder ="Search this book ... " aria-controls ="searchresults-outer " aria-describedby ="searchresults-header ">
144+ </ form >
145+ < div id ="searchresults-outer " class ="searchresults-outer hidden ">
146+ < div id ="searchresults-header " class ="searchresults-header "> </ div >
147+ < ul id ="searchresults ">
148+ </ ul >
149+ </ div >
150+ </ div >
151+
152+ <!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
153+ < script >
154+ document . getElementById ( 'sidebar-toggle' ) . setAttribute ( 'aria-expanded' , sidebar === 'visible' ) ;
155+ document . getElementById ( 'sidebar' ) . setAttribute ( 'aria-hidden' , sidebar !== 'visible' ) ;
156+ Array . from ( document . querySelectorAll ( '#sidebar a' ) ) . forEach ( function ( link ) {
157+ link . setAttribute ( 'tabIndex' , sidebar === 'visible' ? 0 : - 1 ) ;
158+ } ) ;
159+ </ script >
160+
161+ < div id ="content " class ="content ">
162+ < main >
163+ < div class ="content-wrap ">
164+ < h2 id ="hashsets "> < a class ="header " href ="#hashsets "> HashSets</ a > </ h2 >
165+ < p > You’ve just learned that a HashMap stores key–value pairs, with each key being unique.</ p >
166+ < p > A < code > HashSet</ code > is similar, except it stores only the values, and each value must be unique.
167+ There are no keys — you just add elements, and the set makes sure no duplicates are allowed.</ p >
168+ < p > Like < code > HashMap</ code > , < code > HashSet</ code > is contained in < code > java.util</ code > .</ p >
169+ < p > General Syntax for Creating a HashSet</ p >
170+ < pre > < code class ="language-java "> import java.util.HashSet;
171+
172+ HashSet<ElementType> name = new HashSet<ElementType>();
173+ </ code > </ pre >
174+ < p > To add items to a HashSet, use the method .add(E element) where E is the element type parameter.</ p >
175+ < p > Here is an example of a set that stores String names:</ p >
176+ < pre > < code class ="language-java "> public class Program {
177+ public static void main(String[] args) {
178+
179+ HashSet<String> names = new HashSet<String>();
180+
181+ names.add("Johnny");
182+ names.add("Carl");
183+ names.add("Grandpa");
184+ names.add("Carl"); // duplicate - ignored
185+
186+ System.out.println(names);
187+ // prints something like: [Grandpa, Johnny, Carl]
188+ // order is not guaranteed
189+ }
190+ }
191+ </ code > </ pre >
192+ < p > More Methods
193+ To check if the set contains an element, use .contains(E element)</ p >
194+ < p > < b > < code > names.contains("Carl"); // true</ code > </ b >
195+ < b > < code > names.contains("Billy"); // false</ code > </ b > </ p >
196+ < p > To remove an element, use .remove(E element)</ p >
197+ < p > < b > < code > names.remove("Johnny"); // Johnny is no longer in the set</ code > </ b > </ p >
198+ < p > To get the number of items, use < code > .size()</ code > </ p >
199+ < p > < b > < code > names.size();</ code > </ b > </ p >
200+ < p > To remove all elements, use < code > .clear()</ code > </ p >
201+ < p > < b > < code > names.clear();</ code > </ b > </ p >
202+ < blockquote >
203+ < p > For more information, check out < a href ="https://www.w3schools.com/java/java_hashset.asp "> w3schools</ a > </ p >
204+ </ blockquote >
205+ < hr />
206+ < h2 id ="task "> < a class ="header " href ="#task "> Task</ a > </ h2 >
207+ < p > Create a set of your favorite foods (Strings). Print the set, check if a certain food is in it, and remove one.</ p >
208+
209+ </ div >
210+ < div id ="sidetoc ">
211+ < nav id ="pagetoc "> </ nav >
212+ </ div >
213+ </ main >
214+
215+ < nav class ="nav-wrapper " aria-label ="Page navigation ">
216+ <!-- Mobile navigation buttons -->
217+ < a rel ="prev " href ="../Advanced-Java/Hashmaps.html " class ="mobile-nav-chapters previous " title ="Previous chapter " aria-label ="Previous chapter " aria-keyshortcuts ="Left ">
218+ < i class ="fa fa-angle-left "> </ i >
219+ </ a >
220+
221+ < a rel ="next prefetch " href ="../Advanced-Java/Lambdas.html " class ="mobile-nav-chapters next " title ="Next chapter " aria-label ="Next chapter " aria-keyshortcuts ="Right ">
222+ < i class ="fa fa-angle-right "> </ i >
223+ </ a >
224+
225+ < div style ="clear: both "> </ div >
226+ </ nav >
227+ </ div >
228+ </ div >
229+
230+ < nav class ="nav-wide-wrapper " aria-label ="Page navigation ">
231+ < a rel ="prev " href ="../Advanced-Java/Hashmaps.html " class ="nav-chapters previous " title ="Previous chapter " aria-label ="Previous chapter " aria-keyshortcuts ="Left ">
232+ < i class ="fa fa-angle-left "> </ i >
233+ </ a >
234+
235+ < a rel ="next prefetch " href ="../Advanced-Java/Lambdas.html " class ="nav-chapters next " title ="Next chapter " aria-label ="Next chapter " aria-keyshortcuts ="Right ">
236+ < i class ="fa fa-angle-right "> </ i >
237+ </ a >
238+ </ nav >
239+
240+ </ div >
241+
242+
243+
244+ < script >
245+ window . playground_line_numbers = true ;
246+ </ script >
247+
248+ < script >
249+ window . playground_copyable = true ;
250+ </ script >
251+
252+ < script src ="../ace.js "> </ script >
253+ < script src ="../editor.js "> </ script >
254+ < script src ="../mode-rust.js "> </ script >
255+ < script src ="../theme-dawn.js "> </ script >
256+ < script src ="../theme-tomorrow_night.js "> </ script >
257+
258+ < script src ="../elasticlunr.min.js "> </ script >
259+ < script src ="../mark.min.js "> </ script >
260+ < script src ="../searcher.js "> </ script >
261+
262+ < script src ="../clipboard.min.js "> </ script >
263+ < script src ="../highlight.js "> </ script >
264+ < script src ="../book.js "> </ script >
265+
266+ <!-- Inline theme‑toggle wiring -->
267+ < script >
268+ ( function ( ) {
269+ const btn = document . getElementById ( 'theme-toggle' ) ;
270+ const menu = document . getElementById ( 'theme-list' ) ;
271+ const html = document . documentElement ;
272+ function removeAllThemes ( ) {
273+ const themeButtons = document . querySelectorAll ( '#theme-list .theme' ) ;
274+ themeButtons . forEach ( btn => {
275+ const themeClass = btn . id ;
276+ document . documentElement . classList . remove ( themeClass ) ;
277+ } ) ;
278+ }
279+ function setTheme ( name ) {
280+ // remove existing theme classes (adjust to your theme names)
281+ removeAllThemes ( ) ;
282+ html . classList . add ( name ) ;
283+ localStorage . setItem ( 'mdbook-theme' , name ) ;
284+ btn . setAttribute ( 'aria-expanded' , 'false' ) ;
285+ menu . style . display = 'none' ;
286+ document . querySelectorAll ( '.theme-selected' ) . forEach ( item => {
287+ item . classList . remove ( 'theme-selected' ) ;
288+ } ) ;
289+ document . getElementById ( name ) . classList . add ( 'theme-selected' )
290+ }
291+
292+ // toggle popup on button click
293+ btn . addEventListener ( 'click' , e => {
294+ e . stopPropagation ( ) ;
295+ const open = ( menu . style . display === 'block' ) ;
296+ menu . style . display = open ? 'none' : 'block' ;
297+ btn . setAttribute ( 'aria-expanded' , ( ! open ) . toString ( ) ) ;
298+ } ) ;
299+
300+ // pick a theme when you click a menu item
301+ menu . addEventListener ( 'click' , e => {
302+ let tgt = e . target ;
303+ while ( tgt && ! tgt . classList . contains ( 'theme' ) ) tgt = tgt . parentElement ;
304+ if ( tgt ) setTheme ( tgt . id ) ;
305+ } ) ;
306+
307+ // clicking outside closes the menu
308+ document . addEventListener ( 'click' , ( ) => {
309+ menu . style . display = 'none' ;
310+ btn . setAttribute ( 'aria-expanded' , 'false' ) ;
311+ } ) ;
312+ } ) ( ) ;
313+ </ script >
314+
315+
316+ <!-- Custom JS scripts -->
317+ < script src ="../theme/pagetoc.js "> </ script >
318+ < script src ="../theme/rn_qol.js "> </ script >
319+
320+
321+ </ div >
322+ </ body >
323+ </ html >
0 commit comments