2017-02-26 43 views
0

我正在创建音乐播放器应用程序。我检索目录中的数据在一个名为SongInfo类看起来是这样的:使用Java中的嵌套泛型组织数据(Android)

public class SongInfo { 

//id of song 
private long _ID; 

//song name 
private String title; 

//artist name 
private String artistName; 

//album name 
private String albumName; 

//song duration 
long duration; 

//year of release 
private String year; 

//constructor 

//comparators to sort data 

我的应用程序具有这样的结构有一个

ArtistsFragment上显示点击艺术家列表 - >它打开具有所选艺术家的专辑列表的专辑片段 - >打开具有所选专辑的歌曲的ArrayList的歌曲片段。我最后做的反而是创建这种类型的数据结构:

//contains most of the info of the song 
ArrayList<SongInfo> songs; 

//contains an arrayList of songs 
ArrayList<ArrayList<SongInfo>> albums; 

//contains an arraylist of albums  
Arraylist<ArrayList<ArrayList<SongInfo>>> artists; 

这我非常肯定是错误的方式做到这一点,因为如果我有什么更多的层?我正在寻找更好的方法来做到这一点。我只需要一个提示,但是插图/解释会很棒。谢谢:)

+0

“器官将这些数据分成3个不同的片段,例如:艺术家列表 - >所选艺术家的专辑 - >所选专辑的歌曲。“如果你能够在一个句子的片段中解释你在想什么奇怪的想法给许多不知名的人,这不是很好吗?但我恐怕这只对你有意义。 –

+0

@MikeNakis我添加了更多信息,希望它有道理。请让我知道你是否有任何其他怀疑理解这个问题。 – Shubham

+0

创建一个'歌曲'类,它有一个'List '(名为'infos'?)和一个'List'类,它有一个'List '(名为'songs'?)。 – 2017-02-26 19:46:15

回答

1

假设我们要实现这些类:

public static class Song 
{ 
    String title; 
    Artist artist; 
    Album album; 
    Song(String title, Artist artist, Album album) 
     { this.title = title; this.artist = artist; this.album = album; } 
    @Override public String toString() { return "Song: " + title; } 
} 

public static class Artist 
{ 
    String name; 
    Artist(String name) { this.name = name; } 
    @Override public String toString() { return "Artist: " + name; } 
} 

public static class Album 
{ 
    String name; 
    Album(String name) { this.name = name; } 
    @Override public String toString() { return "Album: " + name; } 
} 

其中:

  • Song就是你现在用相当不幸的名称SongInfo
  • Artist调用是一个类,你还没有,但你应该。如果你不能处理这个问题,那么只要你看到Artist就想到一个String,里面有一个艺术家的名字。
  • Album是另一类,你还没有,但你应该。如果您无法处理该问题,那么无论何时您看到Album只需考虑一个String并附上相册的标题即可。

并假设我们开始与这个数据集:

Artist artist1 = new Artist("Artist1"); 
    Artist artist2 = new Artist("Artist2"); 
    Set<Artist> artists = new HashSet<>(Arrays.asList(artist1, artist2)); 
    Album album1 = new Album("Album1"); 
    Album album2 = new Album("Album2"); 
    Set<Album> albums = new HashSet<>(Arrays.asList(album1, album2)); 
    Song song11a = new Song("Song11a", artist1, album1); 
    Song song11b = new Song("Song11b", artist1, album1); 
    Song song22a = new Song("Song22a", artist2, album2); 
    Song song22b = new Song("Song22b", artist2, album2); 
    List<Song> songs = Arrays.asList(song11a, song11b, song22a, song22b); 

那么下面会给你想要的东西:

Collection<Song> getSongsByArtist(Collection<Song> songs, Artist artist) 
{ 
    return songs.stream().filter(song -> song.artist == artist) 
     .collect(Collectors.toList()); 
} 

Collection<Album> getAlbumsByArtist(Collection<Song> songs, Artist artist) 
{ 
    return songs.stream().filter(song -> song.artist == artist) 
     .map(song -> song.album).collect(Collectors.toSet()); 
} 

和下面会给你你一直想知道的一切关于您的数据集,但不敢问:

Map<Song,Artist> artistsBySong = songs.stream() 
    .collect(Collectors.toMap(Function.identity(), song -> song.artist)); 
Map<Song,Album> albumsBySong = songs.stream() 
    .collect(Collectors.toMap(Function.identity(), song -> song.album)); 
Map<Artist,List<Song>> songsByArtist = songs.stream() 
    .collect(Collectors.groupingBy(song -> song.artist)); 
Map<Album,List<Song>> songsByAlbum = songs.stream() 
    .collect(Collectors.groupingBy(song -> song.album)); 
assert songs.stream().map(song -> song.album) 
    .collect(Collectors.toSet()).equals(albums); 
assert songsByAlbum.keySet().equals(albums);