more on collec ons and sor ng
play

More on collec)ons and sor)ng CSCI 136: Fundamentals of - PowerPoint PPT Presentation

More on collec)ons and sor)ng CSCI 136: Fundamentals of Computer Science II Keith Vertanen Overview Java Collec/ons List (last term), e.g.


  1. More ¡on ¡collec)ons ¡and ¡sor)ng ¡ CSCI ¡136: ¡Fundamentals ¡of ¡Computer ¡Science ¡II ¡ ¡• ¡ ¡Keith ¡Vertanen ¡

  2. Overview ¡ • Java ¡Collec/ons ¡ – List ¡(last ¡term), ¡e.g. ¡ ArrayList ¡ – Map ¡(last ¡/me), ¡e.g. ¡ HashMap ¡ – Map ¡from ¡first ¡principles ¡(today) ¡ – Set ¡(today), ¡e.g. ¡ HashSet ¡ • Sor/ng ¡things ¡in ¡a ¡collec/on ¡ – Collections.sort() ¡ – Sor/ng ¡your ¡own ¡data ¡types ¡ 2 ¡

  3. Making ¡my ¡own ¡map: ¡AFempt ¡1 ¡ • Goal: ¡Class ¡to ¡handle ¡mapping ¡a ¡key ¡to ¡value ¡ – Key ¡and ¡value ¡are ¡of ¡type ¡ String ¡ – Public ¡API: ¡ • put(String ¡key, ¡String ¡value) ¡ • get(String ¡key) ¡ – Inner-­‑class ¡to ¡help ¡store ¡pairs: ¡ private ¡class ¡ Pair ¡ { ¡ ¡ ¡ ¡ ¡String ¡key; ¡ ¡ ¡ ¡ ¡String ¡value; ¡ ¡ ¡ ¡ ¡ ¡public ¡ Pair(String ¡key, ¡String ¡value) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡this .key ¡= ¡key; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡this .value ¡= ¡value; ¡ ¡ ¡ ¡ ¡} ¡ } ¡ 3 ¡

  4. ArrayList<Pair> ¡pairs ¡= ¡ new ¡ ArrayList<Pair>(); ¡ ¡ Store ¡the ¡pairs ¡in ¡ public ¡void ¡ put(String ¡key, ¡String ¡value) ¡ a ¡dynamically ¡ { ¡ growing ¡array. ¡ ¡ ¡ ¡ ¡for ¡ (Pair ¡pair ¡: ¡pairs) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡ (pair.key.equals(key)) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ Every ¡/me ¡we ¡put ¡a ¡new ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡pair.value ¡= ¡value; ¡ value, ¡we ¡need ¡to ¡check ¡all ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ; ¡ exis/ng ¡ones ¡since ¡it ¡could ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ be ¡a ¡replacement ¡value! ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡pairs.add( new ¡ Pair(key, ¡value)); ¡ } ¡ ¡ public ¡ String ¡get(String ¡key) ¡ To ¡check ¡for ¡an ¡exis/ng ¡key, ¡ { ¡ ¡ ¡ ¡ ¡for ¡ (Pair ¡pair ¡: ¡pairs) ¡ we ¡have ¡to ¡go ¡through ¡½ ¡of ¡ ¡ ¡ ¡ ¡{ ¡ the ¡exis/ng ¡ones ¡on ¡average. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡ (pair.key.equals(key)) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡ pair.value; ¡ If ¡it ¡doesn't ¡exist ¡in ¡map, ¡we ¡ ¡ ¡ ¡ ¡} ¡ go ¡through ¡them ¡all! ¡ ¡ ¡ ¡ ¡return ¡null ; ¡ } ¡ Instance ¡variable ¡and ¡methods ¡of ¡StringToString Dumb .java ¡ 4 ¡

  5. Tes/ng ¡aFempt ¡1 ¡ • S&P ¡500 ¡data ¡ – 43K ¡data ¡points, ¡date ¡+ ¡/cker ¡symbol ¡ – Measure ¡/me ¡to ¡load ¡map ¡ – Measure ¡/me ¡to ¡lookup ¡something ¡in ¡middle ¡ ¡ • Repeat ¡10K ¡/mes ¡ 20090821,MSFT ¡23.93,24.42,23.77,24.41,690189 ¡ 20090824,MSFT ¡24.37,24.7326,24.28,24.64,541808 ¡ 20090825,MSFT ¡24.6,24.82,24.46,24.64,439802 ¡ 20090826,MSFT ¡24.56,24.75,24.42,24.55,410790 ¡ 20090827,MSFT ¡24.42,24.78,24.3,24.69,454956 ¡ 20090828,MSFT ¡25.05,25.49,24.61,24.68,558079 ¡ 20090831,MSFT ¡24.56,24.85,24.29,24.65,495971 ¡ keys ¡ values ¡ 5 ¡

  6. Tes/ng ¡aFempt ¡1 ¡ public ¡static ¡void ¡ main(String ¡[] ¡args) ¡ { ¡ ¡ ¡ ¡ ¡StringToStringDumb ¡map ¡= ¡ new ¡ StringToStringDumb(); ¡ ¡ ¡ ¡ ¡ ¡// ¡Time ¡how ¡long ¡it ¡takes ¡to ¡load ¡data ¡into ¡the ¡map ¡ ¡ ¡ ¡ ¡Stats ¡timer1 ¡= ¡ new ¡ Stats(); ¡ ¡ ¡ ¡ ¡while ¡ (!StdIn. isEmpty()) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡key ¡ ¡ ¡= ¡StdIn. readString (); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡value ¡= ¡StdIn. readString (); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡map.put(key, ¡value); ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡System. out.println ("Loading ¡data:\n" ¡+ ¡timer1); ¡ ¡ ¡ ¡ ¡ ¡// ¡Time ¡how ¡long ¡it ¡takes ¡to ¡look ¡something ¡up ¡10K ¡times ¡ % ¡java ¡StockDataDumb ¡< ¡sp500_2009.txt ¡ ¡ ¡ ¡ ¡Stats ¡timer2 ¡= ¡ new ¡ Stats(); ¡ ¡ ¡ ¡ ¡ ¡for ¡ ( int ¡ i ¡= ¡0; ¡i ¡< ¡10000; ¡i++) ¡ Loading ¡data: ¡ ¡ ¡ ¡ ¡{ ¡ elapsed ¡(s) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡: ¡63.0 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡value ¡= ¡map.get("20090827,JCP"); ¡ heap ¡memory ¡used ¡(KB) ¡: ¡39571 ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡System. out.println ("Using ¡map:\n" ¡+ ¡timer2); ¡ } ¡ Using ¡map: ¡ elapsed ¡(s) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡: ¡10.517 ¡ heap ¡memory ¡used ¡(KB) ¡: ¡39571 ¡ 6 ¡

  7. How ¡does ¡HashMap ¡actually ¡work? ¡ • Hash ¡func/on: ¡ – Maps ¡the ¡key ¡to ¡a ¡numeric ¡"bucket" ¡number ¡ – e.g. ¡"Keith ¡Vertanen" ¡→ ¡3 ¡ • Some ¡finite ¡number ¡of ¡buckets: ¡ – A ¡bucket ¡stores ¡one ¡or ¡more ¡values ¡ – Each ¡item ¡in ¡a ¡bucket ¡knows ¡its ¡full ¡key ¡ – Compare ¡contents ¡of ¡bucket ¡to ¡find ¡exact ¡target ¡ 0 ¡ 3 ¡ 1 ¡ 2 ¡ 4 ¡ 7 ¡

  8. Hash ¡func/on ¡ • Simple ¡hash ¡func/on: ¡ String ¡→ ¡ int ¡ – Loop ¡over ¡all ¡characters ¡in ¡the ¡string ¡ Warning: ¡ – Sum ¡the ¡Java ¡char ¡value ¡ This ¡is ¡a ¡poor ¡ hash ¡func/on ¡ – Mod ¡result ¡by ¡number ¡of ¡buckets ¡ private ¡int ¡ hash(String ¡str) ¡ { ¡ ¡ ¡ ¡ ¡int ¡ result ¡= ¡0; ¡ ¡ ¡ ¡ ¡for ¡ ( int ¡ i ¡= ¡0; ¡i ¡< ¡str.length(); ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡result ¡+= ¡str.charAt(i); ¡ We ¡can ¡treat ¡chars ¡as ¡numbers ¡ ¡ ¡ ¡ ¡return ¡ result ¡% ¡ BUCKETS; ¡ and ¡sum ¡their ¡values. ¡ } ¡ 0 ¡ 3 ¡ 1 ¡ 2 ¡ 4 ¡ 8 ¡

  9. My ¡own ¡map: ¡AFempt ¡2 ¡ • Goal: ¡Class ¡to ¡handle ¡mapping ¡a ¡key ¡to ¡value ¡ – Same ¡API ¡and ¡inner-­‑class: ¡ • put(String ¡key, ¡String ¡value) ¡ • get(String ¡key) ¡ – Use ¡my ¡hash ¡func/on ¡to ¡separate ¡data ¡into ¡buckets ¡ private ¡class ¡ Pair ¡ { ¡ ¡ ¡ ¡ ¡String ¡key; ¡ ¡ ¡ ¡ ¡String ¡value; ¡ ¡ ¡ ¡ ¡ ¡public ¡ Pair(String ¡key, ¡String ¡value) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡this .key ¡= ¡key; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡this .value ¡= ¡value; ¡ ¡ ¡ ¡ ¡} ¡ } ¡ 9 ¡

  10. public ¡static ¡final ¡int ¡ BUCKETS ¡= ¡256; ¡ ¡ public ¡static ¡final ¡int ¡ INIT_SIZE ¡= ¡16; ¡ 2D ¡array ¡for ¡ private ¡ Pair[][] ¡buckets= ¡ new ¡ Pair[ BUCKETS ][ INIT_SIZE ] ; ¡ storing ¡the ¡data ¡in ¡ ¡ different ¡buckets. ¡ public ¡void ¡ put(String ¡key, ¡String ¡value) ¡ { ¡ ¡ ¡ ¡ ¡int ¡ bucket ¡= ¡hash(key); ¡ Go ¡to ¡the ¡bucket ¡for ¡ ¡ ¡ ¡ ¡for ¡(int ¡ i ¡= ¡0; ¡i ¡< ¡buckets[bucket].length; ¡i++) ¡ ¡ ¡ ¡ ¡{ ¡ this ¡key, ¡then ¡check ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡ ((buckets[bucket][i] ¡== ¡ null ) ¡|| ¡ ¡ ¡ ¡ ¡ any ¡exis/ng ¡pairs ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ (buckets[bucket][i].key.equals(key))) ¡ for ¡a ¡matching ¡key. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ If ¡no ¡match, ¡add ¡a ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡buckets[bucket][i] ¡= ¡ new ¡ Pair(key, ¡value); ¡ new ¡pair. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} ¡ ¡ We ¡ran ¡out ¡of ¡room ¡in ¡our ¡ ¡ ¡ ¡ ¡int ¡ current ¡= ¡buckets[bucket].length; ¡ ini/al ¡16 ¡spots, ¡so ¡double ¡the ¡ ¡ ¡ ¡ ¡Pair ¡[] ¡copy ¡= ¡ new ¡ Pair[current ¡* ¡2]; ¡ ¡ ¡ ¡ ¡for ¡ ( int ¡ i ¡= ¡0; ¡i ¡< ¡current; ¡i++) ¡ array ¡size ¡and ¡copy ¡the ¡old ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡copy[i] ¡= ¡buckets[bucket][i]; ¡ data ¡and ¡add ¡the ¡new ¡guy. ¡ ¡ ¡ ¡ ¡copy[current] ¡= ¡ new ¡ Pair(key, ¡value); ¡ ¡ ¡ ¡ ¡buckets[bucket] ¡= ¡copy; ¡ } ¡ Instance ¡variables ¡and ¡put ¡method ¡of ¡StringToStringHash.java ¡ 10 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend