[{"data":1,"prerenderedAt":1263},["ShallowReactive",2],{"i-simple-icons:github":3,"i-simple-icons:x":8,"i-brand:linkedin":10,"i-simple-icons:medium":14,"i-lucide:mail":16,"i-lucide:sun":18,"pattern:\u002Flearn\u002Fdesign-patterns\u002Fiterator":20,"pattern-group:\u002Flearn\u002Fdesign-patterns\u002Fiterator":1204,"i-lucide:lightbulb":1241,"i-lucide:workflow":1243,"i-lucide:link":1245,"i-lucide:square-terminal":1247,"i-lucide:list-ordered":1249,"i-lucide:bell":1251,"i-lucide:toggle-left":1253,"i-lucide:shuffle":1255,"i-lucide:layout-template":1257,"i-lucide:footprints":1259,"i-lucide:copy":1261},{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":7},0,24,false,"\u003Cpath fill=\"currentColor\" d=\"M12 .297c-6.63 0-12 5.373-12 12c0 5.303 3.438 9.8 8.205 11.385c.6.113.82-.258.82-.577c0-.285-.01-1.04-.015-2.04c-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729c1.205.084 1.838 1.236 1.838 1.236c1.07 1.835 2.809 1.305 3.495.998c.108-.776.417-1.305.76-1.605c-2.665-.3-5.466-1.332-5.466-5.93c0-1.31.465-2.38 1.235-3.22c-.135-.303-.54-1.523.105-3.176c0 0 1.005-.322 3.3 1.23c.96-.267 1.98-.399 3-.405c1.02.006 2.04.138 3 .405c2.28-1.552 3.285-1.23 3.285-1.23c.645 1.653.24 2.873.12 3.176c.765.84 1.23 1.91 1.23 3.22c0 4.61-2.805 5.625-5.475 5.92c.42.36.81 1.096.81 2.22c0 1.606-.015 2.896-.015 3.286c0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":9},"\u003Cpath fill=\"currentColor\" d=\"M14.234 10.162L22.977 0h-2.072l-7.591 8.824L7.251 0H.258l9.168 13.343L.258 24H2.33l8.016-9.318L16.749 24h6.993zm-2.837 3.299l-.929-1.329L3.076 1.56h3.182l5.965 8.532l.929 1.329l7.754 11.09h-3.182z\"\u002F>",{"left":4,"top":11,"width":12,"height":12,"rotate":4,"vFlip":6,"hFlip":6,"body":13},32,448,"\u003Cpath fill=\"currentColor\" d=\"M416 32L31.9 32C14.3 32 0 46.5 0 64.3L0 447.7C0 465.5 14.3 480 31.9 480L416 480c17.6 0 32-14.5 32-32.3l0-383.4C448 46.5 433.6 32 416 32zM135.4 416l-66.4 0 0-213.8 66.5 0 0 213.8-.1 0zM102.2 96a38.5 38.5 0 1 1 0 77 38.5 38.5 0 1 1 0-77zM384.3 416l-66.4 0 0-104c0-24.8-.5-56.7-34.5-56.7-34.6 0-39.9 27-39.9 54.9l0 105.8-66.4 0 0-213.8 63.7 0 0 29.2 .9 0c8.9-16.8 30.6-34.5 62.9-34.5 67.2 0 79.7 44.3 79.7 101.9l0 117.2z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":15},"\u003Cpath fill=\"currentColor\" d=\"M4.21 0A4.2 4.2 0 0 0 0 4.21v15.58A4.2 4.2 0 0 0 4.21 24h15.58A4.2 4.2 0 0 0 24 19.79v-1.093a5 5 0 0 1-.422.02c-2.577 0-4.027-2.146-4.09-4.832a8 8 0 0 1 .022-.708c.093-1.186.475-2.241 1.105-3.022a3.9 3.9 0 0 1 1.395-1.1c.468-.237 1.127-.367 1.664-.367h.023q.151 0 .303.01V4.211A4.2 4.2 0 0 0 19.79 0Zm.198 5.583h4.165l3.588 8.435l3.59-8.435h3.864v.146l-.019.004c-.705.16-1.063.397-1.063 1.254h-.003l.003 10.274c.06.676.424.885 1.063 1.03l.02.004v.145h-4.923v-.145l.019-.005c.639-.144.994-.353 1.054-1.03V7.267l-4.745 11.15h-.261L6.15 7.569v9.445c0 .857.358 1.094 1.063 1.253l.02.004v.147H4.405v-.147l.019-.004c.705-.16 1.065-.397 1.065-1.253V6.987c0-.857-.358-1.094-1.064-1.254l-.018-.004zm19.25 3.668c-1.086.023-1.733 1.323-1.813 3.124H24V9.298a1.4 1.4 0 0 0-.342-.047m-1.862 3.632c-.1 1.756.86 3.239 2.204 3.634v-3.634z\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":17},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"m22 7l-8.991 5.727a2 2 0 0 1-2.009 0L2 7\"\u002F>\u003Crect width=\"20\" height=\"16\" x=\"2\" y=\"4\" rx=\"2\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":19},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Ccircle cx=\"12\" cy=\"12\" r=\"4\"\u002F>\u003Cpath d=\"M12 2v2m0 16v2M4.93 4.93l1.41 1.41m11.32 11.32l1.41 1.41M2 12h2m16 0h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41\"\u002F>\u003C\u002Fg>",{"id":21,"title":22,"body":23,"description":1190,"example":1191,"extension":1192,"group":1193,"icon":1194,"meta":1195,"navigation":301,"order":259,"path":1196,"rawbody":1197,"seo":1198,"sitemap":1199,"source":1200,"stem":1201,"summary":1202,"__hash__":1203},"patterns\u002Flearn\u002Fdesign-patterns\u002Fiterator.md","Iterator",{"type":24,"value":25,"toc":1180},"minimark",[26,31,35,47,54,58,74,78,83,94,98,106,110,124,176,186,190,263,319,710,916,1162,1168,1176],[27,28,30],"h2",{"id":29},"the-idea","The idea",[32,33,34],"p",{},"A collection might keep its items in an array, a linked list, a tree or a hash table. If client code loops over the array directly, it depends on that choice. Change the storage, and every loop has to change too.",[32,36,37,38,42,43,46],{},"The Iterator pattern moves the job of walking through the items into a separate object, the iterator. It answers two questions: is there another item (",[39,40,41],"code",{},"hasNext()","), and what is it (",[39,44,45],{},"next()","). The iterator knows how the collection is stored, and the client only knows the iterator's interface.",[32,48,49,50,53],{},"Since each iterator keeps its own position, several iterators can walk the same collection at the same time without getting in each other's way. Java's ",[39,51,52],{},"java.util.Iterator"," and its for-each loop are built on this pattern.",[27,55,57],{"id":56},"when-to-use-it","When to use it",[59,60,61,65,68,71],"ul",{},[62,63,64],"li",{},"You want to hide how a collection stores its items from the code that uses it.",[62,66,67],{},"You need one way to loop over several kinds of collections.",[62,69,70],{},"You want more than one way to walk the same collection, such as forwards, backwards or filtered.",[62,72,73],{},"You need several loops over the same collection at once, each with its own position.",[27,75,77],{"id":76},"pros-and-cons","Pros and cons",[79,80,82],"h3",{"id":81},"pros","Pros",[59,84,85,88,91],{},[62,86,87],{},"Clients walk a collection without knowing how it is stored, so different collections can be used in the same way.",[62,89,90],{},"Items are reached in order, one at a time, without exposing the collection's internal structure.",[62,92,93],{},"Several iterators can work on the same collection at once, each with its own position.",[79,95,97],{"id":96},"cons","Cons",[59,99,100,103],{},[62,101,102],{},"It adds objects and classes to the system: an iterator for every kind of collection.",[62,104,105],{},"For a simple array or list, a plain loop is shorter and easier to read.",[27,107,109],{"id":108},"the-example","The example",[32,111,112,113,115,116,119,120,123],{},"The example stores strings in a fixed-size array and prints them through an iterator. It defines its own ",[39,114,22],{}," and ",[39,117,118],{},"Collection"," interfaces rather than using the ones in ",[39,121,122],{},"java.util",".",[59,125,126,137,148,156,164],{},[62,127,128,133,134,123],{},[129,130,131],"strong",{},[39,132,118],{},": the aggregate interface, with ",[39,135,136],{},"createIterator()",[62,138,139,143,144,115,146,123],{},[129,140,141],{},[39,142,22],{},": the iterator interface, with ",[39,145,41],{},[39,147,45],{},[62,149,150,155],{},[129,151,152],{},[39,153,154],{},"ConcreteCollection",": the concrete aggregate. It stores items in an array and tracks how many have been added.",[62,157,158,163],{},[129,159,160],{},[39,161,162],{},"ConcreteIterator",": the concrete iterator. It keeps an index and reads items from the collection until it reaches the collection's size.",[62,165,166,171,172,115,174,123],{},[129,167,168],{},[39,169,170],{},"Demo",": adds three items, gives the collection an iterator, then loops with ",[39,173,41],{},[39,175,45],{},[32,177,178,179,181,182,185],{},"In the Gang of Four version, ",[39,180,136],{}," creates a new iterator and returns it. Here it receives an iterator from the caller and stores it, and ",[39,183,184],{},"getIterator()"," hands it back.",[27,187,189],{"id":188},"the-code","The code",[191,192,198],"pre",{"className":193,"code":194,"filename":195,"language":196,"meta":197,"style":197},"language-java shiki shiki-themes haytham-dark haytham-light","public interface Collection\u003CT> {\n    void createIterator(Iterator\u003CT> iterator);\n}\n","Collection.java","java","",[39,199,200,229,257],{"__ignoreMap":197},[201,202,205,209,212,216,220,223,226],"span",{"class":203,"line":204},"line",1,[201,206,208],{"class":207},"sVsk0","public",[201,210,211],{"class":207}," interface",[201,213,215],{"class":214},"sNwy7"," Collection",[201,217,219],{"class":218},"sVG6I","\u003C",[201,221,222],{"class":207},"T",[201,224,225],{"class":218},">",[201,227,228],{"class":218}," {\n",[201,230,232,235,239,242,244,246,248,250,254],{"class":203,"line":231},2,[201,233,234],{"class":207},"    void",[201,236,238],{"class":237},"sVz7n"," createIterator",[201,240,241],{"class":218},"(",[201,243,22],{"class":207},[201,245,219],{"class":218},[201,247,222],{"class":207},[201,249,225],{"class":218},[201,251,253],{"class":252},"sWFPS"," iterator",[201,255,256],{"class":218},");\n",[201,258,260],{"class":203,"line":259},3,[201,261,262],{"class":218},"}\n",[191,264,267],{"className":193,"code":265,"filename":266,"language":196,"meta":197,"style":197},"public interface Iterator\u003CT> {\n    boolean hasNext();\n\n    T next();\n}\n","Iterator.java",[39,268,269,286,297,303,314],{"__ignoreMap":197},[201,270,271,273,275,278,280,282,284],{"class":203,"line":204},[201,272,208],{"class":207},[201,274,211],{"class":207},[201,276,277],{"class":214}," Iterator",[201,279,219],{"class":218},[201,281,222],{"class":207},[201,283,225],{"class":218},[201,285,228],{"class":218},[201,287,288,291,294],{"class":203,"line":231},[201,289,290],{"class":207},"    boolean",[201,292,293],{"class":237}," hasNext",[201,295,296],{"class":218},"();\n",[201,298,299],{"class":203,"line":259},[201,300,302],{"emptyLinePlaceholder":301},true,"\n",[201,304,306,309,312],{"class":203,"line":305},4,[201,307,308],{"class":207},"    T",[201,310,311],{"class":237}," next",[201,313,296],{"class":218},[201,315,317],{"class":203,"line":316},5,[201,318,262],{"class":218},[191,320,323],{"className":193,"code":321,"filename":322,"language":196,"meta":197,"style":197},"public class ConcreteCollection\u003CT> implements Collection\u003CT> {\n    private final T[] items;\n    private int size = 0;\n    private Iterator\u003CT> iterator;\n\n    public ConcreteCollection(int size) {\n        this.items = (T[]) new Object[size];\n    }\n\n    public void add(T item) {\n        this.items[size++] = item;\n    }\n\n    public int getSize() {\n        return size;\n    }\n\n    public T[] getItems() {\n        return items;\n    }\n\n    @Override\n    public void createIterator(Iterator\u003CT> iterator) {\n        this.iterator = iterator;\n    }\n\n    public Iterator\u003CT> getIterator() {\n        return this.iterator;\n    }\n}\n","ConcreteCollection.java",[39,324,325,354,374,393,409,413,434,472,478,483,505,527,532,537,552,562,567,572,588,597,602,607,616,641,656,661,666,686,700,705],{"__ignoreMap":197},[201,326,327,329,332,335,337,339,341,344,346,348,350,352],{"class":203,"line":204},[201,328,208],{"class":207},[201,330,331],{"class":207}," class",[201,333,334],{"class":214}," ConcreteCollection",[201,336,219],{"class":218},[201,338,222],{"class":207},[201,340,225],{"class":218},[201,342,343],{"class":207}," implements",[201,345,215],{"class":214},[201,347,219],{"class":218},[201,349,222],{"class":207},[201,351,225],{"class":218},[201,353,228],{"class":218},[201,355,356,359,362,365,368,371],{"class":203,"line":231},[201,357,358],{"class":207},"    private",[201,360,361],{"class":207}," final",[201,363,364],{"class":207}," T",[201,366,367],{"class":218},"[]",[201,369,370],{"class":252}," items",[201,372,373],{"class":218},";\n",[201,375,376,378,381,384,387,391],{"class":203,"line":259},[201,377,358],{"class":207},[201,379,380],{"class":207}," int",[201,382,383],{"class":252}," size ",[201,385,386],{"class":218},"=",[201,388,390],{"class":389},"sheII"," 0",[201,392,373],{"class":218},[201,394,395,397,399,401,403,405,407],{"class":203,"line":305},[201,396,358],{"class":207},[201,398,277],{"class":207},[201,400,219],{"class":218},[201,402,222],{"class":207},[201,404,225],{"class":218},[201,406,253],{"class":252},[201,408,373],{"class":218},[201,410,411],{"class":203,"line":316},[201,412,302],{"emptyLinePlaceholder":301},[201,414,416,419,421,423,426,429,432],{"class":203,"line":415},6,[201,417,418],{"class":207},"    public",[201,420,334],{"class":237},[201,422,241],{"class":218},[201,424,425],{"class":207},"int",[201,427,428],{"class":252}," size",[201,430,431],{"class":218},")",[201,433,228],{"class":218},[201,435,437,440,442,446,449,452,454,457,460,463,466,469],{"class":203,"line":436},7,[201,438,439],{"class":389},"        this",[201,441,123],{"class":218},[201,443,445],{"class":444},"s4Awn","items",[201,447,448],{"class":218}," =",[201,450,451],{"class":218}," (",[201,453,222],{"class":207},[201,455,456],{"class":218},"[])",[201,458,459],{"class":207}," new",[201,461,462],{"class":207}," Object",[201,464,465],{"class":218},"[",[201,467,468],{"class":252},"size",[201,470,471],{"class":218},"];\n",[201,473,475],{"class":203,"line":474},8,[201,476,477],{"class":218},"    }\n",[201,479,481],{"class":203,"line":480},9,[201,482,302],{"emptyLinePlaceholder":301},[201,484,486,488,491,494,496,498,501,503],{"class":203,"line":485},10,[201,487,418],{"class":207},[201,489,490],{"class":207}," void",[201,492,493],{"class":237}," add",[201,495,241],{"class":218},[201,497,222],{"class":207},[201,499,500],{"class":252}," item",[201,502,431],{"class":218},[201,504,228],{"class":218},[201,506,508,510,512,514,516,518,521,523,525],{"class":203,"line":507},11,[201,509,439],{"class":389},[201,511,123],{"class":218},[201,513,445],{"class":444},[201,515,465],{"class":218},[201,517,468],{"class":252},[201,519,520],{"class":218},"++]",[201,522,448],{"class":218},[201,524,500],{"class":252},[201,526,373],{"class":218},[201,528,530],{"class":203,"line":529},12,[201,531,477],{"class":218},[201,533,535],{"class":203,"line":534},13,[201,536,302],{"emptyLinePlaceholder":301},[201,538,540,542,544,547,550],{"class":203,"line":539},14,[201,541,418],{"class":207},[201,543,380],{"class":207},[201,545,546],{"class":237}," getSize",[201,548,549],{"class":218},"()",[201,551,228],{"class":218},[201,553,555,558,560],{"class":203,"line":554},15,[201,556,557],{"class":207},"        return",[201,559,428],{"class":252},[201,561,373],{"class":218},[201,563,565],{"class":203,"line":564},16,[201,566,477],{"class":218},[201,568,570],{"class":203,"line":569},17,[201,571,302],{"emptyLinePlaceholder":301},[201,573,575,577,579,581,584,586],{"class":203,"line":574},18,[201,576,418],{"class":207},[201,578,364],{"class":207},[201,580,367],{"class":218},[201,582,583],{"class":237}," getItems",[201,585,549],{"class":218},[201,587,228],{"class":218},[201,589,591,593,595],{"class":203,"line":590},19,[201,592,557],{"class":207},[201,594,370],{"class":252},[201,596,373],{"class":218},[201,598,600],{"class":203,"line":599},20,[201,601,477],{"class":218},[201,603,605],{"class":203,"line":604},21,[201,606,302],{"emptyLinePlaceholder":301},[201,608,610,613],{"class":203,"line":609},22,[201,611,612],{"class":218},"    @",[201,614,615],{"class":207},"Override\n",[201,617,619,621,623,625,627,629,631,633,635,637,639],{"class":203,"line":618},23,[201,620,418],{"class":207},[201,622,490],{"class":207},[201,624,238],{"class":237},[201,626,241],{"class":218},[201,628,22],{"class":207},[201,630,219],{"class":218},[201,632,222],{"class":207},[201,634,225],{"class":218},[201,636,253],{"class":252},[201,638,431],{"class":218},[201,640,228],{"class":218},[201,642,643,645,647,650,652,654],{"class":203,"line":5},[201,644,439],{"class":389},[201,646,123],{"class":218},[201,648,649],{"class":444},"iterator",[201,651,448],{"class":218},[201,653,253],{"class":252},[201,655,373],{"class":218},[201,657,659],{"class":203,"line":658},25,[201,660,477],{"class":218},[201,662,664],{"class":203,"line":663},26,[201,665,302],{"emptyLinePlaceholder":301},[201,667,669,671,673,675,677,679,682,684],{"class":203,"line":668},27,[201,670,418],{"class":207},[201,672,277],{"class":207},[201,674,219],{"class":218},[201,676,222],{"class":207},[201,678,225],{"class":218},[201,680,681],{"class":237}," getIterator",[201,683,549],{"class":218},[201,685,228],{"class":218},[201,687,689,691,694,696,698],{"class":203,"line":688},28,[201,690,557],{"class":207},[201,692,693],{"class":389}," this",[201,695,123],{"class":218},[201,697,649],{"class":444},[201,699,373],{"class":218},[201,701,703],{"class":203,"line":702},29,[201,704,477],{"class":218},[201,706,708],{"class":203,"line":707},30,[201,709,262],{"class":218},[191,711,714],{"className":193,"code":712,"filename":713,"language":196,"meta":197,"style":197},"public class ConcreteIterator\u003CT> implements Iterator\u003CT> {\n    private final ConcreteCollection\u003CT> collection;\n    private int index = 0;\n\n    public ConcreteIterator(ConcreteCollection\u003CT> collection) {\n        this.collection = collection;\n    }\n\n    @Override\n    public boolean hasNext() {\n        return index \u003C collection.getSize();\n    }\n\n    @Override\n    public T next() {\n        return collection.getItems()[index++];\n    }\n}\n","ConcreteIterator.java",[39,715,716,743,762,777,781,803,818,822,826,832,845,862,866,870,876,888,908,912],{"__ignoreMap":197},[201,717,718,720,722,725,727,729,731,733,735,737,739,741],{"class":203,"line":204},[201,719,208],{"class":207},[201,721,331],{"class":207},[201,723,724],{"class":214}," ConcreteIterator",[201,726,219],{"class":218},[201,728,222],{"class":207},[201,730,225],{"class":218},[201,732,343],{"class":207},[201,734,277],{"class":214},[201,736,219],{"class":218},[201,738,222],{"class":207},[201,740,225],{"class":218},[201,742,228],{"class":218},[201,744,745,747,749,751,753,755,757,760],{"class":203,"line":231},[201,746,358],{"class":207},[201,748,361],{"class":207},[201,750,334],{"class":207},[201,752,219],{"class":218},[201,754,222],{"class":207},[201,756,225],{"class":218},[201,758,759],{"class":252}," collection",[201,761,373],{"class":218},[201,763,764,766,768,771,773,775],{"class":203,"line":259},[201,765,358],{"class":207},[201,767,380],{"class":207},[201,769,770],{"class":252}," index ",[201,772,386],{"class":218},[201,774,390],{"class":389},[201,776,373],{"class":218},[201,778,779],{"class":203,"line":305},[201,780,302],{"emptyLinePlaceholder":301},[201,782,783,785,787,789,791,793,795,797,799,801],{"class":203,"line":316},[201,784,418],{"class":207},[201,786,724],{"class":237},[201,788,241],{"class":218},[201,790,154],{"class":207},[201,792,219],{"class":218},[201,794,222],{"class":207},[201,796,225],{"class":218},[201,798,759],{"class":252},[201,800,431],{"class":218},[201,802,228],{"class":218},[201,804,805,807,809,812,814,816],{"class":203,"line":415},[201,806,439],{"class":389},[201,808,123],{"class":218},[201,810,811],{"class":444},"collection",[201,813,448],{"class":218},[201,815,759],{"class":252},[201,817,373],{"class":218},[201,819,820],{"class":203,"line":436},[201,821,477],{"class":218},[201,823,824],{"class":203,"line":474},[201,825,302],{"emptyLinePlaceholder":301},[201,827,828,830],{"class":203,"line":480},[201,829,612],{"class":218},[201,831,615],{"class":207},[201,833,834,836,839,841,843],{"class":203,"line":485},[201,835,418],{"class":207},[201,837,838],{"class":207}," boolean",[201,840,293],{"class":237},[201,842,549],{"class":218},[201,844,228],{"class":218},[201,846,847,849,851,853,855,857,860],{"class":203,"line":507},[201,848,557],{"class":207},[201,850,770],{"class":252},[201,852,219],{"class":218},[201,854,759],{"class":252},[201,856,123],{"class":218},[201,858,859],{"class":237},"getSize",[201,861,296],{"class":218},[201,863,864],{"class":203,"line":529},[201,865,477],{"class":218},[201,867,868],{"class":203,"line":534},[201,869,302],{"emptyLinePlaceholder":301},[201,871,872,874],{"class":203,"line":539},[201,873,612],{"class":218},[201,875,615],{"class":207},[201,877,878,880,882,884,886],{"class":203,"line":554},[201,879,418],{"class":207},[201,881,364],{"class":207},[201,883,311],{"class":237},[201,885,549],{"class":218},[201,887,228],{"class":218},[201,889,890,892,894,896,899,902,905],{"class":203,"line":564},[201,891,557],{"class":207},[201,893,759],{"class":252},[201,895,123],{"class":218},[201,897,898],{"class":237},"getItems",[201,900,901],{"class":218},"()[",[201,903,904],{"class":252},"index",[201,906,907],{"class":218},"++];\n",[201,909,910],{"class":203,"line":569},[201,911,477],{"class":218},[201,913,914],{"class":203,"line":574},[201,915,262],{"class":218},[191,917,920],{"className":193,"code":918,"filename":919,"language":196,"meta":197,"style":197},"public class Demo {\n    public static void main(String[] args) {\n        ConcreteCollection\u003CString> collection = new ConcreteCollection\u003CString>(5);\n        collection.add(\"item 1\");\n        collection.add(\"item 2\");\n        collection.add(\"item 3\");\n\n        collection.createIterator(new ConcreteIterator\u003CString>(collection));\n\n        Iterator\u003CString> iterator = collection.getIterator();\n\n        while (iterator.hasNext()) {\n            System.out.println(iterator.next());\n        }\n    }\n}\n","Demo.java",[39,921,922,933,959,991,1009,1024,1039,1043,1070,1074,1099,1103,1122,1149,1154,1158],{"__ignoreMap":197},[201,923,924,926,928,931],{"class":203,"line":204},[201,925,208],{"class":207},[201,927,331],{"class":207},[201,929,930],{"class":214}," Demo",[201,932,228],{"class":218},[201,934,935,937,940,942,945,947,950,952,955,957],{"class":203,"line":231},[201,936,418],{"class":207},[201,938,939],{"class":207}," static",[201,941,490],{"class":207},[201,943,944],{"class":237}," main",[201,946,241],{"class":218},[201,948,949],{"class":207},"String",[201,951,367],{"class":218},[201,953,954],{"class":252}," args",[201,956,431],{"class":218},[201,958,228],{"class":218},[201,960,961,964,966,968,970,973,975,977,979,981,983,986,989],{"class":203,"line":259},[201,962,963],{"class":207},"        ConcreteCollection",[201,965,219],{"class":218},[201,967,949],{"class":207},[201,969,225],{"class":218},[201,971,972],{"class":252}," collection ",[201,974,386],{"class":218},[201,976,459],{"class":207},[201,978,334],{"class":207},[201,980,219],{"class":218},[201,982,949],{"class":207},[201,984,985],{"class":218},">(",[201,987,988],{"class":389},"5",[201,990,256],{"class":218},[201,992,993,996,998,1001,1003,1007],{"class":203,"line":305},[201,994,995],{"class":252},"        collection",[201,997,123],{"class":218},[201,999,1000],{"class":237},"add",[201,1002,241],{"class":218},[201,1004,1006],{"class":1005},"sUOzB","\"item 1\"",[201,1008,256],{"class":218},[201,1010,1011,1013,1015,1017,1019,1022],{"class":203,"line":316},[201,1012,995],{"class":252},[201,1014,123],{"class":218},[201,1016,1000],{"class":237},[201,1018,241],{"class":218},[201,1020,1021],{"class":1005},"\"item 2\"",[201,1023,256],{"class":218},[201,1025,1026,1028,1030,1032,1034,1037],{"class":203,"line":415},[201,1027,995],{"class":252},[201,1029,123],{"class":218},[201,1031,1000],{"class":237},[201,1033,241],{"class":218},[201,1035,1036],{"class":1005},"\"item 3\"",[201,1038,256],{"class":218},[201,1040,1041],{"class":203,"line":436},[201,1042,302],{"emptyLinePlaceholder":301},[201,1044,1045,1047,1049,1052,1054,1057,1059,1061,1063,1065,1067],{"class":203,"line":474},[201,1046,995],{"class":252},[201,1048,123],{"class":218},[201,1050,1051],{"class":237},"createIterator",[201,1053,241],{"class":218},[201,1055,1056],{"class":207},"new",[201,1058,724],{"class":207},[201,1060,219],{"class":218},[201,1062,949],{"class":207},[201,1064,985],{"class":218},[201,1066,811],{"class":252},[201,1068,1069],{"class":218},"));\n",[201,1071,1072],{"class":203,"line":480},[201,1073,302],{"emptyLinePlaceholder":301},[201,1075,1076,1079,1081,1083,1085,1088,1090,1092,1094,1097],{"class":203,"line":485},[201,1077,1078],{"class":207},"        Iterator",[201,1080,219],{"class":218},[201,1082,949],{"class":207},[201,1084,225],{"class":218},[201,1086,1087],{"class":252}," iterator ",[201,1089,386],{"class":218},[201,1091,759],{"class":252},[201,1093,123],{"class":218},[201,1095,1096],{"class":237},"getIterator",[201,1098,296],{"class":218},[201,1100,1101],{"class":203,"line":507},[201,1102,302],{"emptyLinePlaceholder":301},[201,1104,1105,1108,1110,1112,1114,1117,1120],{"class":203,"line":529},[201,1106,1107],{"class":207},"        while",[201,1109,451],{"class":218},[201,1111,649],{"class":252},[201,1113,123],{"class":218},[201,1115,1116],{"class":237},"hasNext",[201,1118,1119],{"class":218},"())",[201,1121,228],{"class":218},[201,1123,1124,1127,1129,1132,1134,1137,1139,1141,1143,1146],{"class":203,"line":534},[201,1125,1126],{"class":252},"            System",[201,1128,123],{"class":218},[201,1130,1131],{"class":444},"out",[201,1133,123],{"class":218},[201,1135,1136],{"class":237},"println",[201,1138,241],{"class":218},[201,1140,649],{"class":252},[201,1142,123],{"class":218},[201,1144,1145],{"class":237},"next",[201,1147,1148],{"class":218},"());\n",[201,1150,1151],{"class":203,"line":539},[201,1152,1153],{"class":218},"        }\n",[201,1155,1156],{"class":203,"line":554},[201,1157,477],{"class":218},[201,1159,1160],{"class":203,"line":564},[201,1161,262],{"class":218},[32,1163,1164,1165,1167],{},"Running ",[39,1166,170],{}," prints:",[191,1169,1174],{"className":1170,"code":1172,"language":1173,"meta":197},[1171],"language-text","item 1\nitem 2\nitem 3\n","text",[39,1175,1172],{"__ignoreMap":197},[1177,1178,1179],"style",{},"html pre.shiki code .sVsk0, html code.shiki .sVsk0{--shiki-default:#9D9FFE;--shiki-light:#6762D3}html pre.shiki code .sNwy7, html code.shiki .sNwy7{--shiki-default:#D4D7FE;--shiki-light:#3F3B87}html pre.shiki code .sVG6I, html code.shiki .sVG6I{--shiki-default:#85858F;--shiki-light:#71717A}html pre.shiki code .sVz7n, html code.shiki .sVz7n{--shiki-default:#EDEDEF;--shiki-light:#0B0B0C}html pre.shiki code .sWFPS, html code.shiki .sWFPS{--shiki-default:#C4C4CC;--shiki-light:#3F3F46}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html pre.shiki code .sheII, html code.shiki .sheII{--shiki-default:#70ADFF;--shiki-light:#195CAE}html pre.shiki code .s4Awn, html code.shiki .s4Awn{--shiki-default:#C5DDFE;--shiki-light:#534DAF}html pre.shiki code .sUOzB, html code.shiki .sUOzB{--shiki-default:#9EC7FE;--shiki-light:#2873D1}",{"title":197,"searchDepth":231,"depth":231,"links":1181},[1182,1183,1184,1188,1189],{"id":29,"depth":231,"text":30},{"id":56,"depth":231,"text":57},{"id":76,"depth":231,"text":77,"children":1185},[1186,1187],{"id":81,"depth":259,"text":82},{"id":96,"depth":259,"text":97},{"id":108,"depth":231,"text":109},{"id":188,"depth":231,"text":189},"The Iterator pattern lets you walk through a collection item by item without knowing how it stores its items inside. Explained with a worked Java example of an iterator over a collection built on an array.","A small collection backed by an array","md","Behavioral","lucide:list-ordered",{},"\u002Flearn\u002Fdesign-patterns\u002Fiterator","---\ntitle: Iterator\ngroup: Behavioral\nicon: lucide:list-ordered\nsummary: 'Walk through the items of a collection one by one, without knowing how the collection stores them.'\ndescription: 'The Iterator pattern lets you walk through a collection item by item without knowing how it stores its items inside. Explained with a worked Java example of an iterator over a collection built on an array.'\nexample: 'A small collection backed by an array'\norder: 3\nsource: https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FBehavioral\u002FIterator\n---\n\n## The idea\n\nA collection might keep its items in an array, a linked list, a tree or a hash table. If client code loops over the array directly, it depends on that choice. Change the storage, and every loop has to change too.\n\nThe Iterator pattern moves the job of walking through the items into a separate object, the iterator. It answers two questions: is there another item (`hasNext()`), and what is it (`next()`). The iterator knows how the collection is stored, and the client only knows the iterator's interface.\n\nSince each iterator keeps its own position, several iterators can walk the same collection at the same time without getting in each other's way. Java's `java.util.Iterator` and its for-each loop are built on this pattern.\n\n## When to use it\n\n- You want to hide how a collection stores its items from the code that uses it.\n- You need one way to loop over several kinds of collections.\n- You want more than one way to walk the same collection, such as forwards, backwards or filtered.\n- You need several loops over the same collection at once, each with its own position.\n\n## Pros and cons\n\n### Pros\n\n- Clients walk a collection without knowing how it is stored, so different collections can be used in the same way.\n- Items are reached in order, one at a time, without exposing the collection's internal structure.\n- Several iterators can work on the same collection at once, each with its own position.\n\n### Cons\n\n- It adds objects and classes to the system: an iterator for every kind of collection.\n- For a simple array or list, a plain loop is shorter and easier to read.\n\n## The example\n\nThe example stores strings in a fixed-size array and prints them through an iterator. It defines its own `Iterator` and `Collection` interfaces rather than using the ones in `java.util`.\n\n- **`Collection`**: the aggregate interface, with `createIterator()`.\n- **`Iterator`**: the iterator interface, with `hasNext()` and `next()`.\n- **`ConcreteCollection`**: the concrete aggregate. It stores items in an array and tracks how many have been added.\n- **`ConcreteIterator`**: the concrete iterator. It keeps an index and reads items from the collection until it reaches the collection's size.\n- **`Demo`**: adds three items, gives the collection an iterator, then loops with `hasNext()` and `next()`.\n\nIn the Gang of Four version, `createIterator()` creates a new iterator and returns it. Here it receives an iterator from the caller and stores it, and `getIterator()` hands it back.\n\n## The code\n\n```java [Collection.java]\npublic interface Collection\u003CT> {\n    void createIterator(Iterator\u003CT> iterator);\n}\n```\n\n```java [Iterator.java]\npublic interface Iterator\u003CT> {\n    boolean hasNext();\n\n    T next();\n}\n```\n\n```java [ConcreteCollection.java]\npublic class ConcreteCollection\u003CT> implements Collection\u003CT> {\n    private final T[] items;\n    private int size = 0;\n    private Iterator\u003CT> iterator;\n\n    public ConcreteCollection(int size) {\n        this.items = (T[]) new Object[size];\n    }\n\n    public void add(T item) {\n        this.items[size++] = item;\n    }\n\n    public int getSize() {\n        return size;\n    }\n\n    public T[] getItems() {\n        return items;\n    }\n\n    @Override\n    public void createIterator(Iterator\u003CT> iterator) {\n        this.iterator = iterator;\n    }\n\n    public Iterator\u003CT> getIterator() {\n        return this.iterator;\n    }\n}\n```\n\n```java [ConcreteIterator.java]\npublic class ConcreteIterator\u003CT> implements Iterator\u003CT> {\n    private final ConcreteCollection\u003CT> collection;\n    private int index = 0;\n\n    public ConcreteIterator(ConcreteCollection\u003CT> collection) {\n        this.collection = collection;\n    }\n\n    @Override\n    public boolean hasNext() {\n        return index \u003C collection.getSize();\n    }\n\n    @Override\n    public T next() {\n        return collection.getItems()[index++];\n    }\n}\n```\n\n```java [Demo.java]\npublic class Demo {\n    public static void main(String[] args) {\n        ConcreteCollection\u003CString> collection = new ConcreteCollection\u003CString>(5);\n        collection.add(\"item 1\");\n        collection.add(\"item 2\");\n        collection.add(\"item 3\");\n\n        collection.createIterator(new ConcreteIterator\u003CString>(collection));\n\n        Iterator\u003CString> iterator = collection.getIterator();\n\n        while (iterator.hasNext()) {\n            System.out.println(iterator.next());\n        }\n    }\n}\n```\n\nRunning `Demo` prints:\n\n```text\nitem 1\nitem 2\nitem 3\n```\n",{"title":22,"description":1190},{"loc":1196},"https:\u002F\u002Fgithub.com\u002FHaythamasalama\u002FdesignPattern\u002Ftree\u002Fmaster\u002Fsrc\u002FBehavioral\u002FIterator","learn\u002Fdesign-patterns\u002Fiterator","Walk through the items of a collection one by one, without knowing how the collection stores them.","wyXx4FyklF866WZVw9I7LjUZ9nn9xIFARcZqeK4mmvA",[1205,1210,1215,1216,1221,1226,1231,1236],{"path":1206,"title":1207,"icon":1208,"summary":1209,"order":204},"\u002Flearn\u002Fdesign-patterns\u002Fchain-of-responsibility","Chain of Responsibility","lucide:link","Pass a request along a chain of handlers, where each one can deal with it or hand it to the next.",{"path":1211,"title":1212,"icon":1213,"summary":1214,"order":231},"\u002Flearn\u002Fdesign-patterns\u002Fcommand","Command","lucide:square-terminal","Turn a request into an object, so it can be passed around, queued, logged or undone.",{"path":1196,"title":22,"icon":1194,"summary":1202,"order":259},{"path":1217,"title":1218,"icon":1219,"summary":1220,"order":305},"\u002Flearn\u002Fdesign-patterns\u002Fobserver","Observer","lucide:bell","One object announces a change, and every object that subscribed to it hears about it.",{"path":1222,"title":1223,"icon":1224,"summary":1225,"order":316},"\u002Flearn\u002Fdesign-patterns\u002Fstate","State","lucide:toggle-left","An object changes its behaviour when its internal state changes, as if it had changed its class.",{"path":1227,"title":1228,"icon":1229,"summary":1230,"order":415},"\u002Flearn\u002Fdesign-patterns\u002Fstrategy","Strategy","lucide:shuffle","Put each way of doing a task in its own class, and choose which one to use while the program runs.",{"path":1232,"title":1233,"icon":1234,"summary":1235,"order":436},"\u002Flearn\u002Fdesign-patterns\u002Ftemplate-method","Template Method","lucide:layout-template","A base class fixes the steps of a task, and subclasses fill in the steps that differ.",{"path":1237,"title":1238,"icon":1239,"summary":1240,"order":474},"\u002Flearn\u002Fdesign-patterns\u002Fvisitor","Visitor","lucide:footprints","Add new operations to a set of classes without changing them, by moving each operation into its own visitor.",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1242},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M15 14c.2-1 .7-1.7 1.5-2.5c1-.9 1.5-2.2 1.5-3.5A6 6 0 0 0 6 8c0 1 .2 2.2 1.5 3.5c.7.7 1.3 1.5 1.5 2.5m0 4h6m-5 4h4\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1244},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Crect width=\"8\" height=\"8\" x=\"3\" y=\"3\" rx=\"2\"\u002F>\u003Cpath d=\"M7 11v4a2 2 0 0 0 2 2h4\"\u002F>\u003Crect width=\"8\" height=\"8\" x=\"13\" y=\"13\" rx=\"2\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1246},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71\"\u002F>\u003Cpath d=\"M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1248},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"m7 11l2-2l-2-2m4 6h4\"\u002F>\u003Crect width=\"18\" height=\"18\" x=\"3\" y=\"3\" rx=\"2\" ry=\"2\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1250},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M11 5h10m-10 7h10m-10 7h10M4 4h1v5M4 9h2m.5 11H3.4c0-1 2.6-1.925 2.6-3.5a1.5 1.5 0 0 0-2.6-1.02\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1252},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M10.268 21a2 2 0 0 0 3.464 0m-10.47-5.674A1 1 0 0 0 4 17h16a1 1 0 0 0 .74-1.673C19.41 13.956 18 12.499 18 8A6 6 0 0 0 6 8c0 4.499-1.411 5.956-2.738 7.326\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1254},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Ccircle cx=\"9\" cy=\"12\" r=\"3\"\u002F>\u003Crect width=\"20\" height=\"14\" x=\"2\" y=\"5\" rx=\"7\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1256},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Cpath d=\"m18 14l4 4l-4 4m0-20l4 4l-4 4\"\u002F>\u003Cpath d=\"M2 18h1.973a4 4 0 0 0 3.3-1.7l5.454-8.6a4 4 0 0 1 3.3-1.7H22M2 6h1.972a4 4 0 0 1 3.6 2.2M22 18h-6.041a4 4 0 0 1-3.3-1.8l-.359-.45\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1258},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Crect width=\"18\" height=\"7\" x=\"3\" y=\"3\" rx=\"1\"\u002F>\u003Crect width=\"9\" height=\"7\" x=\"3\" y=\"14\" rx=\"1\"\u002F>\u003Crect width=\"5\" height=\"7\" x=\"16\" y=\"14\" rx=\"1\"\u002F>\u003C\u002Fg>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1260},"\u003Cpath fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\" d=\"M4 16v-2.38C4 11.5 2.97 10.5 3 8c.03-2.72 1.49-6 4.5-6C9.37 2 10 3.8 10 5.5c0 3.11-2 5.66-2 8.68V16a2 2 0 1 1-4 0m16 4v-2.38c0-2.12 1.03-3.12 1-5.62c-.03-2.72-1.49-6-4.5-6C14.63 6 14 7.8 14 9.5c0 3.11 2 5.66 2 8.68V20a2 2 0 1 0 4 0m-4-3h4M4 13h4\"\u002F>",{"left":4,"top":4,"width":5,"height":5,"rotate":4,"vFlip":6,"hFlip":6,"body":1262},"\u003Cg fill=\"none\" stroke=\"currentColor\" stroke-linecap=\"round\" stroke-linejoin=\"round\" stroke-width=\"2\">\u003Crect width=\"14\" height=\"14\" x=\"8\" y=\"8\" rx=\"2\" ry=\"2\"\u002F>\u003Cpath d=\"M4 16c-1.1 0-2-.9-2-2V4c0-1.1.9-2 2-2h10c1.1 0 2 .9 2 2\"\u002F>\u003C\u002Fg>",1793131416852]