import java.io.*; import java.util.*; class song implements Comparable<song> { String title; String artist; public boolean equals(Object asong) { song s=(song) asong; return getTitle().equals(s.getTitle()); } public int hashCode() { return title.hashCode(); } song(String t,String a) { title=t; artist=a; } public String getTitle() { return title; } public String getArtist() { return artist; } public String toString() { return title+" : "+artist; }
}
public int compareTo(song s) { return title.compareTo(s.getTitle()); }
class Jutebox5 { ArrayList<song> songList=new ArrayList<song>(); public static void main(String g[]) { new Jutebox5().go(); } public void go() { getSong(); System.out.println("Original song list......\n");
}
System.out.println(songList); HashSet<song> s1=new HashSet<song>(); s1.addAll(songList); System.out.println("after sorted by hash......\n"); System.out.println(s1); Collections.sort(songList); System.out.println("song list after sorted......\n"); System.out.println(songList);
public void getSong() { try { File f=new File("SongList.txt"); BufferedReader reader=new BufferedReader(new FileReader(f)); String line=null; while((line=reader.readLine())!=null) { addSongs(line); } } catch(Exception e){} } public void addSongs(String linetoparse) { String[] tokens=linetoparse.split("/"); song s=new song(tokens[0],tokens[1]); songList.add(s); } }