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

more on collec ons and sor ng
SMART_READER_LITE
LIVE PREVIEW

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.


slide-1
SLIDE 1

More ¡on ¡collec)ons ¡and ¡sor)ng ¡

CSCI ¡136: ¡Fundamentals ¡of ¡Computer ¡Science ¡II ¡ ¡• ¡ ¡Keith ¡Vertanen ¡

slide-2
SLIDE 2

Overview ¡

2 ¡

  • 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 ¡

slide-3
SLIDE 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: ¡

3 ¡

private ¡class ¡Pair ¡ { ¡ ¡ ¡ ¡ ¡String ¡key; ¡ ¡ ¡ ¡ ¡String ¡value; ¡ ¡ ¡ ¡ ¡ ¡public ¡Pair(String ¡key, ¡String ¡value) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡this.key ¡= ¡key; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡this.value ¡= ¡value; ¡ ¡ ¡ ¡ ¡} ¡ } ¡

slide-4
SLIDE 4

4 ¡

ArrayList<Pair> ¡pairs ¡= ¡new ¡ArrayList<Pair>(); ¡ ¡ public ¡void ¡put(String ¡key, ¡String ¡value) ¡ { ¡ ¡ ¡ ¡ ¡for ¡(Pair ¡pair ¡: ¡pairs) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(pair.key.equals(key)) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡pair.value ¡= ¡value; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡pairs.add(new ¡Pair(key, ¡value)); ¡ } ¡ ¡ public ¡String ¡get(String ¡key) ¡ { ¡ ¡ ¡ ¡ ¡for ¡(Pair ¡pair ¡: ¡pairs) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(pair.key.equals(key)) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡pair.value; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡return ¡null; ¡ } ¡

Instance ¡variable ¡and ¡methods ¡of ¡StringToStringDumb.java ¡ Store ¡the ¡pairs ¡in ¡ a ¡dynamically ¡ growing ¡array. ¡ Every ¡/me ¡we ¡put ¡a ¡new ¡ value, ¡we ¡need ¡to ¡check ¡all ¡ exis/ng ¡ones ¡since ¡it ¡could ¡ be ¡a ¡replacement ¡value! ¡ To ¡check ¡for ¡an ¡exis/ng ¡key, ¡ we ¡have ¡to ¡go ¡through ¡½ ¡of ¡ the ¡exis/ng ¡ones ¡on ¡average. ¡ ¡ ¡ If ¡it ¡doesn't ¡exist ¡in ¡map, ¡we ¡ go ¡through ¡them ¡all! ¡

slide-5
SLIDE 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 ¡

5 ¡

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 ¡

slide-6
SLIDE 6

Tes/ng ¡aFempt ¡1 ¡

6 ¡

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 ¡ ¡ ¡ ¡ ¡Stats ¡timer2 ¡= ¡new ¡Stats(); ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡10000; ¡i++) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡value ¡= ¡map.get("20090827,JCP"); ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡System.out.println("Using ¡map:\n" ¡+ ¡timer2); ¡ } ¡ % ¡java ¡StockDataDumb ¡< ¡sp500_2009.txt ¡ ¡ Loading ¡data: ¡ elapsed ¡(s) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡: ¡63.0 ¡ heap ¡memory ¡used ¡(KB) ¡: ¡39571 ¡ ¡ Using ¡map: ¡ elapsed ¡(s) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡: ¡10.517 ¡ heap ¡memory ¡used ¡(KB) ¡: ¡39571 ¡

slide-7
SLIDE 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 ¡

7 ¡

0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡

slide-8
SLIDE 8

Hash ¡func/on ¡

  • Simple ¡hash ¡func/on: ¡String ¡→ ¡int ¡

– Loop ¡over ¡all ¡characters ¡in ¡the ¡string ¡ – Sum ¡the ¡Java ¡char ¡value ¡ – Mod ¡result ¡by ¡number ¡of ¡buckets ¡

8 ¡

