CSCI-142 Computer Science 2 Recitationcsci142/Recitations/05/05-JCF1.pdf5 TreeSet An auto-balancing...
Transcript of CSCI-142 Computer Science 2 Recitationcsci142/Recitations/05/05-JCF1.pdf5 TreeSet An auto-balancing...
CSCI-142 Computer Science 2
Recitation
05Java Collection Framework (Part 1)
2
Game Collector: UML
3
ArrayList<E>
this.gameList = new ArrayList<>();this.gameList.add(new Game("Settlers of Catan", 1995, 34.99, Game.Type.BOARD));this.gameList.add(new Game("Fortnite", 2017, 0.00, Game.Type.VIDEO));this.gameList.add(new Game("Magic The Gathering", 1993, 33.55, Game.Type.CARD));this.gameList.add(new Game("World of Warcraft", 2004, 49.99, Game.Type.VIDEO));this.gameList.add(new Game("World of Warcraft", 2005, 39.99, Game.Type.BOARD));this.gameList.add(new Game("Halo 2", 2004, 59.99, Game.Type.VIDEO));
name→"..Catan"year→1995cost→34.99type→BOARD
name→"Fortnite"year→2017Cost→0.00type→VIDEO
name→"Magic.."year→1993Cost→33.55type→CARD
name→"Wo..W"year→2004cost→49.99type→VIDEO
name→"Wo..W"year→2005Cost→39.99type→BOARD
name→"Halo 2"year→2004Cost→59.99type→VIDEO
gameList→
● A contiguous collection of elements:– add (end), get: O(1)
– add (front), remove(0): O(N)
4
ArrayList: contains() ↔ Game: equals()
Game fortnite = new Game("Fortnite", 2017, 0, Game.Type.VIDEO);this.gameList.contains(fortnite); // true
@Overridepublic boolean equals(Object other) { boolean result = false; if (other instanceof Game) { Game otherGame = (Game) other; result = this.name.equals(otherGame.name) && this.year == otherGame.year && this.type == otherGame.type; } return result;}
Game
name→"..Catan"year→1995cost→34.99type→BOARD
name→"Fortnite"year→2017cost→0.00type→VIDEO
name→"Magic.."year→1993cost→33.55type→CARD
name→"Wo..W"year→2004cost→49.99type→VIDEO
name→"Wo..W"year→2005cost→39.99type→BOARD
name→"Halo 2"year→2004cost→59.99type→VIDEO
gameList→
name→"Fortnite"year→2017cost→0.00type→VIDEO
name→"Fortnite"year→2017cost→0.00type→VIDEO
←this
otherGame
5
TreeSet<E>
● An auto-balancing red-black tree
– Elements are ordered and must be unique
– add, remove, contains: O(logN)Set<Game> gameTree = new TreeSet<>(this.gameList);
name→"Wo..W"year→2004cost→49.99type→VIDEO
name→"..Catan"year→1995cost→34.99type→BOARD
name→"Magic.."year→1993Cost→33.55type→CARD
name→"Halo 2"year→2004cost→59.99type→VIDEO
name→"Wo..W"year→2005cost→39.99type→BOARD
name→"Fortnite"year→2017Cost→0.00type→VIDEO
gameTree→
* not the actual treerepresentation
6
Comparable<E>: Natural Order Comparison
public class Game implements Comparable<Game> { ... @Override public int compareTo(Game other) { ... }
● Three different return values• < 0: this is less than other
• == 0: this and other are equal
• > 0: this is greater than other
7
Game’s compareTo
@Overridepublic int compareTo(Game other) { int result = this.year - other.year; if (result == 0) { result = this.name.compareTo(other.name); } return result;}
● Comparison order:– First: ascending by year
– Second: alphabetically by name
0 is Game{name='Magic The Gathering', year=1993, cost=33.55, type=CARD}1 is Game{name='Settlers of Catan', year=1995, cost=34.99, type=BOARD}2 is Game{name='Halo 2', year=2004, cost=59.99, type=VIDEO} passed!3 is Game{name='World of Warcraft', year=2004, cost=49.99, type=VIDEO} passed!4 is Game{name='World of Warcraft', year=2005, cost=39.99, type=BOARD} passed!5 is Game{name='Fortnite', year=2017, cost=0.0, type=VIDEO} passed!
Output
8
TreeSet<E>: contains ↔ Game: compareTo
name→"Wo..W"year→2004cost→49.99type→VIDEO
name→"..Catan"year→1995cost→34.99type→BOARD
name→"Halo 2"year→2004cost→59.99type→VIDEO
name→"Wo..W"year→2005cost→39.99type→BOARD
name→"Fortnite"year→2017Cost→0.00type→VIDEO
← gameTree
Game halo = new Game("Halo 2", 2004, 59.99, Game.Type.VIDEO));gameTree.contains(halo); // true
name→"Halo 2"year→2004cost→59.99type→VIDEO
this → other →
other →
other → name→"Magic.."year→1993Cost→33.55type→CARD
this < other
this == other
this > other
9
Comparator<E>: GameComparator
public class GameComparator implements Comparator<Game> { ... @Override public int compare(Game first, Game second) { ... }
● Using a Comparator allows you to override the natural order comparison of the elements in a tree– It is not a member of the Game class
– The return result is the same idea as with Comparable’s compareTo()
– Must be passed in when the tree is created
10
GameComparator
public int compare(Game first, Game second) { int result = first.getType().compareTo(second.getType()); if (result == 0) { result = Double.compare(second.getCost(), first.getCost()); } return result; }}
● Comparison order:– First: ascending by type (as listed in enum)
– Second: decreasing by cost
0 Game{name='Magic The Gathering', year=1993, cost=33.55, type=CARD}1 Game{name='World of Warcraft', year=2005, cost=39.99, type=BOARD}2 Game{name='Settlers of Catan', year=1995, cost=34.99, type=BOARD}3 Game{name='Halo 2', year=2004, cost=59.99, type=VIDEO}4 Game{name='World of Warcraft', year=2004, cost=49.99, type=VIDEO}5 Game{name='Fortnite', year=2017, cost=0.0, type=VIDEO}
Output
11
TreeSet<E>: contains ↔ GameComparator: compare
name→"Wo..W"year→2004cost→49.99type→VIDEO
name→"..Catan"year→1995cost→34.99type→BOARD
name→"Magic.."year→1993Cost→33.55type→CARD
name→"Halo 2"year→2004cost→59.99type→VIDEO
name→"Wo..W"year→2005cost→39.99type→BOARD
name→"Fortnite"year→2017Cost→0.00type→VIDEO
gameTree→
Set<Game> gameTree = new TreeSet<>(new GameComparator());gameTree.addAll(this.gameList);Game halo = new Game("Halo 2", 2004, 59.99, Game.Type.VIDEO));gameTree.contains(halo); // true
this < other
this == other
this > other
name→"Halo 2"year→2004cost→59.99type→VIDEO
this → ← other
← other
← other
12
HashSet<E>
● Implemented as a hash table– Unique keys/elements (no values)– put, get, contains: O(1)
name→"..Catan"year→1995cost→34.99type→BOARD
name→"Fortnite"year→2017Cost→0.00type→VIDEO
name→"Magic.."year→1993Cost→33.55type→CARD
name→"Wo..W"year→2004cost→49.99type→VIDEO
name→"Wo..W"year→2005Cost→39.99type→BOARD
name→"Halo 2"year→2004Cost→59.99type→VIDEO
null nulltable->
0 11 2 3 44 5
* shown here using chaining but is usually implemented with open addressing
13
Overriding Object’s hashCode
public class Game implements Comparable<Game> { ... @Override public int hashCode() { return this.name.hashCode() + this.year + this.type.hashCode(); }
● By default Object’s hashCode returns the memory address of the object• Two objects that are equal must return
the same hash code
14
HashSet<E> contains ↔ Game’s hashCode
name→"..Catan"year→1995cost→34.99type→BOARD
name→"Fortnite"year→2017Cost→0.00type→VIDEO
name→"Wo..W"year→2004cost→49.99type→VIDEO
name→"Halo 2"year→2004Cost→59.99type→VIDEO
nulltable->
2 3 44 5
Set<Game> gameHash = new HashSet<>(this.gameList);Game halo = new Game("Halo 2", 2004, 59.99, Game.Type.VIDEO));gameHash.contains(halo); // true
...
halo.hashCode() % table.size()
← halo.equals()? false
← halo.equals()? true
15
HashMap<K,V> & TreeMap<K,V>
● HashMap<K,V>
– Keys are unique and hashed, any value
– O(1) access
● TreeMap<K,V>
– Keys are unique and ordered, any value
– O(logN) access
16
HashMap<K,V> & TreeMap<K,V>
Map<Game, Integer> playedMap = new HashMap<>();playedMap.put(this.catanBoard, 100);playedMap.put(this.fortniteVideo, 5000000);playedMap.put(this.magicCard, 600);playedMap.put(this.wowVideo, 9000);playedMap.put(this.haloVideo, 15000);playedMap.put(this.wowBoard, 5);playedMap.put(this.fortniteVideo, playedMap.get(this.fortniteVideo) + 5);
Map<Integer, Game> timesMap = new TreeMap<>();for (Map.Entry<Game, Integer> entry : playedMap.entrySet()) { timesMap.put(entry.getValue(), entry.getKey());}
for (Map.Entry<Integer, Game> entry : timesMap.entrySet()) { System.out.println(entry);}
5=Game{name='World of Warcraft', year=2005, cost=39.99, type=BOARD}100=Game{name='Settlers of Catan', year=1995, cost=34.99, type=BOARD}600=Game{name='Magic The Gathering', year=1993, cost=33.55, type=CARD}9000=Game{name='World of Warcraft', year=2004, cost=49.99, type=VIDEO}15000=Game{name='Halo 2', year=2004, cost=59.99, type=VIDEO}5000005=Game{name='Fortnite', year=2017, cost=0.0, type=VIDEO}
Output
17
Presentation Code
● Game Collector:– https://www.cs.rit.edu/~csci142/Recitations/05/Code/Game.java
– https://www.cs.rit.edu/~csci142/Recitations/05/Code/GameCollector.java
– https://www.cs.rit.edu/~csci142/Recitations/05/Code/GameComparator.java