import java.util.*; import java.io.*; class Song { String title; String artist; 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 class Jutebox2 { ArrayList<Song> songList=new ArrayList<Song>(); public static void main(String f[]) { new Jutebox1().go(); } class ArtistCompare implements Comparator<Song> { public int compare(Song one,Song two) { return(one.getArtist().compareTo(two.getArtist())); } } class TitleCompare implements Comparator<Song> { public int compare(Song one,Song two) { return(one.getTitle().compareTo(two.getTitle())); } } public void go() {
getSongs(); System.out.println(songList); System.out.println("By artist :\n"); ArtistCompare ac=new ArtistCompare(); Collections.sort(songList,ac); System.out.println(songList); System.out.println("By title :\n"); TitleCompare tc=new TitleCompare(); Collections.sort(songList,tc); System.out.println(songList);
}
//HashSet<Song> songList1=new HashSet<Song>(); //songList1.addAll(songList); //System.out.println("by using hashset.........."); //System.out.println(songList1);
void getSongs() { 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){} }
}
void addSongs(String linetoparse) { String[] tokens=linetoparse.split("/"); Song nextSong=new Song(tokens[0],tokens[1]); songList.add(nextSong); }