private ¡int ¡hash(String ¡str) ¡ { ¡ ¡ ¡ ¡ ¡int ¡result ¡= ¡0; ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡str.length(); ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡result ¡+= ¡str.charAt(i); ¡ ¡ ¡ ¡ ¡return ¡result ¡% ¡BUCKETS; ¡ } ¡

Warning: ¡ This ¡is ¡a ¡poor ¡ hash ¡func/on ¡

0 ¡ 1 ¡ 2 ¡ 3 ¡ 4 ¡

We ¡can ¡treat ¡chars ¡as ¡numbers ¡ and ¡sum ¡their ¡values. ¡

slide-9
SLIDE 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 ¡

9 ¡

private ¡class ¡Pair ¡ { ¡ ¡ ¡ ¡ ¡String ¡key; ¡ ¡ ¡ ¡ ¡String ¡value; ¡ ¡ ¡ ¡ ¡ ¡public ¡Pair(String ¡key, ¡String ¡value) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡this.key ¡= ¡key; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡this.value ¡= ¡value; ¡ ¡ ¡ ¡ ¡} ¡ } ¡

slide-10
SLIDE 10

10 ¡

public ¡static ¡final ¡int ¡BUCKETS ¡= ¡256; ¡ ¡ public ¡static ¡final ¡int ¡INIT_SIZE ¡= ¡16; ¡ private ¡Pair[][] ¡buckets= ¡new ¡Pair[BUCKETS][INIT_SIZE]; ¡ ¡ public ¡void ¡put(String ¡key, ¡String ¡value) ¡ { ¡ ¡ ¡ ¡ ¡int ¡bucket ¡= ¡hash(key); ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡buckets[bucket].length; ¡i++) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡((buckets[bucket][i] ¡== ¡null) ¡|| ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡(buckets[bucket][i].key.equals(key))) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡buckets[bucket][i] ¡= ¡new ¡Pair(key, ¡value); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡int ¡current ¡= ¡buckets[bucket].length; ¡ ¡ ¡ ¡ ¡Pair ¡[] ¡copy ¡= ¡new ¡Pair[current ¡* ¡2]; ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡current; ¡i++) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡copy[i] ¡= ¡buckets[bucket][i]; ¡ ¡ ¡ ¡ ¡copy[current] ¡= ¡new ¡Pair(key, ¡value); ¡ ¡ ¡ ¡ ¡buckets[bucket] ¡= ¡copy; ¡ } ¡

Instance ¡variables ¡and ¡put ¡method ¡of ¡StringToStringHash.java ¡ 2D ¡array ¡for ¡ storing ¡the ¡data ¡in ¡ different ¡buckets. ¡ Go ¡to ¡the ¡bucket ¡for ¡ this ¡key, ¡then ¡check ¡ any ¡exis/ng ¡pairs ¡ for ¡a ¡matching ¡key. ¡ ¡ If ¡no ¡match, ¡add ¡a ¡ new ¡pair. ¡ We ¡ran ¡out ¡of ¡room ¡in ¡our ¡ ini/al ¡16 ¡spots, ¡so ¡double ¡the ¡ array ¡size ¡and ¡copy ¡the ¡old ¡ data ¡and ¡add ¡the ¡new ¡guy. ¡

slide-11
SLIDE 11

11 ¡

public ¡String ¡get(String ¡key) ¡ { ¡ ¡ ¡ ¡ ¡int ¡bucket ¡= ¡hash(key); ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡buckets[bucket].length; ¡i++) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(buckets[bucket][i] ¡== ¡null) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡null; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(buckets[bucket][i].key.equals(key)) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡buckets[bucket][i].value; ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡return ¡null; ¡ } ¡

get ¡method ¡of ¡StringToStringHash.java ¡ Go ¡to ¡the ¡bucket ¡ dictated ¡by ¡our ¡key. ¡ ¡ Then ¡look ¡for ¡a ¡pair ¡ with ¡a ¡matching ¡

  • key. ¡ ¡If ¡we ¡don't ¡

