Skip to content

Commit 255eaed

Browse files
committed
redefinition hashCode and equals for class Song
1 parent bdff522 commit 255eaed

6 files changed

Lines changed: 17 additions & 4 deletions

File tree

SongList_C16.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ Shiva Moon/Prem Joshua/6/120
44
Circles/BT/5/110
55
Deep Channel/Afro Celts/4/120
66
Passenger/Headmix/4/100
7-
Listen/Tahiti 80/5/90
7+
Listen/Tahiti 80/5/90
8+
Listen/Tahiti 80/5/90
9+
Listen/Tahiti 80/5/90
10+
Circles/BT/5/110
Binary file not shown.
322 Bytes
Binary file not shown.
260 Bytes
Binary file not shown.

src/CHAPTER_16/Jukebox.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.ArrayList;
77
import java.util.Collections;
88
import java.util.Comparator;
9+
import java.util.HashSet;
910

1011

1112
public class Jukebox {
@@ -22,9 +23,9 @@ public void go() {
2223
System.out.println(songList);
2324
Collections.sort(songList);
2425
System.out.println(songList);
25-
26-
ArtistCompare artistCompare = new ArtistCompare();
27-
Collections.sort(songList, artistCompare);
26+
HashSet<Song> songSet = new HashSet<Song>();
27+
songSet.addAll(songList);
28+
System.out.println(songSet);
2829
}
2930

3031
void getSongs() {

src/CHAPTER_16/Song.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ class Song implements Comparable<Song> {
66
String rating;
77
String bpm;
88

9+
public boolean equals(Object aSong){
10+
Song s = (Song) aSong;
11+
return getTitle().equals(s.getTitle());
12+
}
13+
14+
public int hashCode() {
15+
return title.hashCode();
16+
}
17+
918
Song (String t, String a, String r, String b) {
1019
title = t;
1120
artist = a;

0 commit comments

Comments
 (0)