Chapter 11 - Java Collections Framework

Chapter 11 - Java Collections Framework - &KDSWHU...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: &KDSWHU -DYD &ROOHFWLRQV )UDPHZRUN &RS\ULJKW E\ 6WXDUW 5HJHV DQG 0DUW\ 6WHSS /LVWV &ROOHFWLRQV /LQNHG/LVW YHUVXV $UUD\/LVW ,WHUDWRUV /LQNHG/LVW ([DPSOH 6LHYH $EVWUDFW 'DWD 7\SHV $'7V 6HWV 6HW &RQFHSWV 7UHH6HW YHUVXV +DVK6HW 6HW 2SHUDWLRQV 6HW ([DPSOH /RWWHU\ 0DSV %DVLF 0DS 2SHUDWLRQV 0DS 9LHZV NH\6HW DQG YDOXHV 7UHH0DS YHUVXV +DVK0DS 0DS ([DPSOH :RUG&RXQW &ROOHFWLRQ 2YHUYLHZ ,QWURGXFWLRQ 7KH SUHYLRXV FKDSWHU H[SORUHG WKH $UUD\/LVW FODVV $UUD\/LVWV DUH RQH RI PDQ\ ZD\V WR VWRUH GDWD LQ -DYD ,Q WKLV FKDSWHU ZH OO H[SORUH -DYD V IUDPHZRUN RI GLIIHUHQW GDWD VWRUDJH FROOHFWLRQV LQFOXGLQJ OLQNHG OLVWV VHWV DQG PDSV :H OO VHH KRZ WR XVH WKHVH VWUXFWXUHV WRJHWKHU WR PDQLSXODWH DQG H[DPLQH GDWD LQ PDQ\ ZD\V WR VROYH SURJUDPPLQJ SUREOHPV :H OO VHH D QHZ W\SH RI OLVW FDOOHG D OLQNHG OLVW WKDW VWRUHV LWV GDWD GLIIHUHQWO\ WKDQ DQ $UUD\/LVW EXW VXSSRUWV WKH VDPH RSHUDWLRQV :H OO DOVR VHH FROOHFWLRQV FDOOHG VHWV WKDW GRQ W DOORZ GXSOLFDWH HOHPHQWV DQG DUH JRRG IRU VHDUFKLQJ $QRWKHU FROOHFWLRQ W\SH ZH OO VHH LV WKH PDS ZKLFK FUHDWHV DVVRFLDWLRQV EHWZHHQ SDLUV RI GDWD YDOXHV :H OO DOVR OHDUQ DERXW WKH QRWLRQ RI DEVWUDFW GDWD W\SHV $'7V DV D ZD\ WR VHSDUDWH WKH FDSDELOLWLHV RI D FROOHFWLRQ IURP WKH GHWDLOV RI LWV LPSOHPHQWDWLRQ 573 /LVWV 7KH $UUD\/LVW FODVV IURP &KDSWHU KDV VHYHUDO DGYDQWDJHV RYHU DQ DUUD\ LW NHHSV WUDFN RI LWV VL]H IRU \RX LW DOORZV LQVHUWLRQ DQG UHPRYDO DW DUELWUDU\ SODFHV LQ WKH DUUD\ DQG LW UHVL]HV IRU \RX LI LW JHWV IXOO ,Q WKLV VHFWLRQ ZH OO OHDUQ DERXW DQ REMHFW QDPHG /LQNHG/LVW ZKLFK LV VLPLODU WR $UUD\/LVW :H OO DOVR H[SORUH VRPH FRQFHSWV DERXW JHQHUDOL]LQJ FROOHFWLRQV DQG D XVHIXO REMHFW QDPHG DQ LWHUDWRU WKDW OHWV XV H[DPLQH WKH HOHPHQWV RI DQ\ FROOHFWLRQ &ROOHFWLRQV $Q $UUD\/LVW LV DQ H[DPSOH RI D FROOHFWLRQ ZKLFK LV DQ REMHFW WKDW VWRUHV D JURXS RI RWKHU REMHFWV ZKLFK DUH FDOOHG WKH HOHPHQWV RI WKH FROOHFWLRQ $ FROOHFWLRQ LV VRPHWLPHV DOVR FDOOHG D GDWD VWUXFWXUH &ROOHFWLRQ $Q REMHFW WKDW VWRUHV D JURXS RI RWKHU REMHFWV FDOOHG HOHPHQWV &ROOHFWLRQV DUH GLIIHUHQWLDWHG E\ WKH W\SHV RI HOHPHQWV WKH\ VWRUH WKH RSHUDWLRQV WKH\ DOORZ \RX WR SHUIRUP RQ WKH HOHPHQWV DQG WKH VSHHG RU HIILFLHQF\ RI WKRVH RSHUDWLRQV 6RPH H[DPSOHV RI FROOHFWLRQV DUH /LVW $Q RUGHUHG FROOHFWLRQ RI HOHPHQWV QXPEHUHG ZLWK LQGH[HV 6WDFN $ FROOHFWLRQ ZKHUH HOHPHQWV PD\ RQO\ EH DGGHG DQG UHPRYHG DW WKH WRS HQG DOORZLQJ IRU /DVW ,Q )LUVW 2XW /,)2 HOHPHQW DFFHVV RUGHU 4XHXH $ FROOHFWLRQ ZKHUH HOHPHQWV PD\ RQO\ EH DGGHG WR WKH EDFN HQG DQG RQO\ PD\ EH UHPRYHG IURP WKH IURQW HQG DOORZLQJ IRU )LUVW ,Q )LUVW 2XW ),)2 HOHPHQW DFFHVV RUGHU 6HW $ JURXS RI HOHPHQWV WKDW PD\ QRW FRQWDLQ GXSOLFDWHV DQG JHQHUDOO\ PD\ EH VHDUFKHG YHU\ TXLFNO\ 0DS $ VHW RI SDLUZLVH PDSSLQJV EHWZHHQ D VHW RI HOHPHQWV FDOOHG NH\V DQG D FROOHFWLRQ RI RWKHU HOHPHQWV QDPHG YDOXHV -DYD SURYLGHV D ODUJH JURXS RI XVHIXO FROOHFWLRQV WKDW DOORZ \RX WR VWRUH DFFHVV VHDUFK VRUW DQG PDQLSXODWH GDWD LQ D YDULHW\ RI ZD\V 7RJHWKHU WKHVH FROOHFWLRQV DQG FODVVHV DUH NQRZQ DV WKH -DYD &ROOHFWLRQV )UDPHZRUN 7KLV IUDPHZRUN LV ODUJHO\ FRQWDLQHG LQ WKH SDFNDJH MDYD XWLO 7KHUH V DQ LQWHUIDFH LQ WKH MDYD XWLO SDFNDJH QDPHG &ROOHFWLRQ WKDW HYHU\ FROOHFWLRQ LPSOHPHQWV 7KLV LQWHUIDFH VSHFLILHV WKH RSHUDWLRQV WKDW PRVW FROOHFWLRQV VXSSRUW 7KH IROORZLQJ LV D TXLFN OLVW RI WKRVH RSHUDWLRQV 8VHIXO 0HWKRGV RI WKH &ROOHFWLRQ ,QWHUIDFH 0HWKRG add(element) addAll(collection) clear() contains(element) 'HVFULSWLRQ HQVXUHV WKDW WKLV FROOHFWLRQ FRQWDLQV WKH VSHFLILHG HOHPHQW DGGV DOO HOHPHQWV IURP WKH JLYHQ FROOHFWLRQ WR WKLV FROOHFWLRQ UHPRYHV DOO HOHPHQWV IURP WKLV FROOHFWLRQ UHWXUQV WUXH LI WKLV FROOHFWLRQ FRQWDLQV WKH JLYHQ HOHPHQW 574 containsAll(collection) isEmpty() iterator() remove(element) removeAll(collection) retainAll(collection) size() toArray() UHWXUQV WUXH LI WKLV FROOHFWLRQ FRQWDLQV DOO HOHPHQWV RI WKH JLYHQ FROOHFWLRQ UHWXUQV WUXH LI WKLV FROOHFWLRQ FRQWDLQV QR HOHPHQWV UHWXUQV DQ REMHFW WKDW KHOSV WUDYHUVH WKH HOHPHQWV RI WKLV FROOHFWLRQ UHPRYHV RQH RFFXUUHQFH RI WKH VSHFLILHG HOHPHQW LI LW LV FRQWDLQHG LQ WKLV FROOHFWLRQ UHPRYHV DOO HOHPHQWV RI WKH JLYHQ FROOHFWLRQ IURP WKLV FROOHFWLRQ UHPRYHV DOO HOHPHQWV QRW IRXQG LQ WKH JLYHQ FROOHFWLRQ IURP WKLV FROOHFWLRQ UHWXUQV WKH QXPEHU RI HOHPHQWV LQ WKLV FROOHFWLRQ UHWXUQV DQ DUUD\ FRQWDLQLQJ WKH HOHPHQWV RI WKLV FROOHFWLRQ 7KH &ROOHFWLRQ LQWHUIDFH LV H[WHQGHG DQG LPSOHPHQWHG E\ WKH RWKHU LQWHUIDFHV DQG FODVVHV LQ WKH -DYD &ROOHFWLRQ )UDPHZRUN 7KH IROORZLQJ GLDJUDP VXPPDUL]HV WKH YDULRXV LQWHUIDFHV DQG ZKDW FODVVHV LPSOHPHQW WKHP 7KHVH FODVVHV ZLOO EH GLVFXVVHG LQ WKLV FKDSWHU $Q DEULGJHG YLHZ RI WKH -DYD &ROOHFWLRQV )UDPHZRUN 1RZ ZH OO ORRN DW D FROOHFWLRQ QDPHG /LQNHG/LVW DQG FRPSDUH LW WR RXU H[LVWLQJ NQRZOHGJH RI $UUD\/LVW /LQNHG/LVW YHUVXV $UUD\/LVW $UUD\/LVW LV D SRZHUIXO DQG XVHIXO FROOHFWLRQ EXW WKHUH DUH VRPH FDVHV ZKHUH $UUD\/LVWV DUHQ W LGHDO )RU H[DPSOH FRQVLGHU D SURJUDP WKDW ZDQWV WR UHPRYH HDFK 6WULQJ RI HYHQ OHQJWK IURP DQ $UUD\/LVW RI VWULQJV $ VWUDLJKWIRUZDUG ZD\ WR LPSOHPHQW VXFK DQ RSHUDWLRQ PLJKW EH WKH IROORZLQJ 575 // Removes all strings of even length from // the given array list. public static void removeEvenLength(ArrayList<String> list) { for (int i = 0; i < list.size(); i += 2) { String element = list.get(i); if (element.length() % 2 == 0) { list.remove(i); i--; } } } 7KH SUHFHGLQJ FRGH LV FRUUHFW EXW LW WXUQV RXW WKDW LW GRHVQ W SHUIRUP ZHOO ZKHQ WKH OLVW KDV D ORW RI HOHPHQWV ,W WDNHV VHYHUDO PLQXWHV WR SURFHVV D OLVW RI D PLOOLRQ HOHPHQWV 7KH UHDVRQ LW V VR VORZ LV WKDW HYHU\ WLPH ZH UHPRYH DQ HOHPHQW IURP WKH OLVW ZH KDYH WR VKLIW DOO VXEVHTXHQW HOHPHQWV WR WKH OHIW E\ RQH 'RLQJ WKLV PDQ\ WLPHV DGGV XS WR FDXVH D VORZ SURJUDP $QRWKHU FDVH ZKHQ DQ $UUD\/LVW EHKDYHV VORZO\ LV ZKHQ ZH ZDQW WR XVH LW WR PRGHO D ZDLWLQJ OLQH RU TXHXH ZKHUH HOHPHQWV DUH DOZD\V DGGHG DW WKH HQG RI WKH OLVW DQG DOZD\V UHPRYHG IURP WKH IURQW 3VHXGRFRGH IRU VXFK D TXHXH PLJKW ORRN OLNH WKLV if (a customer has arrived) { list.add(customer). } else { first customer in line = list.remove(0). process first customer. } (DFK UHPRYDO LV D VORZ RSHUDWLRQ RQ D ODUJH OLVW EHFDXVH ZH KDYH WR VOLGH DOO HOHPHQWV WR WKH OHIW E\ RQH 7KHUH V DQRWKHU W\SH RI FROOHFWLRQ FDOOHG D OLQNHG OLVW WKDW FDQ JLYH EHWWHU SHUIRUPDQFH LQ SUREOHPV OLNH WKHVH WKDW LQYROYH D ORW RI DGGLQJ RU UHPRYDO IURP WKH IURQW RU PLGGOH RI D OLVW $ OLQNHG OLVW SURYLGHV WKH VDPH RSHUDWLRQV DV DQ DUUD\ OLVW VXFK DV DGG UHPRYH LV(PSW\ VL]H DQG FRQWDLQV %XW D OLQNHG OLVW VWRUHV LWV HOHPHQWV LQ D IXQGDPHQWDOO\ GLIIHUHQW ZD\ 5DWKHU WKDQ LQ DQ DUUD\ HOHPHQWV DUH VWRUHG LQWR VPDOO LQGLYLGXDO FRQWDLQHUV QDPHG QRGHV 7KH QRGHV DUH OLQNHG WRJHWKHU E\ KDYLQJ HDFK QRGH VWRUH D UHIHUHQFHV WR WKH QH[W QRGH LQ WKH OLVW 7KH RYHUDOO OLQNHG OLVW REMHFW NHHSV UHIHUHQFHV WR WKH IURQW DQG EDFN QRGHV /LQNHG /LVW $ FROOHFWLRQ WKDW VWRUHV D OLVW RI HOHPHQWV LQ VPDOO REMHFW FRQWDLQHUV QDPHG QRGHV ZKLFK DUH OLQNHG WRJHWKHU E\ UHIHUHQFHV 2QH ZD\ WR WKLQN RI D OLQNHG OLVW LV DV DQ DUUD\ OLVW EURNHQ DSDUW ZKHUH HDFK HOHPHQW LV VWRUHG LQ D VPDOO ER[ D QRGH FRQQHFWHG WR D QHLJKERULQJ ER[ E\ DQ DUURZ 576 array list : 0 1 2 3 4 +---+---+---+---+---+ | a | b | c | d | e | +---+---+---+---+---+ +---+ front --> | a | +---+ --> <-+---+ | b | +---+ --> <-+---+ | c | +---+ --> <-+---+ | d | +---+ --> <-+---+ | e | <-- back +---+ linked list: 2QH PDMRU DGYDQWDJH RI D OLQNHG OLVW LV WKDW HOHPHQWV FDQ JHQHUDOO\ EH DGGHG DW WKH IURQW RU EDFN RI WKH OLVW TXLFNO\ EHFDXVH UDWKHU WKDQ VOLGLQJ PDQ\ HOHPHQWV LQ DQ DUUD\ WKH OLVW MXVW FUHDWHV D QHZ QRGH REMHFW DQG OLQNV LW WR WKH RWKHUV LQ WKH OLVW :H GRQ W KDYH WR GR WKLV RXUVHOYHV PDQXDOO\ WKH OLVW WDNHV FDUH RI LW IRU XV LQWHUQDOO\ 7KH IROORZLQJ GLDJUDP VKRZV ZKDW KDSSHQV LQVLGH WKH OLQNHG OLVW ZKHQ DQ HOHPHQW LV DGGHG $GGLQJ DQ HOHPHQW WR WKH IURQW RI D OLQNHG OLVW list.add(0, "x"); 1. make a new node to hold the new element +---+ | x | +---+ 2. connect the new node to the other nodes in the list +---+ | x | +---+ | | v ^ | | --> <-+---+ | b | +---+ --> <-+---+ | c | +---+ --> <-+---+ | d | +---+ --> <-+---+ | e | <-- back +---+ +---+ front --> | a | +---+ 3. change front of list to point to the new node +---+ front --> | x | +---+ --> <-+---+ | a | +---+ --> <-+---+ | b | +---+ --> <-+---+ | c | +---+ --> <-+---+ | d | +---+ --> +---+ | e | <-- back <-- +---+ 7R XVH D OLQNHG OLVW LQ -DYD FUHDWH DQ REMHFW RI W\SH /LQNHG/LVW LQVWHDG RI W\SH $UUD\/LVW /LQNHG/LVW REMHFWV KDYH WKH VDPH PHWKRGV \RX YH XVHG LQ $UUD\/LVW LinkedList<String> words = new LinkedList<String>(); words.add("hello"); words.add("goodbye"); words.add("this"); words.add("that"); 577 ,WHUDWRUV 7KH PDMRU GUDZEDFN WR OLQNHG OLVWV LV WKDW WKH\ GRQ W SURYLGH IDVW UDQGRP DFFHVV WKDW LV \RX FDQ W H[DPLQH DQ DUELWUDU\ HOHPHQW RI WKH OLVW HIILFLHQWO\ 7KH UHDVRQ LV EHFDXVH D OLQNHG OLVW FDQQRW MXPS GLUHFWO\ WR WKH HOHPHQW DW D JLYHQ LQGH[ DV \RX FDQ ZLWK DQ DUUD\ WKH RQO\ ZD\ LW FDQ UHDFK DQ DUELWUDU\ HOHPHQW LV E\ VWDUWLQJ DW WKH IURQW RI WKH OLVW DQG WUDYHUVLQJ WKH OLVW QRGHV QH[W UHIHUHQFHV WR JHW WR WKH LQGH[ \RX GHVLUHG 7KH FRGH WKDW WKH /LQNHG/LVW XVHV WR LPSOHPHQW LWV JHW PHWKRG IRU H[DPSOH ORRNV URXJKO\ OLNH WKLV get(int i) { Node temp = front. while (temp has not reached element i) { temp = temp's next. } return temp's element. } (YHU\ WLPH \RX FDOO JHW VHW DGG RU UHPRYH RQ D OLQNHG OLVW LQWHUQDOO\ WKH OLVW V FRGH UXQV D ORRS WKDW DGYDQFHV WR WKDW LQGH[ 7KLV PHDQV WKDW WKHVH PHWKRGV SHUIRUP VORZO\ RQ D /LQNHG/LVW HVSHFLDOO\ LI \RX FDOO WKHP PDQ\ WLPHV RU FDOO WKHP RQ D OLVW ZLWK PDQ\ HOHPHQWV 7KH IROORZLQJ GLDJUDP VKRZV WKH EHKDYLRU RI D ODUJH OLQNHG OLVW ZKHQ UHWULHYLQJ WKH HOHPHQW DW LQGH[ 5HWULHYLQJ DQ HOHPHQW IURP DQ DUELWUDU\ SRLQW LQ D OLQNHG OLVW list.get(901); 1. create a temporary reference to the front of the list temp ------+ | v 1 +---+ | a | +---+ 2 +---+ | b | <-- +---+ --> 900 +---+ | c | <-- +---+ 901 +---+ | d | <-- +---+ --> 902 +---+ | e | <-<-- +---+ --> 0 +---+ front --> | x | +---+ --> <-- --> ... 2. advance the temporary reference 900 times temp ----------------------------------------------------------------+ | > > > > > v 1 +---+ | a | +---+ 2 +---+ | b | <-- +---+ --> 900 +---+ | c | <-- +---+ 901 +---+ | d | <-- +---+ --> 902 +---+ | e | <-<-- +---+ --> 0 +---+ front --> | x | +---+ --> <-- --> ... 3. return the data element value stored in this node 7KH SUREOHP LV FRPSRXQGHG ZKHQ \RX FDOO JHW RU RWKHU PHWKRGV WKDW DFFHSW DQ LQGH[ DV D SDUDPHWHU PXOWLSOH WLPHV VXFK DV RQ HDFK HOHPHQW RI WKH OLQNHG OLVW ,I DIWHU WKH DERYH FDOO RI JHW ZH GHFLGHG WR FDOO JHW WKH OLQNHG OLVW ZRXOG DJDLQ KDYH WR VWDUW IURP WKH IURQW DQG DGYDQFH WR LQGH[ 7KH RQO\ FDVH ZKHUH WKHVH PHWKRGV UXQ TXLFNO\ LV ZKHQ \RX SDVV DQ LQGH[ QHDU WKH IURQW RU EDFN RI WKH OLVW VXFK DV RU WKH OLVW V VL]H PLQXV 578 (DUOLHU ZH VDZ D PHWKRG WR UHPRYH 6WULQJV RI HYHQ OHQJWK IURP DQ $UUD\/LVW ,I ZH DGDSWHG WKLV FRGH WR XVH D /LQNHG/LVW DQG PDGH QR RWKHU PRGLILFDWLRQV LW ZRXOG UXQV YHU\ VORZO\ RQ ODUJH OLVWV EHFDXVH LW FDOOV WKH JHW DQG UHPRYH PHWKRGV PDQ\ WLPHV // This code performs poorly on a linked list. public static void removeEvenLength(LinkedList<String> list) { for (int i = 0; i < list.size(); i++) { String element = list.get(i); // slow! if (element.length() % 2 == 0) { list.remove(i); // slow! i--; } } } +RZHYHU WKHUH V DQ HIILFLHQW ZD\ WR H[DPLQH HYHU\ HOHPHQW RI D OLQNHG OLVW LI ZH ZDQW VHTXHQWLDO DFFHVV WKDW LV WR H[DPLQH HDFK HOHPHQW LQ RUGHU IURP WKH IURQW WR WKH EDFN 7R GR WKLV ZH FDQ XVH D VSHFLDO REMHFW QDPHG DQ LWHUDWRU WKDW NHHSV WUDFN RI RXU FXUUHQW SRVLWLRQ LQ WKH OLVW VR WKDW ZKHQ ZH JR IURP RQH HOHPHQW WR WKH QH[W ZH GRQ W KDYH WR ZDON WKH SRLQWHUV DOO WKH ZD\ IURP WKH IURQW RI WKH OLVW ,WHUDWRU $Q REMHFW WKDW UHSUHVHQWV D SRVLWLRQ ZLWKLQ D OLVW DQG DOORZV \RX WR UHWULHYH WKH HOHPHQWV RI D OLVW LQ VHTXHQWLDO RUGHU $V ZH OO VHH ODWHU LQ WKLV FKDSWHU LWHUDWRUV DUH YHU\ XVHIXO EHFDXVH HYHU\ FROOHFWLRQ LQ WKH -DYD &ROOHFWLRQV )UDPHZRUN KDV DQ LWHUDWRU \RX FDQ XVH 7KLV PHDQV WKDW \RX OO KDYH D IDPLOLDU LQWHUIDFH IRU H[DPLQLQJ WKH HOHPHQWV RI DQ\ FROOHFWLRQ $Q LWHUDWRU REMHFW KDV WKH IROORZLQJ PHWKRGV 8VHIXO 0HWKRGV RI ,WHUDWRU 2EMHFWV 0HWKRG hasNext() next() 'HVFULSWLRQ UHWXUQV WUXH LI WKHUH DUH PRUH HOHPHQWV WR EH H[DPLQHG UHWXUQV WKH QH[W HOHPHQW IURP WKH OLVW DQG DGYDQFHV WKH SRVLWLRQ RI WKH LWHUDWRU E\ RQH remove() UHPRYHV WKH HOHPHQW PRVW UHFHQWO\ UHWXUQHG E\ QH[W 7R JHW DQ LWHUDWRU IURP PRVW FROOHFWLRQV VXFK DV $UUD\/LVWV RU /LQNHG/LVWV \RX FDQ FDOO WKH PHWKRG QDPHG LWHUDWRU RQ WKH OLVW ZKLFK UHWXUQV DQ ,WHUDWRU REMHFW IRU WKDW OLVW V HOHPHQWV <RX GRQ W XVH WKH QHZ NH\ZRUG *HQHUDOO\ WKH SDWWHUQ RI XVLQJ DQ LWHUDWRU ORRNV OLNH WKH IROORZLQJ Iterator<<type>> itr = <collection>.iterator(); while (itr.hasNext()) { do something with itr.next(); } 579 7KH H[DPSOH RI UHPRYLQJ VWULQJV RI HYHQ OHQJWK FDQ EH LPSOHPHQWHG PXFK PRUH HIILFLHQWO\ XVLQJ DQ LWHUDWRU :KLOH WKH RULJLQDO $UUD\/LVW YHUVLRQ WRRN VHYHUDO PLQXWHV WR SURFHVV D OLVW RI RQH PLOOLRQ HOHPHQWV WKLV QHZ FRGH ILQLVKHV D PLOOLRQ HOHPHQW OLVW LQ XQGHU RQH WHQWK RI D VHFRQG ,W SHUIRUPV WKLV TXLFNO\ EHFDXVH WKH LWHUDWRU UHWDLQV WKH FXUUHQW SRVLWLRQ LQ WKH OLVW EHWZHHQ JHWWLQJ RU UHPRYLQJ HDFK HOHPHQW // Removes all strings of even length from // the given linked list. public static void removeEvenLength(LinkedList<String> list) { Iterator<String> i = list.iterator(); while (i.hasNext()) { String element = i.next(); if (element.length() % 2 == 0) { i.remove(); } } } &RPPRQ 3URJUDPPLQJ (UURU &DOOLQJ QH[W RQ LWHUDWRU WRR PDQ\ WLPHV ,WHUDWRUV FDQ EH D ELW FRQIXVLQJ WR QHZ SURJUDPPHUV VR \RX KDYH WR EH FDUHIXO WR XVH WKHP FRUUHFWO\ 7KH IROORZLQJ FRGH DWWHPSWV WR XVH DQ LWHUDWRU WR ILQG DQG UHWXUQ WKH ORQJHVW 6WULQJ LQ D OLQNHG OLVW EXW LW KDV D EXJ // Returns the longest string in the list. (does not work!) public static String longest(LinkedList<String> list) { Iterator<String> itr = list.iterator(); String longest = list.next(); // initialize to first element while (itr.hasNext()) { if (itr.next().length() > longest.length()) { longest = itr.next(); } } return longest; } 7KH SUREOHP ZLWK WKH SUHYLRXV FRGH LV WKDW WKH QH[W PHWKRG LV FDOOHG RQ WKH LWHUDWRU LQ WZR SODFHV RQFH ZKHQ WHVWLQJ LWV OHQJWK DQG DJDLQ ZKHQ WU\LQJ WR VWRUH WKH VWULQJ DV WKH ORQJHVW (DFK WLPH \RX FDOO QH[W WKH LWHUDWRU DGYDQFHV E\ RQH SRVLWLRQ VR LI LW V FDOOHG WZLFH LQ WKH ORRS \RX OO VNLS DQ HOHPHQW ZKHQ \RX ILQG D PDWFK )RU H[DPSOH LI WKH OLVW FRQWDLQV > RK KHOOR KRZ DUH \RX @ ZH PLJKW VHH WKH KHOOR DQG LQWHQG WR VWRUH LW EXW WKH VHFRQG FDOO WR QH[W ZRXOG FDXVH XV WR DFWXDOO\ VWRUH WKH IROORZLQJ HOHPHQW KRZ 7KH VROXWLRQ LV WR VDYH WKH UHVXOW RI WKH LWU QH[W UHSODFH WKH SUHYLRXV ZKLOH ORRS FDOO LQWR D YDULDEOH 7KH IROORZLQJ FRGH ZRXOG // This version of the code is now correct. while (itr.hasNext()) { String current = itr.next(); if (current.length() > longest.length()) { longest = current; } } 580 ,WHUDWRUV DUH DOVR XVHG LQWHUQDOO\ E\ -DYD V HQKDQFHG IRU ORRS ,W WXUQV RXW WKDW ZKHQ \RX XVH D IRU HDFK ORRS OLNH WKH IROORZLQJ -DYD LV DFWXDOO\ JUDEELQJ WKH HOHPHQWV XVLQJ DQ LWHUDWRU XQGHU WKH KRRG for (String word : list) { System.out.println(word + " " + word.length()); } $V WKH FRPSLOHU SURFHVVHV WKH IRU HDFK ORRS LW HVVHQWLDOO\ FRQYHUWV LW LQWR WKH IROORZLQJ FRGH Iterator<String> i = list.iterator(); while (i.hasNext()) { String word = i.next(); System.out.println(word + " " + word.length()); } 7KHUH V D PRUH DGYDQFHG YHUVLRQ RI ,WHUDWRU QDPHG /LVW,WHUDWRU WKDW ZRUNV RQO\ RQ OLVWV /LVW,WHUDWRU SURYLGHV RSHUDWLRQV OLNH DGGLQJ HOHPHQWV VHWWLQJ HOHPHQW YDOXHV DQG UHYHUVH LWHUDWLRQ IURP EDFN WR IURQW :H ZRQ W GLVFXVV /LVW,WHUDWRU LQ GHWDLO KHUH ,Q VXPPDU\ WKH IROORZLQJ DUH VRPH RI WKH PDMRU EHQHILWV RI $UUD\/LVW DQG /LQNHG/LVW $UUD\/LVW VWUHQJWKV UDQGRP DFFHVV DQ\ HOHPHQW FDQ EH DFFHVVHG TXLFNO\ DGGLQJ DQG UHPRYLQJ DW WKH HQG RI WKH OLVW LV IDVW /LQNHG/LVW VWUHQJWKV DGGLQJ DQG UHPRYLQJ DW HLWKHU HQG RI WKH OLVW LV IDVW IDVW DGG UHPRYH GXULQJ D VHTXHQWLDO DFFHVV ZLWK DQ LWHUDWRU QR QHHG WR H[SDQG DQ DUUD\ ZKHQ IXOO FDQ EH PRUH HDVLO\ XVHG DV D TXHXH /LQNHG/LVW ([DPSOH 6LHYH &RQVLGHU WKH WDVN RI ILQGLQJ DOO SULPH QXPEHUV XS WR D JLYHQ PD[LPXP 5HFDOO WKDW SULPH QXPEHUV DUH LQWHJHUV ZLWK QR IDFWRUV RWKHU WKDQ DQG WKHPVHOYHV LV GHILQHG DV WKH VPDOOHVW SULPH QXPEHU 7R EXLOG D OLVW RI SULPH QXPEHUV ZH FRXOG FHUWDLQO\ MXVW ZULWH D EUXWH IRUFH VROXWLRQ XVLQJ IRU ORRSV for (each number from 2 to maximum) { if (number is prime) { add to list of prime numbers. } } 7R ILJXUH RXW ZKHWKHU HDFK QXPEHU LV SULPH ZH G KDYH WR ZULWH DQRWKHU IRU ORRS WKDW WHVWHG DOO ORZHU LQWHJHUV WR VHH ZKHWKHU WKH\ ZHUH IDFWRUV RI WKDW QXPEHU 581 7KHUH V D PRUH FOHYHU DOJRULWKP IRU ILQGLQJ SULPH QXPEHUV FDOOHG WKH 6LHYH RI (UDWRVWKHQHV 7KH VLHYH DOJRULWKP VWDUWV E\ FUHDWLQJ WZR OLVWV RI QXPEHUV RQH OLVW RI QXPEHUV WR SURFHVV DQG DQRWKHU RI QXPEHUV NQRZQ WR EH SULPH ,QLWLDOO\ WKH QXPEHUV WR SURFHVV FDQ FRQWDLQ HYHU\ QXPEHU IURP WR WKH PD[LPXP ZKLOH WKH OLVW RI SULPHV ZLOO EH HPSW\ +HUH LV WKH LQLWLDO VWDWH RI WKH WZR OLVWV IRU D PD[LPXP RI numbers: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25] primes: 7KH VLHYH ZRUNV E\ UHSHDWHGO\ UHPRYLQJ WKH ILUVW HOHPHQW IURP WKH QXPEHUV OLVW DQG DVVXPLQJ LW WR EH SULPH DQG WKHQ ILOWHULQJ RXW DOO RWKHU HOHPHQWV IURP WKH QXPEHUV OLVW WKDW DUH PXOWLSOHV RI WKLV ILUVW HOHPHQW +HUH DUH WKH VWDWHV RI WKH OLVWV DIWHU WKH ILUVW WKUHH SDVVHV RI WKH DOJRULWKP numbers: [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25] primes: [2] numbers: [5, 7, 11, 13, 17, 19, 23, 25] primes: [2, 3] numbers: [7, 11, 13, 17, 19, 23] primes: [2, 3, 5] 1RZ OHW V LPSOHPHQW WKH VLHYH DOJRULWKP :H OO XVH OLQNHG OLVWV WR UHSUHVHQW WKH OLVWV RI QXPEHUV DQG SULPHV 7KLV LV SUHIHUDEOH RYHU $UUD\/LVWV EHFDXVH ZH UH GRLQJ D ORW RI UHPRYDO IURP WKH IURQW RI WKH OLVW 7KLV LV PRUH HIILFLHQW RQ D /LQNHG/LVW WKDQ RQ DQ $UUD\/LVW EHFDXVH WKH $UUD\/LVW KDV WR VKLIW HOHPHQWV OHIW RQ UHPRYDO )LUVW ZH OO FUHDWH DQ HPSW\ OLVW RI SULPHV DQG D OLVW RI DOO QXPEHUV XS WR WKH JLYHQ PD[LPXP LinkedList<Integer> primes = new LinkedList<Integer>(); LinkedList<Integer> numbers = new LinkedList<Integer>(); for (int i = 2; i <= max; i++) { numbers.add(i); } 1H[W ZH OO SURFHVV WKH OLVW RI QXPEHUV DV SUHYLRXVO\ GHVFULEHG :H OO XVH DQ LWHUDWRU WR PDNH SDVVHV RYHU WKH QXPEHUV OLVW DQG UHPRYH HOHPHQWV WKDW DUH PXOWLSOHV RI WKH IURQW HOHPHQW while (!numbers.isEmpty()) { // remove a prime number from the front of the list int front = numbers.remove(0); primes.add(front); // remove all multiples of this prime number Iterator<Integer> itr = numbers.iterator(); while (itr.hasNext()) { int current = itr.next(); if (current % front == 0) { itr.remove(); } } } 582 +HUH V WKH FRPSOHWH SURJUDP 7KH PDLQ DGGLWLRQ LV D PDLQ PHWKRG WKDW SURPSWV WKH XVHU IRU WKH PD[LPXP QXPEHU WR H[DPLQH 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 // Uses a linked list to implement the Sieve of // Eratosthenes algorithm for finding prime numbers. import java.util.*; public class Sieve { public static void main(String args) { System.out.println("This program will tell you all prime"); System.out.println("numbers up to a given maximum."); System.out.println(); Scanner console = new Scanner(System.in); System.out.print("Maximum number? "); int max = console.nextInt(); LinkedList<Integer> primes = sieve(max); System.out.println("Prime numbers up to " + max + ":"); System.out.println(primes); } // Returns a list of all prime numbers up to the given maximum // using the Sieve of Eratosthenes algorithm. public static LinkedList<Integer> sieve(int max) { LinkedList<Integer> primes = new LinkedList<Integer>(); // add all numbers from 2 to max to a list LinkedList<Integer> numbers = new LinkedList<Integer>(); for (int i = 2; i <= max; i++) { numbers.add(i); } while (!numbers.isEmpty()) { // remove a prime number from the front of the list int front = numbers.remove(0); primes.add(front); // remove all multiples of this prime number Iterator<Integer> itr = numbers.iterator(); while (itr.hasNext()) { int current = itr.next(); if (current % front == 0) { itr.remove(); } } } return primes; } } +HUH V WKH RXWSXW IURP DQ H[DPSOH SURJUDP WKDW SURPSWV WKH XVHU IRU D PD[LPXP QXPEHU DQG WKHQ FDOOV WKH VLHYH PHWKRG DSSURSULDWHO\ 583 This program will tell you all prime numbers up to a given maximum. Maximum number? 50 Prime numbers up to 50: [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47] 7KH DOJRULWKP FDQ EH RSWLPL]HG LQ D IHZ ZD\V )RU RQH WKH LQLWLDO OLVW RI QXPEHUV GRHVQ W DFWXDOO\ QHHG WR VWRUH HYHU\ LQWHJHU IURP WKURXJK WKH PD[LPXP ,W FDQ LQVWHDG VWRUH DQG HDFK RGG LQWHJHU XS WR WKH PD[LPXP 7KLV PDNHV WKH DOJRULWKP PRUH HIILFLHQW EHFDXVH LW UHTXLUHV IHZHU QXPEHUV WR EH SURFHVVHG $QRWKHU RSWLPL]DWLRQ WR WKH VLHYH DOJRULWKP LV WR VWRS RQFH WKH IURQW HOHPHQW RI WKH QXPEHUV OLVW LV JUHDWHU WKDQ WKH VTXDUH URRW RI WKH PD[LPXP EHFDXVH DQ\ QXPEHU WKLV ODUJH WKDW UHPDLQV LQ WKH OLVW FDQQRW KDYH DQ\ PXOWLSOHV UHPDLQLQJ LQ WKH OLVW )RU H[DPSOH LQ WKH SUHFHGLQJ VDPSOH OLVWV IRU D PD[LPXP RI RQFH WKH IURQW RI WKH QXPEHUV OLVW H[FHHGV DOO UHPDLQLQJ QXPEHUV LQ WKH OLVW DUH NQRZQ WR EH SULPH DQG FDQ EH SODFHG LQWR WKH SULPHV OLVW 7KHVH RSWLPL]DWLRQV DUH OHIW DV H[HUFLVHV $EVWUDFW 'DWD 7\SHV $'7V ,W V QR DFFLGHQW WKDW WKH /LQNHG/LVW FROOHFWLRQ SURYLGHV WKH VDPH PHWKRGV DV WKH $UUD\/LVW 7KH\ UH ERWK FRQVLGHUHG WR EH LPSOHPHQWDWLRQV RI WKH VDPH RYHUDOO W\SH RI GDWD VWUXFWXUH D OLVW ,Q IDFW WKHUH V DQ LQWHUIDFH LQ WKH MDYD XWLO SDFNDJH QDPHG /LVW WKDW LV LPSOHPHQWHG E\ ERWK /LQNHG/LVW DQG $UUD\/LVW 7KH /LVW LQWHUIDFH GHFODUHV DOO WKH FRPPRQ PHWKRGV WKDW ERWK OLVWV LPSOHPHQW :KLOH WKH WZR OLVW W\SHV KDYH GLIIHUHQW EHQHILWV WKH ZD\ WKDW WKH\ UH XVHG LV WKH VDPH 7KLV LGHD RI JDWKHULQJ GDWD VWUXFWXUHV XQGHU D FRPPRQ LQWHUIDFH LV UHODWHG WR D FODVVLF GDWD VWUXFWXUH FRQFHSW FDOOHG DQ DEVWUDFW GDWD W\SH RU $'7 )RUPDOO\ DQ $'7 LV D GHVFULSWLRQ RI D GDWD VWUXFWXUH WKDW VSHFLILHV WKH W\SH RI GDWD WR EH VWRUHG LQ WKH VWUXFWXUH DQG WKH RSHUDWLRQV WKDW FDQ EH SHUIRUPHG RQ WKH FROOHFWLRQ $Q $'7 GRHVQ W VSHFLI\ H[DFWO\ KRZ WKH GDWD VWUXFWXUH LV LPSOHPHQWHG RQO\ ZKDW GDWD LW FDQ VWRUH DQG ZKDW LW FDQ GR $EVWUDFW 'DWD 7\SH $'7 $ VSHFLILFDWLRQ RI D GDWD VWUXFWXUH EDVHG RQ LWV W\SH RI GDWD DQG WKH RSHUDWLRQV LW FDQ SHUIRUP ,Q -DYD DQ $'7 V VHW RI RSHUDWLRQV LV VSHFLILHG E\ WKH PHWKRGV RI WKH LQWHUIDFH DQG WKH W\SH RI GDWD LV VSHFLILHG E\ WKH W\SH SDUDPHWHU EHWZHHQ DQG ! ,W V FRQVLGHUHG D JRRG SUDFWLFH WR GHFODUH DQ\ YDULDEOHV DQG SDUDPHWHUV RI FROOHFWLRQ W\SHV XVLQJ WKH DSSURSULDWH $'7 LQWHUIDFH W\SH UDWKHU WKDQ WKH DFWXDO FODVV W\SH <RX FDQ W FUHDWH DQ REMHFW RI W\SH /LVW EXW \RX FDQ XVH /LVW DV WKH GHFODUHG W\SH RI D YDULDEOH )RU H[DPSOH List<Integer> list = new LinkedList<Integer>(); 584 <RX FDQ DOVR XVH WKH $'7 LQWHUIDFH W\SHV OLNH /LVW ZKHQ GHFODULQJ SDUDPHWHUV UHWXUQ W\SHV RU GDWD ILHOGV ,W V XVHIXO ZKHQ ZULWLQJ D PHWKRG WKDW DFFHSWV D FROOHFWLRQ DV D SDUDPHWHU EHFDXVH LW PHDQV WKDW PHWKRG ZLOO EH DEOH WR RSHUDWH VXFFHVVIXOO\ RQ DQ\ FROOHFWLRQ WKDW LPSOHPHQWV WKDW $'7 LQWHUIDFH )RU H[DPSOH WKH IROORZLQJ PHWKRG FDQ DFFHSW D /LQNHG/LVW RU $UUD\/LVW DV LWV DFWXDO SDUDPHWHU // Returns the longest string in the given list. public static String longest(List<String> list) { 6HYHUDO XVHIXO VWDWLF PHWKRGV WKDW RSHUDWH RQ /LVWV DUH LQ WKH &ROOHFWLRQV FODVV 7KHVH PHWKRGV KHDGHUV VSHFLI\ SDUDPHWHUV RI W\SH /LVW UDWKHU WKDQ /LQNHG/LVW RU $UUD\/LVW 7KHVH PHWKRGV SHUIRUP FRPPRQ WDVNV RQ OLVWV VXFK DV VRUWLQJ VKXIIOLQJ DQG VHDUFKLQJ +HUH V D TXLFN OLVW RI XVHIXO PHWKRGV IURP WKH &ROOHFWLRQV FODVV WKDW RSHUDWH RQ OLVWV 8VHIXO 6WDWLF 0HWKRGV RI WKH &ROOHFWLRQV &ODVV 0HWKRG binarySearch(list, value) 'HVFULSWLRQ 6HDUFKHV D VRUWHG DUUD\ IRU D JLYHQ HOHPHQW YDOXH DQG UHWXUQV LWV LQGH[ copy(destinationList, sourceList) &RSLHV DOO HOHPHQWV IURP WKH VRXUFH OLVW WR WKH GHVWLQDWLRQ OLVW fill(list, value) 5HSODFHV HYHU\ HOHPHQW LQ WKH JLYHQ OLVW ZLWK WKH JLYHQ YDOXH max(list) 5HWXUQV WKH HOHPHQW ZLWK WKH KLJKHVW YDOXH min(list) 5HWXUQV WKH HOHPHQW ZLWK WKH ORZHVW YDOXH replaceAll(list, oldValue, newValue) 5HSODFHV DOO RFFXUUHQFHV RI WKH ROG YDOXH ZLWK WKH QHZ YDOXH reverse(list) 3ODFHV WKH HOHPHQWV LQWR WKH RSSRVLWH RUGHU rotate(list, distance) 6OLGHV HDFK HOHPHQW WR WKH ULJKW E\ WKH JLYHQ QXPEHU RI LQGH[HV ZUDSSLQJ DURXQG WKH ODVW HOHPHQWV WR WKH IURQW shuffle(list) 3ODFHV WKH HOHPHQWV LQWR UDQGRP RUGHU sort(list) 3ODFHV WKH HOHPHQWV LQWR VRUWHG QRQGHFUHDVLQJ RUGHU swap(list, index1, index2) 6ZLWFKHV WKH HOHPHQW YDOXHV DW WKH JLYHQ WZR LQGH[HV 1RWLFH WKDW WKHVH PHWKRGV DUH VWDWLF VR WKH\ PXVW EH FDOOHG E\ ZULWLQJ WKH ZRUG &ROOHFWLRQV IROORZHG E\ D GRW DQG WKH PHWKRG V QDPH )RU H[DPSOH LI ZH KDYH D /LQNHG/LVW YDULDEOH QDPHG OLVW DQG ZH ZLVK WR UHYHUVH WKH OLVW V FRQWHQWV ZH G ZULWH Collections.reverse(list); 7KHUH DUH VHYHUDO RWKHU $'7 LQWHUIDFHV LQ WKH &ROOHFWLRQV )UDPHZRUN EHVLGHV /LVW VXFK DV 4XHXH 6HW DQG 0DS :H OO YLVLW PRVW RI WKHP LQ WKH UHVW RI WKLV FKDSWHU 585 6HWV $ PDMRU OLPLWDWLRQ RI ERWK W\SHV RI OLVWV LV WKDW WKH\ UH VORZ WR VHDUFK *HQHUDOO\ LI ZH ZDQW WR VHDUFK D OLVW ZH KDYH WR VHTXHQWLDOO\ ORRN DW HDFK HOHPHQW WR VHH LI ZH YH IRXQG WKH WDUJHW 7KLV FDQ WDNH D ORQJ WLPH IRU D ODUJH OLVW $QRWKHU OLPLWDWLRQ RI OLVWV LV WKDW LW V QRW HDV\ WR SUHYHQW D OLVW IURP VWRULQJ GXSOLFDWH YDOXHV ,Q PDQ\ FDVHV WKLV LVQ W D SUREOHP EXW LI IRU H[DPSOH \RX DUH VWRULQJ D FROOHFWLRQ WR FRXQW WKH QXPEHU RI XQLTXH ZRUGV LQ D ERRN \RX GRQ W ZDQW DQ\ GXSOLFDWHV WR H[LVW 7R SUHYHQW GXSOLFDWHV LQ D OLVW ZH G KDYH WR DJDLQ VHTXHQWLDOO\ VHDUFK WKH OLVW RQ HYHU\ DGG RSHUDWLRQ WR PDNH VXUH ZH ZHUHQ W DGGLQJ D ZRUG WKDW ZDV DOUHDG\ SUHVHQW 7KHUH V DQRWKHU DEVWUDFW GDWD W\SH FDOOHG D VHW WKDW LV YHU\ XVHIXO IRU PDLQWDLQLQJ D FROOHFWLRQ RI HOHPHQWV WKDW SUHYHQWV GXSOLFDWHV DQG FDQ EH VHDUFKHG TXLFNO\ 6HW $ FROOHFWLRQ WKDW VWRUHV D JURXS RI HOHPHQWV ZLWKRXW DOORZLQJ GXSOLFDWHV 7KLV LV YHU\ PXFK OLNH WKH PDWKHPDWLFDO QRWLRQ RI D VHW 6HWV ORVH VRPH DELOLWLHV ZH KDG ZLWK OLVWV QDPHO\ DQ\ RSHUDWLRQ WKDW UHTXLUHV DQ LQGH[ %XW ZH JDLQ QHZ IHDWXUHV LQ WUDGH VXFK DV HOLPLQDWLRQ RI GXSOLFDWHV DQG IDVW VHDUFKLQJ 6HW &RQFHSWV 7KH &ROOHFWLRQV )UDPHZRUN KDV DQ LQWHUIDFH QDPHG 6HW ZKRVH WZR SULPDU\ LPSOHPHQWDWLRQV DUH QDPHG +DVK6HW DQG 7UHH6HW +DVK6HW LV WKH JHQHUDO SXUSRVH VHW FODVV ZKLOH 7UHH6HW RIIHUV D IHZ DGYDQWDJHV VHHQ ODWHU ,I ZH G OLNH WR VWRUH D VHW RI 6WULQJ YDOXHV ZH FRXOG ZULWH WKH IROORZLQJ FRGH 7KH VHW ZLOO RQO\ FRQWDLQ IRXU HOHPHQWV EHFDXVH 0RH ZRXOG RQO\ EH SODFHG LQWR WKH VHW RQFH Set<String> stooges = new HashSet<String>(); stooges.add("Larry"); stooges.add("Moe"); stooges.add("Curly"); stooges.add("Moe"); // duplicate, won't be added stooges.add("Shemp"); stooges.add("Moe"); // duplicate, won't be added 6HWV SURYLGH H[DFWO\ WKH RSHUDWLRQV IURP WKH &ROOHFWLRQ LQWHUIDFH VKRZQ HDUOLHU LQ WKLV FKDSWHU VXFK DV DGG FRQWDLQV DQG UHPRYH ,W V JHQHUDOO\ DVVXPHG WKDW WKHVH RSHUDWLRQV DUH HIILFLHQW VR WKDW \RX FDQ DGG PDQ\ HOHPHQWV WR D VHW DQG VHDUFK LW PDQ\ WLPHV ZLWKRXW LQFXUULQJ SRRU SHUIRUPDQFH $ 6HW DOVR SURYLGHV D WR6WULQJ PHWKRG WKDW OHWV \RX VHH LWV HOHPHQWV 3ULQWLQJ WKH SUHFHGLQJ VWRRJHV VHW ZRXOG SURGXFH WKH IROORZLQJ RXWSXW [Moe, Shemp, Larry, Curly] 586 $ +DVK6HW LV LPSOHPHQWHG XVLQJ D VSHFLDO LQWHUQDO DUUD\ QDPHG D KDVK WDEOH WKDW SODFHV HOHPHQWV LQWR VSHFLILF SRVLWLRQV EDVHG XSRQ LQWHJHUV FDOOHG KDVK FRGHV (YHU\ -DYD REMHFW KDV D KDVK FRGH WKDW FDQ EH DFFHVVHG WKURXJK LWV KDVK&RGH PHWKRG <RX GRQ W QHHG WR XQGHUVWDQG WKH GHWDLOV RI +DVK6HW V LPSOHPHQWDWLRQ WR XVH LW EXW WKH ERWWRP OLQH LV WKDW LW V LPSOHPHQWHG LQ VXFK D ZD\ WKDW \RX FDQ DGG UHPRYH DQG VHDUFK IRU HOHPHQWV YHU\ TXLFNO\ 2QH GUDZEDFN RI WKH +DVK6HW LV WKDW LW VWRUHV LWV HOHPHQWV LQ DQ XQSUHGLFWDEOH RUGHU 7KH SUHFHGLQJ VWRRJHV VHW V HOHPHQWV ZHUHQ W DOSKDEHWL]HG QRU GLG WKH\ PDWFK WKH RUGHU LQ ZKLFK WKH\ ZHUH LQVHUWHG 7KLV VWUDQJH EHKDYLRU LV D WUDGHRII IRU WKH +DVK6HW V IDVW SHUIRUPDQFH 6HWV DUH YHU\ XVHIXO IRU H[DPLQLQJ ORWV RI GDWD ZKLOH LJQRULQJ GXSOLFDWHV )RU H[DPSOH LI ZH ZDQWHG WR VHH KRZ PDQ\ XQLTXH ZRUGV DSSHDU LQ WKH ERRN 0RE\ 'LFN ZH FRXOG ZULWH FRGH VXFK DV WKH IROORZLQJ Set<String> words = new HashSet<String>(); Scanner in = new Scanner(new File("mobydick.txt")); while (in.hasNext()) { String word = in.next(); word = word.toLowerCase(); words.add(word); } System.out.println("Number of unique words = " + words.size()); 7KH FRGH SURGXFHV WKH IROORZLQJ RXWSXW ZKHQ UXQ RQ RXU FRS\ RI WKH WH[W RI 0RE\ 'LFN Number of unique words = 19474 6HWV KDYH D FRQYHQLHQW FRQVWUXFWRU WKDW DFFHSWV DQRWKHU FROOHFWLRQ DV D SDUDPHWHU DQG SXWV DOO XQLTXH HOHPHQWV IURP WKDW FROOHFWLRQ LQWR WKH 6HW 2QH FOHYHU XVDJH RI WKLV FRQVWUXFWRU LV WR ILQG RXW ZKHWKHU D /LVW FRQWDLQV DQ\ GXSOLFDWHV 7R GR VR VLPSO\ FRQVWUXFW D 6HW IURP LW DQG VHH LI WKH VL]HV GLIIHU 7KH IROORZLQJ FRGH GHPRQVWUDWHV WKLV // Returns true if the given list contains any duplicate elements. public static boolean hasDuplicates(List<Integer> list) { Set<Integer> set = new HashSet<Integer>(list); return set.size() < list.size(); } 2QH GUDZEDFN RI D 6HW LV WKDW LW GRHVQ W VWRUH HOHPHQWV E\ LQGH[HV 6R WKH IROORZLQJ VRUW RI ORRS GRHVQ W FRPSLOH RQ D 6HW EHFDXVH LW GRHVQ W KDYH D JHW PHWKRG WKDW DFFHSWV DQ LQGH[ DV D SDUDPHWHU // This code doesn't compile. for (int i = 0; i < set.size(); i++) { String word = set.get(i); // error -- no get method System.out.println(word); } ,QVWHDG LI \RX ZDQW WR ORRS RYHU WKH HOHPHQWV RI D 6HW \RX PXVW GR VR XVLQJ DQ ,WHUDWRU /LNH DOO FROOHFWLRQV VHWV KDYH WKH LWHUDWRU PHWKRG WR JUDE DQ LWHUDWRU IRU WKHLU HOHPHQWV <RX FDQ WKHQ XVH WKH IDPLOLDU KDV1H[W QH[W ORRS WR H[DPLQH HDFK HOHPHQW 587 // This code does work correctly. Iterator<String> itr = set.iterator(); while (itr.hasNext()) { String word = itr.next(); System.out.println(word); } $ VKRUWHU DOWHUQDWLYH WR WKH SUHFHGLQJ FRGH LV WR XVH D IRU HDFK ORRS RYHU WKH HOHPHQWV RI WKH VHW $V PHQWLRQHG SUHYLRXVO\ WKH FRGH EHKDYHV WKH VDPH ZD\ EXW LV HDVLHU WR ZULWH DQG UHDG for (String word : set) { System.out.println(word); } 7UHH6HW YHUVXV +DVK6HW 7KHUH V DQRWKHU FODVV WKDW LPSOHPHQWV WKH 6HW LQWHUIDFH QDPHG 7UHH6HW 7UHH6HWV VWRUH WKHLU HOHPHQWV XVLQJ DQ LQWHUQDO OLQNHG GDWD VWUXFWXUH FDOOHG D ELQDU\ WUHH $ 7UHH6HW LV D ELW VORZHU WKDQ D +DVK6HW EXW LW VWRUHV LWV HOHPHQWV LQ VRUWHG RUGHU $ 7UHH6HW LV VWLOO TXLWH IDVW DQG LV SHUIHFWO\ HIILFLHQW HQRXJK IRU KHDY\ XVH 7UHH6HWV FDQ EH XVHIXO LI \RX ZDQW WR SULQW WKH VHW DQG KDYH WKH RXWSXW RUGHUHG )RU H[DPSOH WKH IROORZLQJ FRGH GLVSOD\V WKH VRUWHG VHW RI DOO WKUHH OHWWHU ZRUGV LQ 0RE\ 'LFN WKDW VWDUW ZLWK D Set<String> words = new TreeSet<String>(); Scanner in = new Scanner(new File("mobydick.txt")); while (in.hasNext()) { String word = in.next(); word = word.toLowerCase(); if (word.startsWith("a") && word.length() == 3) { words.add(word); } } System.out.println("Three-letter 'a' words = " + words); 7KH FRGH SURGXFHV WKH IROORZLQJ RXWSXW Three-letter 'a' words = [act, add, ado, aft, age, ago, ahi, aid, aim, air, alb, ale, all, and, ant, any, ape, apt, arc, are, ark, arm, art, asa, ash, ask, ass, ate, awe, axe, aye] $ 7UHH6HW FDQ RQO\ EH XVHG LI LW NQRZV KRZ WR VRUW LWV HOHPHQWV LQWR RUGHU 7KLV PHDQV LW ZLOO ZRUN LI LWV HOHPHQWV DUH RI DQ\ W\SH WKDW LPSOHPHQWV WKH &RPSDUDEOH LQWHUIDFH VXFK DV ,QWHJHU RU 6WULQJ <RX FDQ DOVR XVH 7UHH6HWV E\ SURYLGLQJ \RXU RZQ REMHFW WKDW VSHFLILHV KRZ WR FRPSDUH HOHPHQWV FDOOHG D &RPSDUDWRU &RPSDUDWRUV DUH GLVFXVVHG LQ &KDSWHU ZKHQ ZH FRYHU VHDUFKLQJ DQG VRUWLQJ )RU H[DPSOH \RX VKRXOG QRW WU\ WR FRQVWUXFW D 7UHH6HW RI 3RLQW HOHPHQWV DV LQ WKH IROORZLQJ FRGH // Bad code! It's illegal to use a TreeSet<Point> here, // because Point objects are not Comparable. Set<Point> set = new TreeSet<Point>(); set.add(new Point(5, 2)); set.add(new Point(2, 1)); 588 7KH SUHFHGLQJ FRGH FRPSLOHV XQIRUWXQDWHO\ EXW FUDVKHV ZKHQ \RX UXQ LW EHFDXVH LW GRHVQ W NQRZ KRZ WR RUGHU WKH 3RLQWV LQ WKH 7UHH6HW :H G EH EHWWHU RII XVLQJ D +DVK6HW LQ WKLV FDVH Exception in thread "main" java.lang.ClassCastException: java.awt.Point at java.util.TreeMap.compare(Unknown Source) at java.util.TreeMap.put(Unknown Source) at java.util.TreeSet.add(Unknown Source) ,Q VXPPDU\ WKH IROORZLQJ DUH VRPH RI WKH PDMRU EHQHILWV RI +DVK6HW DQG 7UHH6HW +DVK6HW VWUHQJWKV H[WUHPHO\ IDVW SHUIRUPDQFH IRU DGG FRQWDLQV UHPRYH FDQ EH XVHG ZLWK DQ\ W\SH RI REMHFWV DV LWV HOHPHQWV 7UHH6HW VWUHQJWKV HOHPHQWV DUH VWRUHG LQ VRUWHG RUGHU PXVW EH XVHG ZLWK HOHPHQWV WKDW FDQ EH FRPSDUHG VXFK DV ,QWHJHU 6WULQJ 6HW 2SHUDWLRQV &RQVLGHU WKH FDVH ZKHUH \RX KDYH WZR VHWV DQG QHHG WR ILJXUH RXW KRZ PDQ\ WRWDO XQLTXH HOHPHQWV RFFXU EHWZHHQ WKH WZR VHWV <RX FDQ W MXVW DGG WKH VHWV VL]HV VLQFH WKH\ PLJKW KDYH VRPH HOHPHQWV LQ FRPPRQ WKDW VKRXOGQ W EH FRXQWHG WZLFH LQ \RXU WRWDO <RX FRXOG FRXQW DOO HOHPHQWV IURP WKH ILUVW VHW DQG FRXQW RQO\ WKH XQLTXH HOHPHQWV RI WKH VHFRQG E\ FKHFNLQJ WR VHH ZKHWKHU HDFK HOHPHQW IURP WKH VHFRQG LV DOVR LQ WKH ILUVW // Returns the number of elements contained in both set1 and set2. public static int totalElements(Set<String> set1, Set<String> set2) { int count = set1.size(); for (String element : set2) { if (!set2.contains(element)) { count++; } } return count; } $ PRUH HOHJDQW ZD\ WR SHUIRUP WKLV FDOFXODWLRQ LV WR FRPSXWH D XQLRQ EHWZHHQ WKH WKH VHWV 7KH XQLRQ RI WZR VHWV $ DQG % LV WKH VHW RI DOO HOHPHQWV WKDW DUH LQ FRQWDLQHG LQ HLWKHU $ RU % RU ERWK 8QLRQ LV DQ H[DPSOH RI D VHW RSHUDWLRQ D VHW RSHUDWLRQ LV D FRPELQDWLRQ RI WZR VHWV WR SURGXFH D QHZ VHW RU UHVXOW 2WKHU VHW RSHUDWLRQV LQFOXGH LQWHUVHFWLRQ WKH VHW RI DOO HOHPHQWV WKDW DUH LQ ERWK $ DQG % DQG GLIIHUHQFH WKH VHW RI DOO HOHPHQWV WKDW DUH LQ $ EXW QRW LQ % <RX FDQ ZULWH FRGH WR SHUIRUP VHW RSHUDWLRQV E\ FDOOLQJ WKH YDULRXV $OO PHWKRGV ZLWK WKH UHOHYDQW SDLU RI VHWV 7KH IROORZLQJ WDEOH VXPPDUL]HV ZKLFK PHWKRG FRUUHVSRQGV WR ZKLFK VHW RSHUDWLRQ 589 6HW RSHUDWLRQ XQLRQ LQWHUVHFWLRQ GLIIHUHQFH VXSHUVHW 0HWKRG addAll retainAll removeAll containsAll 'HVFULSWLRQ VHW RI DOO HOHPHQWV WKDW DUH LQ $ RU % RU ERWK VHW RI DOO HOHPHQWV WKDW DUH LQ ERWK $ DQG % VHW RI DOO HOHPHQWV WKDW DUH LQ $ EXW QRW LQ % UHWXUQV WUXH LI $ LV D VXSHUVHW RI FRQWDLQV DOO HOHPHQWV RI % )RU H[DPSOH RQH FRXOG UHZULWH WKH WRWDO(OHPHQWV FRGH WR XVH D XQLRQ ZLWK WKH DGG$OO PHWKRG // Returns the number of elements contained in both set1 and set2. public static int totalElements(Set<String> set1, Set<String> set2) { Set<String> union = new HashSet<String>(set1); union.addAll(set2); return union.size(); } $Q LPSRUWDQW WKLQJ WR QRWH DERXW WKH VHW RSHUDWLRQV LQ -DYD LV WKDW WKH\ PRGLI\ WKH H[LVWLQJ VHW UDWKHU WKDQ FUHDWLQJ D QHZ VHW IRU \RX 1RWLFH WKDW LQ WKH SUHFHGLQJ WRWDO(OHPHQWV FRGH ZH LQLWLDOL]H D QHZ +DVK6HW WKDW FRQWDLQV DOO HOHPHQWV IURP VHW DQG WKHQ DGG VHW V FRQWHQWV WR RXU QHZ VHW UDWKHU WKDQ FRPELQLQJ VHW DQG VHW GLUHFWO\ :H GR WKLV EHFDXVH WKH FDOOHU PLJKW QRW ZDQW XV WR GLVWXUE WKHLU VHWV RULJLQDO FRQWHQWV 6HW RSHUDWLRQV DUH RIWHQ GHSLFWHG E\ GUDZLQJV FDOOHG 9HQQ GLDJUDPV ZKHUH VHWV DUH FLUFOHV DQG VKDGHG RYHUODSSLQJ EHWZHHQ FLUFOHV UHSUHVHQWV VHW RSHUDWLRQV +HUH V DQ H[DPSOH 6HW ([DPSOH /RWWHU\ &RQVLGHU WKH WDVN RI ZULWLQJ D ORWWHU\ SURJUDP 7KH SURJUDP VKRXOG UDQGRPO\ JHQHUDWH D ZLQQLQJ ORWWHU\ WLFNHW WKHQ SURPSW WKH SOD\HU WR HQWHU WKHLU ORWWR QXPEHUV 'HSHQGLQJ RQ KRZ PDQ\ QXPEHUV PDWFK WKH SOD\HU ZLQV YDULRXV FDVK SUL]HV 6HWV PDNH H[FHOOHQW FROOHFWLRQV IRU VWRULQJ WKH ZLQQLQJ ORWWR QXPEHUV DQG WKH SOD\HU V QXPEHUV 7KH\ SUHYHQW WKH SRVVLELOLW\ RI GXSOLFDWHV DQG WKH\ TXLFNO\ DOORZ XV WR WHVW ZKHWKHU D QXPEHU LQ RQH VHW LV LQ WKH RWKHU 7KLV ZLOO KHOS XV WR FRXQW WKH QXPEHU RI FRUUHFW ZLQQLQJ QXPEHUV WKH SOD\HU KDV HQWHUHG 590 7KH IROORZLQJ FRGH XVHV D 5DQGRP REMHFW WR LQLWLDOL]H D VHW RI ZLQQLQJ ORWWHU\ QXPEHUV EHWZHHQ DQG 7KH FRGH XVHV D ZKLOH ORRS EHFDXVH WKH VDPH QXPEHU PLJKW EH UDQGRPO\ JHQHUDWHG PRUH WKDQ RQFH Set<Integer> winningNumbers = new TreeSet<Integer>(); Random r = new Random(); while (winningNumbers.size() < 6) { int number = r.nextInt(40) + 1; winningNumbers.add(number); } 2QFH WKH ZLQQLQJ QXPEHU VHW LV JHQHUDWHG ZH OO UHDG WKH SOD\HU V ORWWR QXPEHUV LQWR D VHFRQG VHW 7R ILJXUH RXW KRZ PDQ\ QXPEHUV WKH SOD\HU KDV FKRVHQ FRUUHFWO\ ZH FRXOG VHDUFK WKH ZLQQLQJ QXPEHU VHW WR VHH ZKHWKHU LW FRQWDLQV HDFK QXPEHU IURP WKH WLFNHW %XW D PRUH HOHJDQW ZD\ WR SHUIRUP WKLV WHVW LV WR SHUIRUP DQ LQWHUVHFWLRQ EHWZHHQ WKH ZLQQLQJ QXPEHUV VHW DQG WKH SOD\HU V WLFNHW VHW 7KH IROORZLQJ FRGH FUHDWHV WKH LQWHUVHFWLRQ RI WKH SOD\HU V WLFNHW DQG WKH ZLQQLQJ QXPEHUV E\ FRS\LQJ WKH WLFNHW DQG WKHQ UHPRYLQJ DQ\ HOHPHQWV IURP LW WKDW DUHQ W ZLQQLQJ QXPEHUV // find the winning numbers from the user's ticket Set<Integer> intersection = new TreeSet<Integer>(ticket); intersection.retainAll(winningNumbers); 2QFH ZH KDYH WKH LQWHUVHFWLRQ ZH FDQ DVN IRU LWV VL]H WR VHH KRZ PDQ\ RI WKH SOD\HU V QXPEHUV ZHUH ZLQQLQJ QXPEHUV DQG ZH FDQ SLFN DQ DSSURSULDWH FDVK SUL]H DPRXQW IRU WKHP 2XU YHUVLRQ VWDUWV ZLWK D SUL]H DQG GRXEOHV IRU HDFK ZLQQLQJ QXPEHU :LWK DOO RI WKLV LQ PLQG KHUH LV WKH FRPSOHWH FRGH IRU WKH ORWWHU\ SURJUDP :H YH PDGH D IHZ VWDWLF PHWKRGV IRU VWUXFWXUH DQG PDGH D IHZ FRQVWDQWV IRU WKH QXPEHU RI QXPEHUV PD[LPXP QXPEHU DQG ORWWR SUL]H DPRXQWV 591 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 // Plays a quick lottery game with the user, // reading lucky numbers and printing how many // matched a winning lottery ticket. import java.util.*; public class Lottery { public static final int NUMBERS = 6; public static final int MAX_NUMBER = 40; public static void main(String args) { // get winning number and ticket sets Set<Integer> winningNumbers = createWinningNumbers(); Set<Integer> ticket = getTicket(); System.out.println(); // keep only the winning numbers from the user's ticket Set<Integer> intersection = new TreeSet<Integer>(ticket); intersection.retainAll(winningNumbers); // print results System.out.println("Your ticket numbers are " + ticket); System.out.println("The winning numbers are " + winningNumbers); System.out.println(); System.out.println("You had " + intersection.size() + " matching numbers."); if (intersection.size() > 0) { double prize = 100 * Math.pow(2, intersection.size()); System.out.println("The matched numbers are " + intersection); System.out.println("Your prize is $" + prize); } } // generates a set of the winning lotto numbers public static Set<Integer> createWinningNumbers() { Set<Integer> winningNumbers = new TreeSet<Integer>(); Random r = new Random(); while (winningNumbers.size() < NUMBERS) { int number = r.nextInt(MAX_NUMBER) + 1; winningNumbers.add(number); } return winningNumbers; } // reads the player's lottery ticket from the console public static Set<Integer> getTicket() { Set<Integer> ticket = new TreeSet<Integer>(); Scanner console = new Scanner(System.in); System.out.print("Type your " + NUMBERS + " unique lotto numbers: "); while (ticket.size() < NUMBERS) { int number = console.nextInt(); ticket.add(number); } return ticket; } } +HUH V RQH H[DPSOH RXWSXW IURP UXQQLQJ WKH SURJUDP 592 Type your 6 unique lotto numbers: 2 8 15 18 21 32 Your ticket numbers are [2, 8, 15, 18, 21, 32] The winning numbers are [1, 3, 15, 16, 18, 39] You had 2 matching numbers. The matched numbers are [15, 18] Your prize is $400.0 0DSV &RQVLGHU WKH WDVN RI ZULWLQJ D WHOHSKRQH ERRN SURJUDP ZKHUH XVHUV FDQ W\SH D SHUVRQ V QDPH DQG VHDUFK IRU WKDW SHUVRQ V SKRQH QXPEHU <RX FRXOG VWRUH WKH GDWD LQWR DQ DUUD\ OLVW RU VHW 3HUKDSV \RX G PDNH D VPDOO FODVV QDPHG 3KRQH%RRN5HFRUG WKDW VWRUHV D SHUVRQ V QDPH DQG SKRQH QXPEHU \RXU OLVW ZRXOG FRQWDLQ PDQ\ 3KRQH%RRN5HFRUG REMHFWV :KHQ VHDUFKLQJ IRU D SKRQH QXPEHU \RX G WUDYHUVH WKH OLVW DQG ORRN IRU WKH 3KRQH%RRN5HFRUG ZLWK WKDW QDPH DQG UHWXUQ LWV SKRQH QXPEHU $ VROXWLRQ VXFK DV WKH RQH MXVW GHVFULEHG LVQ W YHU\ SUDFWLFDO LQ SUDFWLFH ,Q D ODUJH OLVW RI UHFRUGV LW WDNHV WKH SURJUDP D ORQJ WLPH WR ORRN DW HDFK RQH WR ILQG WKH ULJKW UHFRUG WR UHWULHYH WKH SKRQH QXPEHU 7KHUH DUH PDQ\ GDWD SURFHVVLQJ WDVNV OLNH WKLV LQ ZKLFK LW V XVHIXO WR OLQN SDLUV RI REMHFWV OLNH D QDPH DQG D WHOHSKRQH QXPEHU WRJHWKHU :H ILQG RXUVHOYHV VD\LQJ , G OLNH WR DVVRFLDWH HYHU\ $ ZLWK D % +HUH DUH VRPH H[DPSOHV RI WDVNV OLNH WKLV 6WRUH D FROOHFWLRQ RI QDPHV DQG SKRQH QXPEHUV :KHQ WKH XVHU W\SHV D QDPH ORRN XS WKDW SHUVRQ V SKRQH QXPEHU &RXQW WKH QXPEHU RI RFFXUUHQFHV RI HYHU\ ZRUG LQ D ERRN *URXS WKH ZRUGV RI D ERRN E\ OHQJWK DQG UHSRUW KRZ PDQ\ ZRUGV DUH HDFK OHQJWK $VVRFLDWH HDFK FKDW XVHU ZLWK WKHLU VHW RI IULHQGV EXGGLHV 6WRUH D IDPLO\ WUHH ZKHUH HDFK SHUVRQ NQRZV DERXW KLV KHU PRWKHU DQG IDWKHU -DYD V FROOHFWLRQ IUDPHZRUN LQFOXGHV DQ DEVWUDFW GDWD W\SH FDOOHG PDS WKDW KHOSV WR VROYH WKHVH W\SHV RI WDVNV $ PDS DOORZV \RX WR FUHDWH PDQ\ RQH ZD\ DVVRFLDWLRQV EHWZHHQ SDLUV RI REMHFWV :H VD\ WKDW D PDS DVVRFLDWHV D NH\ ZLWK D YDOXH $ NH\ FDQ RQO\ PDS WR RQH YDOXH EXW LW V SRVVLEOH IRU PXOWLSOH NH\V WR PDS WR WKH VDPH YDOXH 0DS $ GDWD VWUXFWXUH WKDW DVVRFLDWHV REMHFWV FDOOHG NH\V ZLWK REMHFWV FDOOHG YDOXHV $ PDS FDQ EH WKRXJKW RI DV D SDLU RI FRQQHFWHG FROOHFWLRQV D VHW RI NH\V DQG D FROOHFWLRQ RI YDOXHV DVVRFLDWHG ZLWK WKRVH NH\V 7KH IROORZLQJ GLDJUDP LV DQ H[DPSOH RI WKLV LGHD 593 %DVLF 0DS 2SHUDWLRQV $ 0DS LV FRQVWUXFWHG ZLWK QRW RQH EXW WZR JHQHULF W\SH SDUDPHWHUV VHSDUDWHG E\ FRPPDV 7KH ILUVW W\SH SDUDPHWHU UHSUHVHQWV WKH W\SH RI WKH NH\V DQG WKH VHFRQG UHSUHVHQWV WKH W\SH RI WKH YDOXHV 7KLV PDNHV IRU D OHQJWK\ GHFODUDWLRQ OLQH LQ \RXU FRGH 7KH IROORZLQJ LV DQ H[DPSOH RI FRQVWUXFWLQJ D VDODU\ PDS ZKLFK DVVRFLDWHV SHRSOH V QDPHV ZLWK WKHLU VDODULHV 1RWLFH WKDW ZH KDYH WR XVH WKH ZUDSSHU W\SH 'RXEOH UDWKHU WKDQ WKH SULPLWLYH W\SH GRXEOH Map<String, Double> salaryMap = new HashMap<String, Double>(); 7KHUH LV DQ LQWHUIDFH QDPHG 0DS WR UHSUHVHQW WKH 0DS $'7 WKH WZR SULPDU\ FODVVHV WKDW LPSOHPHQW WKH LQWHUIDFH DUH QDPHG +DVK0DS DQG 7UHH0DS +DVK0DS LV WKH PRUH JHQHUDO SXUSRVH PDS DQG 7UHH0DS VWRUHV FRPSDUDEOH NH\V LQ VRUWHG RUGHU 2QH PDLQ RSHUDWLRQ RQ D PDS LV WKH SXW PHWKRG ZKLFK LV URXJKO\ VLPLODU WR WKH DGG PHWKRG RI PRVW RWKHU FROOHFWLRQV 7KH SXW PHWKRG DFFHSWV D NH\ DQG D YDOXH DV SDUDPHWHUV DQG VWRUHV D PDSSLQJ EHWZHHQ WKH NH\ DQG YDOXH LQWR WKH PDS ,I WKH NH\ ZDV SUHYLRXVO\ DVVRFLDWHG ZLWK VRPH RWKHU YDOXH LW LV UHSODFHG salaryMap.put("Stuart Reges", 20000.00); salaryMap.put("Marty Stepp", 15500.00); salaryMap.put("Jenny", 86753.09); 2QFH \RX YH DGGHG D NH\ YDOXH SDLU WR WKH PDS \RX FDQ ORRN XS D YDOXH ODWHU E\ FDOOLQJ WKH PDS V JHW PHWKRG ZKLFK DFFHSWV D NH\ DV D SDUDPHWHU DQG UHWXUQV WKH YDOXH DVVRFLDWHG ZLWK WKDW NH\ double jenSalary = salaryMap.get("Jenny"); System.out.println("Jenny's salary is $" + jenSalary); 7R VHH ZKHWKHU D PDS FRQWDLQV D PDSSLQJ IRU D JLYHQ NH\ \RX FDQ XVH WKH FRQWDLQV.H\ PHWKRG RU \RX FDQ FDOO WKH JHW PHWKRG DQG WHVW IRU D QXOO UHVXOW 594 Scanner console = new Scanner(System.in); System.out.print("Type a person's name: "); String name = console.nextLine(); // search the map for the given name if (salaryMap.containsKey(name)) { double salary = salaryMap.get(name); System.out.println(name + "'s salary is $" + salary + "."); } else { System.out.println("I don't have a salary record for " + name + "."); } 6RPH SHRSOH WKLQN RI PDSV DV D JHQHUDOL]DWLRQ RI OLVWV ,Q D OLVW \RX VWRUH HOHPHQWV DVVRFLDWHG ZLWK LQWHJHU LQGH[HV ,Q D PDS \RX VWRUH HOHPHQWV DVVRFLDWHG ZLWK REMHFW NH\V $ PDS LV VRPHZKDW OLNH D OLVW H[FHSW WKDW \RX FDQ XVH REMHFWV DV WKH LQGH[HV IRU VWRULQJ DQG UHWULHYLQJ HOHPHQWV 7KH IROORZLQJ LV D OLVW RI WKH PHWKRGV RI 0DSV 8VHIXO 0HWKRGV RI 0DSV 0HWKRG clear() containsKey(key) containsValue(value) get(key) isEmpty() keySet() put(key, value) putAll(map) remove(key) size() values() 'HVFULSWLRQ UHWXUQV WUXH LI WKH JLYHQ NH\ PDSV WR VRPH YDOXH LQ WKLV PDS UHWXUQV WUXH LI VRPH NH\ PDSV WR WKH JLYHQ YDOXH LQ WKLV PDS UHWXUQV WKH YDOXH DVVRFLDWHG ZLWK WKLV NH\ RU QXOO LI QRW IRXQG UHWXUQV WUXH LI WKLV FROOHFWLRQ FRQWDLQV QR NH\V RU YDOXHV UHWXUQV D 6HW RI DOO NH\V LQ WKLV PDS DVVRFLDWHV WKH JLYHQ NH\ ZLWK WKH JLYHQ YDOXH DGGV DOO NH\ YDOXH PDSSLQJV IURP WKH JLYHQ PDS WR WKLV PDS UHPRYHV WKH JLYHQ NH\ DQG LWV DVVRFLDWHG YDOXH IURP WKLV PDS UHWXUQV WKH QXPEHU RI NH\ YDOXH PDSSLQJV LQ WKLV PDS UHWXUQV D &ROOHFWLRQ RI DOO YDOXHV LQ WKLV PDS 0DSV GR KDYH D WR6WULQJ PHWKRG WKDW UHSUHVHQWV WKHP DV HDFK NH\ IROORZHG E\ DQ VLJQ IROORZHG E\ WKH YDOXH WR ZKLFK WKDW NH\ PDSV 7KH RUGHU LQ ZKLFK WKH NH\V DSSHDU GHSHQGV RQ WKH W\SH RI PDS XVHG ZKLFK ZH OO JHW WR LQ D PRPHQW +HUH V ZKDW WKH VDODU\ PDS GHFODUHG SUHYLRXVO\ ZRXOG ORRN OLNH ZKHQ SULQWHG {Jenny=86753.09, Stuart Reges=20000.0, Marty Stepp=15500.0} 0DS 9LHZV NH\6HW DQG YDOXHV 8QOLNH PRVW FROOHFWLRQV D PDS GRHVQ W KDYH DQ LWHUDWRU PHWKRG EHFDXVH LW ZRXOGQ W EH FOHDU H[DFWO\ ZKDW \RX ZDQWHG WR LWHUDWH RYHU 7KH NH\V" 7KH YDOXHV" %RWK" ,QVWHDG PDSV KDYH D SDLU RI PHWKRGV QDPHG NH\6HW DQG YDOXHV WKDW UHVSHFWLYHO\ UHWXUQ \RX D 6HW RI DOO NH\V LQ WKH PDS DQG D &ROOHFWLRQ RI DOO YDOXHV LQ WKH PDS 7KHVH DUH VRPHWLPHV FDOOHG FROOHFWLRQ YLHZV RI D PDS EHFDXVH WKH\ DUH HDFK FROOHFWLRQV WKDW FRQFHSWXDOO\ H[LVW LQVLGH RI WKH PDS 595 )RU H[DPSOH FRQVLGHU D PDS ZKRVH NH\V DUH SHUVRQV QDPHV DQG ZKRVH YDOXHV DUH WKHLU 6RFLDO 6HFXULW\ 1XPEHUV Map<String, Integer> ssnMap = new HashMap<String, Integer>(); ssnMap.put("Stuart Reges", 439876305); ssnMap.put("Marty Stepp", 504386382); ssnMap.put("Jenny", 867530912); ,I ZH ZDQWHG WR ZULWH D ORRS WKDW SULQWHG HYHU\ SHUVRQ V QDPH ZKR LV LQ WKH PDS ZH FRXOG FDOO WKH NH\6HW PHWKRG RQ WKH PDS 7KLV PHWKRG UHWXUQV D 6HW FROOHFWLRQ FRQWDLQLQJ HYHU\ NH\ IURP WKH KDVK WDEOH LQ WKLV FDVH HYHU\ 6WULQJ IRU D SHUVRQ V QDPH ,I \RX VWRUH WKH NH\6HW LQWR D YDULDEOH \RX VKRXOG GHFODUH WKDW YDULDEOH DV W\SH 6HW ZLWK WKH PDS V NH\V W\SH EHWZHHQ WKH DQG ! Set<String> nameSet = ssnMap.keySet(); for (String name : nameSet) { System.out.println("Name: " + name); } 7KH SUHFHGLQJ FRGH ZRXOG SURGXFH WKH IROORZLQJ RXWSXW WKH NH\V DUH LQ DQ XQSUHGLFWDEOH RUGHU VLQFH D +DVK0DS LV XVHG Name: Jenny Name: Stuart Reges Name: Marty Stepp ,I ZH LQVWHDG ZDQWHG WR ORRS RYHU HYHU\ 6RFLDO 6HFXULW\ 1XPEHU HYHU\ YDOXH VWRUHG LQ WKH PDS ZH G LQVWHDG FDOO WKH YDOXHV PHWKRG RQ WKH PDS 7KH YDOXHV PHWKRG UHWXUQV D UHIHUHQFH RI W\SH &ROOHFWLRQ DQG QRW RI W\SH 6HW EHFDXVH WKH YDOXHV PD\ FRQWDLQ GXSOLFDWHV VLQFH LW V OHJDO IRU WZR NH\V WR PDS WR WKH VDPH YDOXH ,I \RX VWRUH WKH NH\6HW LQWR D YDULDEOH \RX VKRXOG GHFODUH WKDW YDULDEOH DV W\SH &ROOHFWLRQ ZLWK WKH PDS V YDOXHV W\SH EHWZHHQ WKH DQG ! Collection<Integer> ssnValues = ssnMap.values(); for (int ssn : ssnValues) { System.out.println("SSN: " + ssn); } 7KH SUHFHGLQJ FRGH ZRXOG SURGXFH WKH IROORZLQJ RXWSXW SSN: 867530912 SSN: 439876305 SSN: 504386382 7KH NH\V DQG YDOXHV DUH RIWHQ FRPELQHG E\ ORRSLQJ RYHU WKH NH\V DQG WKHQ JHWWLQJ WKH YDOXH IRU HDFK NH\ 1RWLFH WKDW WKH IROORZLQJ FRGH DOVR GRHVQ W GHFODUH D YDULDEOH WR VWRUH WKH NH\ VHW EXW LQVWHDG MXVW FDOOV NH\6HW GLUHFWO\ LQ WKH IRU HDFK ORRS for (String name : ssnMap.keySet()) { int ssn = ssnMap.get(name); System.out.println(name + "'s SSN is " + ssn); } 7KH UHVXOWLQJ RXWSXW LV WKH IROORZLQJ 596 Jenny's SSN is 867530912 Stuart Reges's SSN is 439876305 Marty Stepp's SSN is 504386382 7KHUH V D UHODWHG PHWKRG QDPHG HQWU\6HW WKDW UHWXUQV REMHFWV RI D W\SH FDOOHG (QWU\ WKDW UHSUHVHQWV ERWK WKH NH\V DQG WKH YDOXHV EXW ZH ZRQ W H[SORUH WKLV KHUH 7UHH0DS YHUVXV +DVK0DS 6LPLODUO\ WR 6HWV ZKLFK ZH VDZ SUHYLRXVO\ WKHUH DUH WZR IODYRUV RI 0DS FROOHFWLRQV LQ -DYD +DVK0DS DQG 7UHH0DS $V ZLWK VHWV D +DVK6HW SHUIRUPV D ELW IDVWHU DQG FDQ VWRUH DQ\ W\SH RI GDWD DQG LW NHHSV LWV NH\V LQ D VRPHZKDW KDSKD]DUG RUGHU %\ FRQWUDVW D 7UHH0DS FDQ RQO\ VWRUH &RPSDUDEOH GDWD DQG LV D ELW VORZHU EXW LW NHHSV LWV NH\V LQ VRUWHG RUGHU ,I ZH GHFODUHG WKH 6RFLDO 6HFXULW\ 1XPEHU PDS IURP WKH SUHYLRXV VHFWLRQ WR XVH D 7UHH0DS ZH G VHH D GLIIHUHQW RUGHULQJ WR WKH NH\V ZKHQ ZH SULQWHG LW RQ WKH FRQVROH Map<String, Integer> ssnMap = new TreeMap<String, Integer>(); ssnMap.put("Stuart Reges", 439876305); ssnMap.put("Marty Stepp", 504386382); ssnMap.put("Jenny", 867530912); System.out.println(map); 7KH IROORZLQJ RXWSXW ZRXOG EH SURGXFHG 1RWLFH WKDW WKH QDPHV WKH PDS V NH\V DUH LQ VRUWHG DOSKDEHWLFDO RUGHU {Jenny=867530912, Marty Stepp=504386382, Stuart Reges=439876305} +DVK0DS LV VWLOO WKH RQH UHFRPPHQGHG IRU JHQHUDO XVH E\ 6XQ EHFDXVH PDQ\ DSSOLFDWLRQV GRQ W FDUH DERXW WKH RUGHU RI WKH NH\V EHFDXVH PDQ\ SURJUDPV EHQHILW IURP LWV EHWWHU SHUIRUPDQFH DQG EHFDXVH LW ZRUNV HYHQ RQ GDWD WKDW LVQ W &RPSDUDEOH DQG WKHUHIRUH GRHVQ W KDYH DQ\ QDWXUDO RUGHULQJ 0DS ([DPSOH :RUG &RXQW ,Q DQ HDUOLHU H[DPSOH ZH FRXQWHG WKH QXPEHU RI XQLTXH ZRUGV LQ WKH ERRN 0RE\ 'LFN :KDW LI ZH ZHUH DVNHG WR ILQG WKH ZRUGV LQ WKH ERRN WKDW RFFXU WKH PRVW WLPHV" ,W VHHPV OLNH IRU HDFK ZRUG LQ WKH ERRN WKDW ZRUG KDV D FRXQW RI KRZ PDQ\ WLPHV LW RFFXUV ,I ZH FRXOG H[DPLQH DOO RI WKRVH FRXQWV DQG SULQW WKH RQHV ZLWK ODUJH YDOXHV ZH G KDYH RXU DQVZHU ,W WXUQV RXW WKDW PDSV DUH YHU\ XVHIXO IRU VROYLQJ VXFK D SUREOHP :H FDQ PDLQWDLQ D ZRUG FRXQW PDS ZKHUH HDFK NH\ LV D ZRUG DQG LWV DVVRFLDWHG YDOXH LV WKH QXPEHU RI RFFXUUHQFHV RI WKDW ZRUG LQ WKH ERRN 597 wordCountMap = empty. for (each word from file) { if (I have never seen this word before) { set this word's count to 1. } else { increase this word's count by one. } } $VVXPLQJ WKDW ZH KDYH D 6FDQQHU WR UHDG WKH DSSURSULDWH ILOH DQG D 0DS WR VWRUH ZRUG FRXQWV OLNH WKH IROORZLQJ Map<String, Integer> wordCountMap = new TreeMap<String, Integer>(); Scanner in = new Scanner(new File("mobydick.txt")); :H FRXOG UHDG WKH ILOH V FRQWHQWV DQG VWRUH WKHP LQ WKH PDS DV IROORZV 1RWLFH WKDW LI WKH ZRUG KDV EHHQ VHHQ EHIRUH ZH UHWULHYH LWV ROG FRXQW YDOXH LQFUHPHQW LW E\ WKHQ SXW WKH QHZ YDOXH EDFN LQWR WKH PDS :KHQ \RX SXW D NH\ YDOXH PDSSLQJ LQWR D PDS WKDW DOUHDG\ FRQWDLQV WKDW NH\ WKH ROG PDSSLQJ LV UHSODFHG )RU H[DPSOH LI WKH ZRUG RFHDQ PDSSHG WR WKH QXPEHU DQG ZH SXW D QHZ PDSSLQJ IURP RFHDQ WR WKH ROG PDSSLQJ IURP RFHDQ WR LV UHSODFHG :H GRQ W KDYH WR UHPRYH LW PDQXDOO\ while (in.hasNext()) { String word = in.next(); if (!wordCountMap.containsKey(word)) { map.put(word, 1); } else { int count = map.get(word); map.put(word, count + 1); } } // never seen before // seen before 2QFH ZH YH EXLOW WKH ZRUG FRXQW PDS LI ZH ZDQWHG WR SULQW DOO ZRUGV WKDW DSSHDU PRUH WKDQ VD\ WLPHV LQ WKH ERRN ZH FRXOG ZULWH FRGH OLNH WKH IROORZLQJ for (String word : wordCountMap.keySet()) { int count = wordCountMap.get(word); if (count > 2000) { System.out.println(word + " occurs " + count + " times."); } } +HUH V WKH FRPSOHWH SURJUDP ZLWK D PHWKRG DGGHG IRU VWUXFWXUH DQG D FRQVWDQW IRU WKH QXPEHU RI RFFXUUHQFHV QHHGHG IRU D ZRUG WR EH SULQWHG 598 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 // Uses maps to implement a word count, so that the user // can see which words occur the most in the book Moby Dick. import java.io.*; import java.util.*; public class WordCount { // minimum number of occurrences needed to be printed public static final int OCCURRENCES = 2000; public static void main(String args) throws FileNotFoundException { System.out.println("This program displays the most frequently"); System.out.println("occurring words from the book Moby Dick."); System.out.println(); // read the book into a map Scanner in = new Scanner(new File("mobydick.txt")); Map<String, Integer> wordCountMap = getCountMap(in); for (String word : wordCountMap.keySet()) { int count = wordCountMap.get(word); if (count > OCCURRENCES) { System.out.println(word + " occurs " + count + " times."); } } } // Reads the book text and returns a map from words to counts public static Map<String, Integer> getCountMap(Scanner in) { Map<String, Integer> wordCountMap = new TreeMap<String, Integer>(); while (in.hasNext()) { String word = in.next().toLowerCase(); if (!wordCountMap.containsKey(word)) { // never seen this word before wordCountMap.put(word, 1); } else { // seen this word before; increment count int count = wordCountMap.get(word); wordCountMap.put(word, count + 1); } } return wordCountMap; } } 7KH SURJUDP SURGXFHV WKH IROORZLQJ RXWSXW RQ RXU FRS\ RI 0RE\ 'LFN 599 This program displays the most frequently occurring words from the book Moby Dick. a occurs 4509 times. and occurs 6138 times. his occurs 2451 times. in occurs 3975 times. of occurs 6405 times. that occurs 2705 times. the occurs 13991 times. to occurs 4433 times. &ROOHFWLRQ 2YHUYLHZ :H YH VHHQ WKUHH PDMRU DEVWUDFW GDWD W\SHV LQ WKLV FKDSWHU OLVWV VHWV DQG PDSV ,W V LPSRUWDQW WR XQGHUVWDQG WKH GLIIHUHQFHV EHWZHHQ WKHP DQG ZKHQ HDFK VKRXOG EH XVHG 7KH IROORZLQJ WDEOH VXPPDUL]HV WKH GLIIHUHQW FROOHFWLRQV DQG WKHLU SURV DQG FRQV $'7 LPSOHPHQWDWLRQV /LVW $UUD\/LVW /LQNHG/LVW GHVFULSWLRQ DQ RUGHUHG VHTXHQFH RI HOHPHQWV EHQHILWV ZHDNQHVVHV H[DPSOH XVDJHV OLVW RI DFFRXQWV ZDLWLQJ OLQH WKH OLQHV RI D ILOH XQLTXH ZRUGV LQ D ERRN ORWWHU\ WLFNHW QXPEHUV ZRUG FRXQWLQJ SKRQH ERRN PDLQWDLQV DQ LQGH[HG VORZ WR VHDUFK SRVLWLRQ IRU HYHU\ VORZ WR DGG HOHPHQW UHPRYH DUELWUDU\ HOHPHQWV 6HW +DVK6HW 7UHH6HW D VHW RI XQLTXH HOHPHQWV IDVW WR VHDUFK QR GXSOLFDWHV PLVVLQJ LQGH[HV FDQQRW UHWULHYH DUELWUDU\ HOHPHQWV IURP LW 0DS +DVK0DS 7UHH0DS D JURXS RI DVVRFLDWLRQV EHWZHHQ SDLUV RI NH\ REMHFWV DQG YDOXH REMHFWV DOORZV \RX WR FRQQHFW KDUGHU WR XQGHUVWDQG DQG RQH SLHFH RI LQIRUPDWLRQ WR XVH DQRWKHU DQG VHDUFK IRU RQH EDVHG RQ WKH RWKHU :KHQ DSSURDFKLQJ D QHZ SURJUDPPLQJ SUREOHP LQYROYLQJ GDWD \RX FDQ DVN \RXUVHOI TXHVWLRQV WR KHOS GHFLGH ZKDW FROOHFWLRQ LV PRVW DSSURSULDWH +HUH DUH VRPH H[DPSOHV +RZ GR \RX SODQ WR VHDUFK WKH GDWD" ,I \RX ZDQW WR ORRN XS SDUWLFXODU HOHPHQWV RI GDWD E\ LQGH[ \RX ZDQW D OLVW ,I \RX LQWHQG WR VHDUFK IRU HOHPHQWV D VHW PD\ EH EHWWHU ,I \RX QHHG WR ILQG DQ REMHFW JLYHQ SDUWLDO LQIRUPDWLRQ DERXW LW IRU H[DPSOH WR ILQG D XVHU V EDQN DFFRXQW REMHFW EDVHG RQ LWV 3,1 RU ,' QXPEHU D PDS PD\ EH EHVW :KDW DUH \RX JRLQJ WR GR ZLWK WKH GDWD" 'R \RX LQWHQG WR DGG DQG UHPRYH PDQ\ HOHPHQWV RU VHDUFK WKH GDWD PDQ\ WLPHV RU FRQQHFW WKLV GDWD WR RWKHU GDWD" 600 ,Q ZKDW RUGHU VKRXOG WKH HOHPHQWV EH VWRUHG" /LVWV RUGHU WKHLU HOHPHQWV E\ RUGHU RI LQVHUWLRQ ZKLOH WKH 7UHH FROOHFWLRQV 7UHH6HW DQG 7UHH0DS RUGHU HOHPHQWV E\ WKHLU &RPSDUDEOH QDWXUDO RUGHULQJ ,I RUGHU GRHVQ W PDWWHU \RX PD\ ZDQW D +DVK FROOHFWLRQ VXFK DV +DVK6HW &KDSWHU 6XPPDU\ $ FROOHFWLRQ LV DQ REMHFW WKDW VWRUHV D JURXS RI RWKHU REMHFWV ([DPSOHV RI FROOHFWLRQV DUH $UUD\/LVWV +DVK6HWV DQG 7UHH0DSV &ROOHFWLRQV DUH XVHG WR VWUXFWXUH RUJDQL]H DQG VHDUFK GDWD $ OLQNHG OLVW LV D FROOHFWLRQ VLPLODU WR DQ $UUD\/LVW EXW LPSOHPHQWHG LQWHUQDOO\ E\ VWRULQJ HDFK HOHPHQW LQWR D VPDOO FRQWDLQHU REMHFW QDPHG D QRGH /LQNHG OLVWV DUH IDVWHU WKDQ DUUD\ OLVWV DW FHUWDLQ RSHUDWLRQV VXFK DV DGGLQJ DQG UHPRYLQJ IURP WKH IURQW RU PLGGOH RI WKH OLVW $Q LWHUDWRU LV DQ REMHFW WKDW UHSUHVHQWV D SRVLWLRQ LQ D OLVW DQG OHWV \RX H[DPLQH LWV HOHPHQWV LQ RUGHU /LQNHG OLVWV DUH RIWHQ XVHG ZLWK LWHUDWRUV EHFDXVH LW V VORZ WR DFFHVV DQ DUELWUDU\ HOHPHQW IURP WKH OLVW $Q DEVWUDFW GDWD W\SH $'7 LV D VSHFLILFDWLRQ RI D GDWD VWUXFWXUH E\ ZKDW GDWD LW FDQ KROG DQG ZKDW RSHUDWLRQV LW FDQ SHUIRUP 7ZR H[DPSOHV RI $'7V DUH /LVW DQG 6HW $'7V LQ -DYD V FROOHFWLRQ IUDPHZRUN DUH UHSUHVHQWHG DV LQWHUIDFHV VXFK DV WKH /LVW LQWHUIDFH ZKLFK LV LPSOHPHQWHG E\ ERWK /LQNHG/LVW DQG $UUD\/LVW $ VHW LV D FROOHFWLRQ WKDW GRHVQ W DOORZ GXSOLFDWHV 6HWV DOVR JHQHUDOO\ FDQ EH VHDUFKHG YHU\ TXLFNO\ WR VHH ZKHWKHU WKH\ FRQWDLQ D SDUWLFXODU HOHPHQW YDOXH 7KH 6HW LQWHUIDFH UHSUHVHQWV VHWV 7KHUH DUH WZR PDMRU VHW FODVVHV LQ -DYD 7UHH6HW DQG +DVK6HW $ 7UHH6HW KROGV &RPSDUDEOH GDWD LQ D VRUWHG RUGHU D +DVK6HW FDQ KROG DQ\ GDWD DQG FDQ EH VHDUFKHG PRUH TXLFNO\ EXW LWV HOHPHQWV DUH VWRUHG LQ XQSUHGLFWDEOH RUGHU $ PDS LV D FROOHFWLRQ WKDW DVVRFLDWHV NH\ REMHFWV ZLWK YDOXH REMHFWV 0DSV DUH XVHG WR FUHDWH DVVRFLDWLRQDO UHODWLRQVKLSV EHWZHHQ SLHFHV RI GDWD VXFK DV DVVRFLDWLQJ D SHUVRQ V QDPH ZLWK WKHLU SKRQH QXPEHU 7KHUH DUH WZR PDMRU PDS FODVVHV LQ -DYD 7UHH6HW DQG +DVK6HW $ 7UHH0DS KROGV &RPSDUDEOH NH\V LQ D VRUWHG RUGHU D +DVK0DS FDQ KROG DQ\ GDWD DV LWV NH\V DQG SHUIRUPV YDOXH ORRNXSV PRUH TXLFNO\ EXW LWV NH\V DUH VWRUHG LQ XQSUHGLFWDEOH RUGHU 6HOI &KHFN 3UREOHPV 6HFWLRQ /LVWV :KHQ VKRXOG \RX XVH D /LQNHG/LVW LQVWHDG RI DQ $UUD\/LVW" :RXOG D /LQNHG/LVW RU DQ $UUD\/LVW SHUIRUP EHWWHU ZKHQ UXQ RQ WKH IROORZLQJ FRGH" :K\" 601 // This code performs very poorly on a linked list! public static int min(LinkedList<Integer> list) { int min = list.get(0); for (int i = 1; i < list.size(); i++) { if (list.get(i) < min) { min = list.get(i); } } return min; } :KDW LV DQ LWHUDWRU" :K\ DUH LWHUDWRUV RIWHQ XVHG ZLWK OLQNHG OLVWV" :ULWH D SLHFH RI FRGH WKDW FRXQWV WKH QXPEHU RI GXSOLFDWH HOHPHQWV LQ D OLQNHG OLVW WKDW LV WKH QXPEHU RI HOHPHQWV ZKRVH YDOXHV DUH UHSHDWHG DW DQ HDUOLHU LQGH[ LQ WKH OLVW $VVXPH WKDW DOO GXSOLFDWHV LQ WKH OLVW RFFXU FRQVHFXWLYHO\ )RU H[DPSOH WKH OLVW > @ FRQWDLQV GXSOLFDWHV RQH GXSOLFDWH RI HOHPHQW YDOXH WKUHH GXSOLFDWHV RI HOHPHQW YDOXH DQG RQH GXSOLFDWH RI HOHPHQW YDOXH <RX PD\ ZLVK WR SXW \RXU FRGH LQWR D PHWKRG QDPHG FRXQW'XSOLFDWHV WKDW DFFHSWV WKH OLQNHG OLVW DV D SDUDPHWHU DQG UHWXUQV WKH QXPEHU RI GXSOLFDWHV :ULWH D SLHFH RI FRGH WKDW LQVHUWV D 6WULQJ LQWR DQ RUGHUHG OLQNHG OLVW RI 6WULQJV PDLQWDLQLQJ VRUWHG RUGHU )RU WKH OLVW > $OSKD %DNHU )R[WURW 7DQJR :KLVNH\ @ LQVHUWLQJ &KDUOLH LQ RUGHU ZRXOG SURGXFH WKH OLVW > $OSKD %DNHU &KDUOLH )R[WURW 7DQJR :KLVNH\ @ :ULWH D PHWKRG QDPHG UHPRYH$OO WKDW DFFHSWV D OLQNHG OLVW RI LQWHJHUV DV D SDUDPHWHU DQG UHPRYHV DOO RFFXUUHQFHV RI D SDUWLFXODU YDOXH <RX PXVW SUHVHUYH WKH RULJLQDO UHODWLYH RUGHU RI WKH UHPDLQLQJ HOHPHQWV RI WKH OLVW )RU WKH OLVW > @ WKH FDOO removeAll(list, 3) ZRXOG FKDQJH WKH OLVW WR > @ :ULWH D PHWKRG QDPHG ZUDS+DOI WKDW DFFHSWV D OLQNHG OLVW RI LQWHJHUV DV D SDUDPHWHU DQG PRYHV WKH ILUVW KDOI RI WKH OLVW RQWR WKH EDFN RI WKH OLVW ,I WKH OLVW FRQWDLQV DQ RGG QXPEHU RI HOHPHQWV WKH VPDOOHU KDOI LV ZUDSSHG LQ RWKHU ZRUGV IRU D OLVW RI VL]H 1 WKH PLGGOH HOHPHQW 1 EHFRPHV WKH ILUVW HOHPHQW LQ DOO FDVHV )RU WKH OLVW > @ FDOOLQJ ZUDS+DOI RQ WKH OLVW ZRXOG FKDQJH WKDW OLVW LQWR > @ )RU WKH OLVW > @ WKH UHVXOW ZRXOG EH > @ :KDW LV DQ DEVWUDFW GDWD W\SH $'7 " :KDW $'7 GRHV D OLQNHG OLVW LPSOHPHQW" $ SUHYLRXV TXHVWLRQ DVNHG \RX WR FRXQW WKH GXSOLFDWHV LQ D OLQNHG OLVW 5HZULWH WKLV DV D PHWKRG QDPHG FRXQW'XSOLFDWHV WKDW ZLOO DOORZ HLWKHU DQ $UUD\/LVW RU D /LQNHG/LVW WR EH SDVVHG DV WKH SDUDPHWHU 6HFWLRQ 6HWV /LVWV KDYH HYHU\ PHWKRG WKDW D 6HW KDV DQG PRUH 6R ZK\ ZRXOG \RX XVH D 6HW UDWKHU WKDQ D /LVW" :KHQ VKRXOG \RX XVH D 7UHH6HW DQG ZKHQ VKRXOG \RX XVH D +DVK6HW" 602 $ 6HW GRHVQ W KDYH WKH JHW DQG VHW PHWKRGV WKDW DQ $UUD\/LVW KDV 6R KRZ GR \RX H[DPLQH HYHU\ HOHPHQW RI D 6HW" :KDW HOHPHQWV DUH FRQWDLQHG LQ WKH IROORZLQJ VHW DIWHU WKLV FRGH H[HFXWHV" Set<Integer> set = new HashSet<Integer>(); set.add(74); set.add(12); set.add(182); set.add(90); set.add(43); set.remove(74); set.remove(999); set.remove(43); set.add(32); set.add(182); set.add(9); set.add(29); :KDW HOHPHQWV DUH FRQWDLQHG LQ WKH IROORZLQJ VHW DIWHU WKLV FRGH H[HFXWHV" Set<Integer> set = new HashSet<Integer>(); set.add(8); set.add(41); set.add(18); set.add(50); set.add(132); set.add(28); set.add(79); set.remove(41); set.remove(28); set.add(86); set.add(98); set.remove(18); :KDW HOHPHQWV DUH FRQWDLQHG LQ WKH IROORZLQJ VHW DIWHU WKLV FRGH H[HFXWHV" Set<Integer> set = new HashSet<Integer>(); set.add(4); set.add(15); set.add(73); set.add(84); set.add(247); set.remove(15); set.add(42); set.add(12); set.remove(73); set.add(94); set.add(11); +RZ GR \RX SHUIRUP D XQLRQ RSHUDWLRQ RQ WZR VHWV" $Q LQWHUVHFWLRQ" 7U\ WR JLYH DQ DQVZHU WKDW GRHVQ W UHTXLUH DQ\ ORRSV 603 6HFWLRQ 0DSV :ULWH WKH FRGH WR GHFODUH D 0DS IURP SHRSOH V QDPHV WR WKHLU DJHV $GG PDSSLQJV IRU \RXU RZQ QDPH DJH DV ZHOO DV WKRVH RI D IHZ IULHQGV RU UHODWLYHV $ 0DS GRHVQ W KDYH WKH JHW DQG VHW PHWKRGV WKDW DQ $UUD\/LVW KDV ,W GRHVQ W HYHQ KDYH DQ LWHUDWRU PHWKRG OLNH D 6HW GRHV QRU FDQ D IRU HDFK ORRS EH XVHG RQ LW GLUHFWO\ 6R KRZ GR \RX H[DPLQH HYHU\ NH\ RU HYHU\ YDOXH RI D 0DS" :KDW NH\V DQG YDOXHV DUH FRQWDLQHG LQ WKH IROORZLQJ PDS DIWHU WKLV FRGH H[HFXWHV" HashMap<Object, String> map = new HashMap<Object, String>(); map.put(7, "Marty"); map.put(34, "Louann"); map.put(27, "Donald"); map.put(15, "Moshe"); map.put(84, "Larry"); map.remove("Louann"); map.put(7, "Ed"); map.put(2350, "Orlando"); map.remove(8); map.put(5, "Moshe"); map.remove(84); map.put(17, "Steve"); :KDW NH\V DQG YDOXHV DUH FRQWDLQHG LQ WKH IROORZLQJ PDS DIWHU WKLV FRGH H[HFXWHV" HashMap<Integer, String> map = new HashMap<Integer, String>(); map.put(8, "Eight"); map.put(41, "Forty-one"); map.put(8, "Ocho"); map.put(18, "Eighteen"); map.put(50, "Fifty"); map.put(132, "OneThreeTwo"); map.put(28, "Twenty-eight"); map.put(79, "Seventy-nine"); map.remove(41); map.remove(28); map.remove("Eight"); map.put(86, "Eighty-six"); map.put(98, "Ninety-eight"); map.remove(18); 0RGLI\ WKH ZRUG FRXQW SURJUDP VR WKDW LW SULQWV WKH ZRUGV VRUWHG E\ WKH QXPEHU RI RFFXUUHQFHV 7R GR WKLV ZULWH FRGH DW WKH HQG RI WKH SURJUDP WR FUHDWH D UHYHUVH PDS IURP FRXQWV WR ZRUGV EDVHG RQ WKH RULJLQDO PDS $VVXPH WKDW QR WZR ZRUGV RI LQWHUHVW RFFXU WKH H[DFW VDPH QXPEHU RI WLPHV 604 ([HUFLVHV 0RGLI\ WKH 6LHYH SURJUDP H[DPSOH IURP WKLV FKDSWHU WR PDNH RSWLPL]DWLRQV )LUVW LQVWHDG RI VWRULQJ DOO LQWHJHUV XS WR WKH PD[LPXP LQ WKH QXPEHUV OLVW RQO\ VWRUH DQG DOO RGGV VWDUWLQJ IURP XSZDUG 6HFRQG LI WKH IURQW RI WKH QXPEHUV OLVW HYHU UHDFKHV WKH VTXDUH URRW RI WKH PD[LPXP \RX PD\ SXW DOO UHPDLQLQJ YDOXHV IURP WKH QXPEHUV OLVW LQWR WKH SULPHV OLVW :K\ LV WKLV WUXH" :ULWH D PHWKRG QDPHG DOWHUQDWH WKDW DFFHSWV WZR /LVWV DV LWV SDUDPHWHUV DQG UHWXUQV D QHZ /LVW FRQWDLQLQJ DOWHUQDWLQJ HOHPHQWV IURP WKH WZR OLVWV LQ WKH IROORZLQJ RUGHU ILUVW HOHPHQW IURP ILUVW OLVW ILUVW HOHPHQW IURP VHFRQG OLVW VHFRQG HOHPHQW IURP ILUVW OLVW VHFRQG HOHPHQW IURP VHFRQG OLVW WKLUG HOHPHQW IURP ILUVW OLVW WKLUG HOHPHQW IURP VHFRQG OLVW ,I WKH OLVWV GR QRW FRQWDLQ WKH VDPH QXPEHU RI HOHPHQWV WKH UHPDLQLQJ HOHPHQWV IURP WKH ORQJHU OLVW VLW FRQVHFXWLYHO\ DW WKH HQG )RU D ILUVW OLVW RI > @ DQG D VHFRQG OLVW RI > @ WKH FDOO RI alternate(list1, list2) VKRXOG UHWXUQ D OLVW FRQWDLQLQJ > @ :ULWH D PHWKRG QDPHG UHPRYH,Q5DQJH WKDW DFFHSWV IRXU SDUDPHWHUV D /LQNHG/LVW DQ HOHPHQW YDOXH D VWDUWLQJ LQGH[ DQG DQ HQGLQJ LQGH[ ,WV EHKDYLRU LV WR UHPRYH DOO RFFXUUHQFHV RI WKH JLYHQ HOHPHQW WKDW DSSHDU LQ WKH OLVW EHWZHHQ WKH VWDUWLQJ LQGH[ LQFOXVLYH DQG WKH HQGLQJ LQGH[ H[FOXVLYH 2WKHU YDOXHV DQG RFFXUUHQFHV RI WKH JLYHQ YDOXH WKDW DSSHDU RXWVLGH WKH JLYHQ LQGH[ UDQJH DUH QRW DIIHFWHG )RU H[DPSOH ZLWK WKH OLVW > removeInRange(list, 0, 5, 13); ZRXOG SURGXFH WKH OLVW > 1RWLFH WKDW WKH ]HURHV ZKRVH LQGH[HV ZHUH EHWZHHQ LQFOXVLYH DQG RULJLQDO OLVW EHIRUH DQ\ PRGLILFDWLRQV DUH PDGH DUH UHPRYHG @ WKH FDOO RI @ H[FOXVLYH LQ WKH :ULWH D PHWKRG QDPHG SDUWLWLRQ WKDW DFFHSWV D OLVW RI LQWHJHUV DQG DQ LQWHJHU YDOXH ( DV LWV SDUDPHWHU DQG UHDUUDQJHV SDUWLWLRQV WKH OLVW VR WKDW DOO LWV HOHPHQWV OHVV WKDQ ( RFFXU EHIRUH DOO HOHPHQWV JUHDWHU WKDQ ( 7KH H[DFW RUGHU RI WKH HOHPHQWV LV XQLPSRUWDQW VR ORQJ DV DOO HOHPHQWV OHVV WKDQ ( DSSHDU EHIRUH DOO HOHPHQWV JUHDWHU WKDQ ( )RU H[DPSOH IRU WKH OLQNHG OLVW > @ RQH DFFHSWDEOH RUGHULQJ RI WKH OLVW DIWHU WKH FDOO RI SDUWLWLRQ OLVW ZRXOG EH > @ <RX PD\ DVVXPH WKDW WKH OLVW FRQWDLQV QR GXSOLFDWHV DQG GRHV QRW FRQWDLQ WKH HOHPHQW YDOXH ( :ULWH D PHWKRG QDPHG VRUW$QG5HPRYH'XSOLFDWHV WKDW DFFHSWV D OLVW RI LQWHJHUV DV LWV SDUDPHWHU DQG UHDUUDQJHV WKH OLVW V HOHPHQWV LQWR VRUWHG DVFHQGLQJ RUGHU DV ZHOO DV UHPRYLQJ DOO GXSOLFDWH YDOXHV IURP WKH OLVW )RU H[DPSOH WKH OLVW > @ ZRXOG EHFRPH > @ DIWHU D FDOO WR \RXU PHWKRG 8VH D 6HW DV SDUW RI \RXU VROXWLRQ 605 :ULWH D PHWKRG QDPHG V\PPHWULF6HW'LIIHUHQFH WKDW DFFHSWV WZR 6HWV DV SDUDPHWHUV DQG UHWXUQV D QHZ 6HW FRQWDLQLQJ WKHLU V\PPHWULF VHW GLIIHUHQFH WKH VHW RI HOHPHQWV FRQWDLQHG LQ HLWKHU RI WZR VHWV EXW QRW LQ ERWK )RU H[DPSOH WKH V\PPHWULF GLIIHUHQFH EHWZHHQ WKH VHWV ^ ` DQG ^ ` LV ^ ` :ULWH D PHWKRG QDPHG LV WR WKDW DFFHSWV D 0DS6WULQJ 6WULQJ! DV LWV SDUDPHWHU DQG UHWXUQV WUXH LI QR WZR NH\V PDS WR WKH VDPH YDOXH )RU H[DPSOH ^0DUW\ +DZNLQJ 6PLWK 1HZWRQ ` UHWXUQV IDOVH EXW ^0DUW\ +DZNLQJ 6PLWK 1HZWRQ ` UHWXUQV WUXH 7KH HPSW\ PDS LV FRQVLGHUHG WR DQG UHWXUQV WUXH :ULWH D PHWKRG QDPHG VXE0DS WKDW DFFHSWV WZR 0DS6WULQJ 6WULQJ! DV LWV SDUDPHWHUV DQG UHWXUQV WUXH LI HYHU\ NH\ LQ WKH ILUVW PDS LV DOVR FRQWDLQHG LQ WKH VHFRQG PDS DQG HYHU\ NH\ LQ WKH ILUVW PDS PDSV WR WKH VDPH YDOXH LQ WKH VHFRQG PDS )RU H[DPSOH ^6PLWK 0DUW\ ` LV D VXE0DS RI ^0DUW\ +DZNLQJ 6PLWK 1HZWRQ ` 7KH HPSW\ PDS LV D VXE0DS RI HYHU\ PDS :ULWH D PHWKRG QDPHG UHYHUVH WKDW DFFHSWV D 0DS6WULQJ 6WULQJ! DV D SDUDPHWHU DQG UHWXUQV D QHZ 0DS WKDW LV WKH RULJLQDO V UHYHUVH 7KH UHYHUVH RI D PDS LV D QHZ PDS WKDW XVHV WKH YDOXHV IURP WKH RULJLQDO DV LWV NH\V DQG WKH NH\V IURP WKH RULJLQDO DV LWV YDOXHV 6LQFH D PDS V YDOXHV QHHG QRW EH XQLTXH EXW LWV NH\V PXVW EH \RX VKRXOG KDYH HDFK YDOXH PDS WR D VHW RI NH\V ,Q RWKHU ZRUGV LI WKH RULJLQDO PDS PDSV NH\V RI W\SH . WR YDOXHV RI W\SH 9 WKH QHZ PDS PDSV NH\V RI W\SH 9 WR YDOXHV WKDW DUH 6HWV FRQWDLQLQJ HOHPHQWV RI W\SH . )RU H[DPSOH WKH PDS ^ 0DUW\ 6XH (G 'DYH (G 0DUW\ (G` KDV D UHYHUVH RI ^0DUW\ > @ 6XH > @ (G > @ 'DYH > @` 7KH RUGHU RI WKH NH\V DQG YDOXHV GRHV QRW PDWWHU 3URJUDPPLQJ 3URMHFWV :ULWH D SURJUDP WKDW FRPSXWHV WKH HGLW GLVWDQFH EHWZHHQ WZR ZRUGV 7KH HGLW GLVWDQFH DOVR FDOOHG /HYHQVKWHLQ GLVWDQFH IRU FUHDWRU 9ODGLPLU /HYHQVKWHLQ EHWZHHQ WZR VWULQJV LV WKH PLQLPXP QXPEHU RI RSHUDWLRQV QHHGHG WR WUDQVIRUP RQH VWULQJ LQWR WKH RWKHU )RU WKLV SURJUDP DQ RSHUDWLRQ LV D VXEVWLWXWLRQ RI D VLQJOH FKDUDFWHU VXFK DV IURP EULVN WR EULFN 7KH HGLW GLVWDQFH EHWZHHQ WKH ZRUGV GRJ DQG FDW LV EHFDXVH WKH FKDLQ RI GRW FRW DQG FDW WUDQVIRUPV GRJ LQWR FDW :KHQ FRPSXWLQJ WKH HGLW GLVWDQFH EHWZHHQ WZR ZRUGV HDFK LQWHUPHGLDWH ZRUG PXVW EH DQ DFWXDO YDOLG ZRUG (GLW GLVWDQFHV DUH XVHIXO LQ DSSOLFDWLRQV WKDW QHHG WR GHWHUPLQH KRZ VLPLODU WZR VWULQJV DUH VXFK DV VSHOO FKHFNHUV 5HDG \RXU LQSXW IURP D GLFWLRQDU\ WH[W ILOH )URP WKLV ILOH FRPSXWH D PDS IURP HYHU\ ZRUG WR LWV LPPHGLDWH QHLJKERUV WKDW LV WKH ZRUGV WKDW KDYH DQ HGLW GLVWDQFH RI IURP LW 2QFH WKLV PDS LV EXLOW \RX FDQ ZDON LW WR ILQG SDWKV IURP RQH ZRUG WR DQRWKHU $ JRRG ZD\ WR SURFHVV SDWKV WR ZDON WKH QHLJKERU PDS LV WR XVH D OLQNHG OLVW RI ZRUGV WR YLVLW VWDUWLQJ LQLWLDOO\ ZLWK WKH EHJLQQLQJ ZRUG VXFK DV GRJ <RXU DOJRULWKP VKRXOG UHSHDWHGO\ UHPRYH WKH IURQW ZRUG RI WKH OLVW DQG DGG DOO RI LWV QHLJKERUV WR WKH HQG RI WKH OLVW XQWLO WKH HQGLQJ ZRUG VXFK DV FDW LV IRXQG RU XQWLO WKH OLVW EHFRPHV HPSW\ ZKLFK ZRXOG LQGLFDWH WKDW QR SDWK H[LVWV EHWZHHQ WKH WZR ZRUGV 606 :ULWH D SURJUDP WKDW VROYHV D FODVVLF VWDEOH PDUULDJH SUREOHP 7KLV SUREOHP GHDOV ZLK D JURXS RI PHQ DQG D JURXS RI ZRPHQ 7KH SURJUDP WULHV WR SDLU WKHP XS VR DV WR JHQHUDWH DV PDQ\ PDUULDJHV DV SRVVLEOH WKDW DUH DOO VWDEOH $ VHW RI PDUULDJHV LV XQVWDEOH LI \RX FDQ ILQG D PDQ DQG D ZRPDQ ZKR ZRXOG UDWKHU EH PDUULHG WR HDFK RWKHU WKDQ WR WKHLU FXUUHQW VSRXVHV LQ ZKLFK FDVH WKH WZR ZRXOG EH LQFOLQHG WR GLYRUFH WKHLU VSRXVHV DQG PDUU\ HDFK RWKHU 7KH LQSXW ILOH IRU WKH SURJUDP ZLOO OLVW DOO RI WKH PHQ RQH SHU OLQH IROORZHG E\ D EODQN OLQH IROORZHG E\ DOO RI WKH ZRPHQ RQH SHU OLQH 7KH PHQ DQG ZRPHQ DUH QXPEHUHG E\ WKHLU SRVLWLRQ LQ WKH LQSXW ILOH WKH ILUVW PDQ LV WKH VHFRQG PDQ LV DQG VR RQ WKH ILUVW ZRPDQ LV WKH VHFRQG ZRPDQ LV DQG VR RQ (DFK LQSXW OLQH H[FHSW WKH EODQN OLQH VHSDUDWLQJ PHQ IURP ZRPHQ KDV D QDPH IROORZHG E\ D FRORQ IROORZHG E\ D OLVW RI LQWHJHUV 7KHVH LQWHJHUV DUH WKH SUHIHUHQFHV IRU WKLV SDUWLFXODU SHUVRQ )RU H[DPSOH WKH IROORZLQJ LQSXW OLQH LQ WKH PHQ V VHFWLRQ Joe: 10 8 35 9 20 22 33 6 29 7 32 16 18 25 LQGLFDWHV WKDW WKH SHUVRQ LV QDPHG -RH DQG WKDW KLV ILUVW FKRLFH IRU PDUULDJH LV ZRPDQ KLV VHFRQG FKRLFH LV ZRPDQ DQG VR RQ $Q\ ZRPHQ QRW OLVWHG DUH FRQVLGHUHG XQDFFHSWDEOH WR -RH 7KH VWDEOH PDUULDJH SUREOHP LV VROYHG E\ WKH IROORZLQJ DOJRULWKP assign each person to be free. while (some man M with a nonempty preference list is free) { W = first woman on M's list. if (some man P is engaged to W) { assign P to be free. assign M and W to be engaged to each other. for (each successor Q of M who is on W's list) { delete W from Q's preference list. delete Q from W's preference list. } } )RU WKH IROORZLQJ LQSXW Man 1: 4 Man 2: 2 Man 3: 2 Man 4: 3 Woman 1: Woman 2: Woman 3: Woman 4: 1 3 4 1 4 1 1 4 2 1 3 4 1 3 2 1 3 4 1 2 3 2 3 3 2 4 4 2 7KH IROORZLQJ LV D VWDEOH PDUULDJH VROXWLRQ 607 Man Man Man Man 1 3 2 4 and and and and Woman Woman Woman Woman 4 2 3 1 6WXDUW 5HJHV 0DUW\ 6WHSS 608 ...
View Full Document

Ask a homework question - tutors are online