find ¡one, ¡we ¡know ¡ the ¡key ¡isn't ¡in ¡the ¡

  • map. ¡
slide-12
SLIDE 12

Tes/ng ¡aFempt ¡2 ¡

12 ¡

public ¡static ¡void ¡main(String ¡[] ¡args) ¡ { ¡ ¡ ¡ ¡ ¡StringToStringHash ¡map ¡= ¡new ¡StringToStringHash(); ¡ ¡ ¡ ¡ ¡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); ¡ ¡ ¡ ¡ ¡ ¡Stats ¡timer2 ¡= ¡new ¡Stats(); ¡ ¡ ¡ ¡ ¡for ¡(int ¡i ¡= ¡0; ¡i ¡< ¡10000; ¡i++) ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡value ¡= ¡map.get("20090827,JCP"); ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡System.out.println("Using ¡map:\n" ¡+ ¡timer2); ¡ } ¡

% ¡java ¡StockDataHash ¡< ¡sp500_2009.txt ¡ ¡ Loading ¡data: ¡ elapsed ¡(s) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡: ¡0.861 ¡ heap ¡memory ¡used ¡(KB) ¡: ¡38017 ¡ ¡ Using ¡map: ¡ elapsed ¡(s) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡: ¡0.086 ¡ heap ¡memory ¡used ¡(KB) ¡: ¡38017 ¡ % ¡java ¡StockDataDumb ¡< ¡sp500_2009.txt ¡ ¡ Loading ¡data: ¡ elapsed ¡(s) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡: ¡63.0 ¡ heap ¡memory ¡used ¡(KB) ¡: ¡39571 ¡ ¡ Using ¡map: ¡ elapsed ¡(s) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡: ¡10.517 ¡ heap ¡memory ¡used ¡(KB) ¡: ¡39571 ¡ % ¡java ¡StockDataHashMap ¡< ¡sp500_2009.txt ¡ ¡ Loading ¡data: ¡ elapsed ¡(s) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡: ¡0.276 ¡ heap ¡memory ¡used ¡(KB) ¡: ¡38017 ¡ ¡ Using ¡map: ¡ elapsed ¡(s) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡: ¡0.0050 ¡ heap ¡memory ¡used ¡(KB) ¡: ¡38017 ¡

Java’s ¡HashMap: ¡ The ¡Winner! ¡ My ¡class: ¡ ¡ (bad) ¡hash ¡func/on ¡ My ¡class: ¡ ¡ no ¡hashing ¡

slide-13
SLIDE 13

Java ¡collec/ons ¡API ¡

  • Collec/ons ¡API: ¡

– Useful ¡classes ¡for ¡storing ¡stuff ¡ – List ¡

  • Knows ¡about ¡the ¡order ¡of ¡things ¡

¡

– Set ¡

  • Knows ¡if ¡something ¡is ¡in ¡the ¡set ¡

¡

– Map ¡

  • Knows ¡how ¡to ¡get ¡from ¡key ¡to ¡value ¡
  • Mul/ple ¡keys ¡can ¡go ¡to ¡same ¡value ¡

13 ¡

slide-14
SLIDE 14

14 ¡

slide-15
SLIDE 15

HashSet: ¡storing ¡a ¡set ¡of ¡values ¡

  • Common ¡problem: ¡check ¡if ¡something ¡is ¡in ¡a ¡set ¡

– Only ¡need ¡to ¡know ¡if ¡key ¡present ¡or ¡not ¡ – Java ¡HashSet, ¡no ¡duplicates

  • Like ¡a ¡HashMap ¡but ¡with ¡only ¡has ¡keys, ¡no ¡values! ¡

15 ¡

public ¡class ¡HelloHashSet ¡ ¡ { ¡ ¡ ¡ ¡public ¡static ¡void ¡main(String ¡[] ¡args) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡HashSet<String> ¡set ¡= ¡new ¡HashSet<String>(); ¡

¡

¡ ¡ ¡ ¡ ¡ ¡set.add("carrot"); ¡ ¡ ¡ ¡ ¡ ¡ ¡set.add("apple"); ¡ ¡ ¡ ¡ ¡ ¡ ¡set.add("carrot"); ¡

¡

¡ ¡ ¡ ¡ ¡ ¡System.out.println("set ¡size ¡= ¡" ¡+ ¡set.size()); ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(set.contains("apple")) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println("I ¡spy ¡a ¡fruit!"); ¡ ¡ ¡ ¡} ¡ } ¡ % ¡java ¡HelloHashSet ¡ set ¡size ¡= ¡2 ¡ I ¡spy ¡a ¡fruit! ¡

slide-16
SLIDE 16

HashSet ¡example ¡1 ¡

16 ¡

  • Goal: ¡Print ¡all ¡unique ¡words ¡in ¡a ¡file ¡

HashSet<String> ¡dict ¡= ¡new ¡HashSet<String>(); ¡ ¡ try ¡ { ¡ ¡ ¡ ¡Scanner ¡scan ¡= ¡new ¡Scanner(new ¡File(args[0])); ¡ ¡ ¡ ¡while ¡(scan.hasNext()) ¡ ¡ ¡ ¡ ¡ ¡ ¡dict.add(scan.next().toLowerCase()); ¡ ¡ ¡ ¡scan.close(); ¡ } ¡ catch ¡(FileNotFoundException ¡e) ¡ { ¡ ¡ ¡ ¡e.printStackTrace(); ¡ ¡ ¡ ¡return; ¡ } ¡ ¡ for ¡(String ¡s ¡: ¡dict) ¡ ¡ ¡ ¡System.out.println(s); ¡

Read ¡in ¡ the ¡words ¡ from ¡the ¡

  • file. ¡

Itera/ng ¡over ¡all ¡the ¡elements ¡ and ¡print ¡them ¡out. ¡ Crea/ng ¡the ¡set, ¡

  • nly ¡needs ¡a ¡single ¡

reference ¡type. ¡

slide-17
SLIDE 17

HashSet ¡example ¡2 ¡

17 ¡

  • Goal: ¡Spell ¡check ¡a ¡file, ¡flag ¡words ¡not ¡in ¡dic/onary ¡

HashSet<String> ¡dict ¡= ¡new ¡HashSet<String>(); ¡ try ¡ { ¡ ¡ ¡ ¡Scanner ¡scan ¡= ¡new ¡Scanner(new ¡File(args[0])); ¡ ¡ ¡ ¡while ¡(scan.hasNext()) ¡ ¡ ¡ ¡ ¡ ¡ ¡dict.add(scan.next().toLowerCase()); ¡ ¡ ¡ ¡scan.close(); ¡ ¡ ¡ ¡ ¡scan ¡= ¡new ¡Scanner(new ¡File(args[1])); ¡ ¡ ¡ ¡while ¡(scan.hasNext()) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡word ¡= ¡scan.next() ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡.toLowerCase() ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡.replaceAll("[^a-­‑z']", ¡""); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(!dict.contains(word)) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println(word); ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡scan.close(); ¡ } ¡ catch ¡(FileNotFoundException ¡e) ¡ { ¡ ¡ ¡ ¡e.printStackTrace(); ¡ } ¡

Read ¡in ¡from ¡first ¡file: ¡Load ¡ dic/onary ¡of ¡words ¡ Read ¡in ¡from ¡second ¡file: ¡ ¡ Check ¡for ¡words ¡no ¡in ¡dic/onary ¡

¡

(convert ¡all ¡words ¡to ¡lower ¡case, ¡ strip ¡any ¡character ¡except ¡for ¡a-­‑z ¡ and ¡apostrophe) ¡

slide-18
SLIDE 18

HashSet, ¡important ¡methods ¡

18 ¡

Method ¡ Descrip)on ¡

add(Object ¡o) ¡

Add ¡new ¡element ¡o ¡to ¡the ¡set. ¡ Returns ¡true ¡if ¡element ¡o ¡was ¡added ¡(not ¡already ¡in ¡ the ¡set), ¡returns ¡false ¡if ¡set ¡already ¡contains ¡o. ¡

size() ¡

Return ¡number ¡of ¡items ¡in ¡the ¡set. ¡

contains(Object ¡o) ¡

Returns ¡true ¡if ¡the ¡set ¡contains ¡element ¡o, ¡ ¡

false ¡otherwise. clear() ¡

Remove ¡all ¡the ¡elements ¡from ¡the ¡set. ¡

remove(Object ¡o) ¡

Removes ¡element ¡o ¡from ¡the ¡set ¡if ¡present. ¡ Returns ¡true ¡if ¡something ¡removed, ¡false ¡otherwise. ¡

slide-19
SLIDE 19

Sor/ng ¡

  • Omen ¡we ¡want ¡things ¡in ¡a ¡certain ¡order ¡

– e.g. ¡Print ¡all ¡unique ¡words ¡in ¡a ¡file ¡in ¡alphabe/cal ¡order ¡ – e.g. ¡Display ¡all ¡Word ¡objects ¡in ¡order ¡of ¡probability ¡ – e.g. ¡Sort ¡all ¡the ¡cards ¡in ¡your ¡hand ¡

19 ¡

slide-20
SLIDE 20

Sor/ng ¡cards ¡

20 ¡

1: ¡Aces ¡high, ¡diamonds ¡> ¡hearts: ¡ 2: ¡Aces ¡high, ¡hearts ¡> ¡diamonds: ¡ 3: ¡Aces ¡low, ¡diamonds ¡> ¡hearts: ¡ 4: ¡2 ¡biggest, ¡spade ¡> ¡hearts ¡> ¡clubs ¡> ¡diamonds ¡

We ¡need ¡to ¡define ¡what ¡we ¡ mean ¡by ¡"sorted". ¡

slide-21
SLIDE 21

How ¡to ¡sort ¡

  • A ¡large ¡variety ¡of ¡sor/ng ¡algorithms: ¡

– Bubble ¡sort ¡ – Inser/on ¡sort ¡ – Selec/on ¡sort ¡ – Quick ¡sort ¡ – Merge ¡sort ¡ – Stupid ¡sort ¡(aka ¡bogosort, ¡slowstart), ¡bogobogosort ¡ – … ¡

  • Eventually ¡you'll ¡learn ¡about ¡some ¡of ¡these ¡

– Today: ¡sort ¡using ¡built-­‑in ¡Java ¡Collec/ons ¡API ¡ – Works ¡great ¡for ¡List ¡ADTs ¡like ¡ArrayList ¡ – Java ¡uses ¡an ¡op/mized ¡version ¡of ¡merge ¡sort ¡

21 ¡

slide-22
SLIDE 22

Sor/ng ¡an ¡ArrayList ¡

22 ¡

public ¡static ¡void ¡main(String ¡[] ¡args) ¡ { ¡ ¡ ¡ ¡ArrayList<String> ¡words ¡= ¡new ¡ArrayList<String>(); ¡ ¡ ¡ ¡try ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡Scanner ¡scan ¡= ¡new ¡Scanner(new ¡File(args[0])); ¡ ¡ ¡ ¡ ¡ ¡ ¡while ¡(scan.hasNext()) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡words.add(scan.next()); ¡ ¡ ¡ ¡ ¡ ¡ ¡scan.close(); ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡catch ¡(FileNotFoundException ¡e) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡e.printStackTrace(); ¡ ¡ ¡ ¡ ¡ ¡ ¡return; ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡Collections.sort(words); ¡ ¡ ¡ ¡ ¡for ¡(String ¡s ¡: ¡words) ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println(s); ¡ } ¡

Read ¡in ¡ the ¡words ¡ from ¡the ¡

  • file. ¡

Sort ¡the ¡ArrayList ¡ so ¡it ¡is ¡in ¡ alphabe/cal ¡order ¡

% ¡java ¡WordsSorted ¡ wiki10.txt ¡ a ¡ a ¡ a ¡ a ¡ a ¡ ailment ¡ an ¡ and ¡ and ¡ and ¡ and ¡ and ¡ appointed ¡

slide-23
SLIDE 23

Sor/ng ¡Word ¡objects ¡

  • Goal: ¡Print ¡Word ¡objects: ¡most ¡probable ¡first ¡

– Read ¡from ¡file ¡into ¡ArrayList<Word> ¡object ¡ – Sort ¡the ¡ArrayList ¡ – Iterate ¡through ¡list, ¡prin/ng ¡each ¡entry ¡

23 ¡

and ¡ ¡ ¡ ¡ ¡0.0148 ¡ is ¡ ¡ ¡ ¡ ¡ ¡0.00987 ¡ in ¡ ¡ ¡ ¡ ¡ ¡0.00904 ¡ to ¡ ¡ ¡ ¡ ¡ ¡0.00791 ¡ was ¡ ¡ ¡ ¡ ¡0.00676 ¡

  • f ¡ ¡ ¡ ¡ ¡ ¡0.00648 ¡

for ¡ ¡ ¡ ¡ ¡0.00535 ¡ the ¡ ¡ ¡ ¡ ¡0.00535 ¡

  • r ¡ ¡ ¡ ¡ ¡ ¡0.00527 ¡

that ¡ ¡ ¡ ¡0.00494 ¡ ... ¡

slide-24
SLIDE 24

Sor/ng ¡a ¡Word ¡list: ¡failure ¡

24 ¡

ArrayList<Word> ¡entries ¡= ¡new ¡ArrayList<Word>(); ¡

¡

try ¡ { ¡ ¡ ¡ ¡Scanner ¡scan ¡= ¡new ¡Scanner(new ¡File(args[0])); ¡ ¡ ¡ ¡while ¡(scan.hasNext()) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡String ¡word ¡= ¡scan.next(); ¡ ¡ ¡ ¡ ¡ ¡ ¡double ¡prob ¡= ¡scan.nextDouble(); ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(word.length() ¡> ¡0) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡entries.add(new ¡Word(word, ¡prob)); ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡scan.close(); ¡ } ¡ catch ¡(FileNotFoundException ¡e) ¡ { ¡ ¡ ¡ ¡e.printStackTrace(); ¡ ¡ ¡ ¡return; ¡ } ¡

¡

Collections.sort(entries); ¡

¡

for ¡(Word ¡e ¡: ¡entries) ¡ ¡ ¡ ¡System.out.println(e); ¡

% ¡javac ¡SortWord.java ¡ SortWord.java:32: ¡error: ¡no ¡suitable ¡method ¡found ¡for ¡sort(ArrayList<Word>) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡Collections.sort(entries); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡^ ¡ ¡ ¡ ¡ ¡method ¡Collections.<T#1>sort(List<T#1>) ¡is ¡not ¡applicable ¡ ¡ ¡ ¡ ¡ ¡ ¡(inference ¡variable ¡T#1 ¡has ¡incompatible ¡bounds ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡equality ¡constraints: ¡Word ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡upper ¡bounds: ¡Comparable<? ¡super ¡T#1>) ¡ ¡ ¡ ¡ ¡method ¡Collections.<T#2>sort(List<T#2>,Comparator<? ¡super ¡T#2>) ¡is ¡not ¡applicable ¡ ¡ ¡ ¡ ¡ ¡ ¡(cannot ¡infer ¡type-­‑variable(s) ¡T#2 ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡(actual ¡and ¡formal ¡argument ¡lists ¡differ ¡in ¡length)) ¡ ¡ ¡where ¡T#1,T#2 ¡are ¡type-­‑variables: ¡ ¡ ¡ ¡ ¡T#1 ¡extends ¡Comparable<? ¡super ¡T#1> ¡declared ¡in ¡method ¡<T#1>sort(List<T#1>) ¡ ¡ ¡ ¡ ¡T#2 ¡extends ¡Object ¡declared ¡in ¡method ¡<T#2>sort(List<T#2>,Comparator<? ¡super ¡T#2>) ¡ 1 ¡error ¡

slide-25
SLIDE 25

Making ¡objects ¡sortable ¡

  • In ¡order ¡to ¡use ¡Collection.sort(): ¡

– Reference ¡type ¡stored ¡in ¡the ¡ArrayList ¡must ¡ "implements ¡Comparable" ¡

  • i.e. ¡Class ¡must ¡have ¡a ¡compareTo() ¡method ¡

25 ¡

slide-26
SLIDE 26

Words ¡in ¡ascending ¡order ¡

26 ¡

public ¡class ¡Word ¡implements ¡Comparable<Word> ¡ { ¡ ¡ ¡ ¡ ¡public ¡int ¡compareTo(Word ¡other) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(getProbability() ¡> ¡other.getProbability()) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡if ¡(getProbability() ¡< ¡other.getProbability()) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡-­‑1; ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡-­‑0; ¡ ¡ ¡ ¡} ¡ ... ¡

% ¡java ¡SortWord ¡entries.txt ¡ moore ¡(prob ¡0.000058 ¡audio ¡'') ¡ bull ¡(prob ¡0.000058 ¡audio ¡'') ¡ craft ¡(prob ¡0.000058 ¡audio ¡'') ¡ periodically ¡(prob ¡0.000058 ¡audio ¡'') ¡ founder ¡(prob ¡0.000058 ¡audio ¡'') ¡ nick ¡(prob ¡0.000058 ¡audio ¡'') ¡ sanctions ¡(prob ¡0.000058 ¡audio ¡'') ¡ manufactured ¡(prob ¡0.000058 ¡audio ¡'') ¡ drill ¡(prob ¡0.000058 ¡audio ¡'') ¡ ... ¡

But ¡we ¡wanted ¡ the ¡most ¡ probable ¡first! ¡

slide-27
SLIDE 27

Words ¡in ¡descending ¡order ¡

27 ¡

public ¡class ¡Word ¡implements ¡Comparable<Word> ¡ { ¡ ¡ ¡ ¡ ¡public ¡int ¡compareTo(Word ¡other) ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(getProbability() ¡> ¡other.getProbability()) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡-­‑1; ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡if ¡(getProbability() ¡< ¡other.getProbability()) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡1; ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡0; ¡ ¡ ¡ ¡} ¡ ... ¡

% ¡java ¡SortWord ¡entries.txt ¡ and ¡(prob ¡0.014800 ¡audio ¡'') ¡ is ¡(prob ¡0.009870 ¡audio ¡'') ¡ in ¡(prob ¡0.009040 ¡audio ¡'') ¡ to ¡(prob ¡0.007910 ¡audio ¡'') ¡ was ¡(prob ¡0.006760 ¡audio ¡'') ¡

  • f ¡(prob ¡0.006480 ¡audio ¡'') ¡

for ¡(prob ¡0.005350 ¡audio ¡'') ¡ the ¡(prob ¡0.005350 ¡audio ¡'') ¡

  • r ¡(prob ¡0.005270 ¡audio ¡'') ¡

... ¡

slide-28
SLIDE 28

Summary ¡

  • Java ¡Collec/ons ¡API ¡

– Represent ¡lists, ¡maps, ¡and ¡sets: ¡

  • ArrayList<type1> ¡
  • HashMap<type1, ¡type2> ¡
  • HashSet<type1> ¡

– Provides ¡ability ¡to ¡sort ¡objects ¡in ¡a ¡collec/on ¡

  • Built-­‑in ¡reference ¡types ¡work ¡out-­‑of-­‑the-­‑box ¡

– String, ¡Integer, ¡Double, ¡... ¡

  • You ¡own ¡data ¡types: ¡

– You ¡must ¡implement ¡the ¡Comparable ¡interface ¡ – Add ¡a ¡compareTo() ¡method ¡that ¡returns ¡-­‑1, ¡0, ¡1 ¡

28 ¡