java - 在 Java 中使用嵌套泛型组织数据(Android)

标签 java android data-structures

我正在创建一个音乐播放器应用程序。我从名为 SongInfo 的类中的目录中检索数据,如下所示:

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,单击时显示艺术家列表 -> 打开专辑 fragment ,其中包含所选艺术家的专辑列表 -> 打开歌曲 fragment ,其中包含所选专辑的歌曲数组列表。我最终要做的是创建这种类型的数据结构:

//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; 

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

最佳答案

假设我们从这些类开始:

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 );

关于java - 在 Java 中使用嵌套泛型组织数据(Android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42473006/

相关文章:

java - 合并配置库

java - beanshell 相当于 continue

java - 尝试在 OfBiz 中保存任务时收到 FK 违规错误

android - 华为 map 不加载瓷砖

android - 不同图像的不同持续时间

c - 存储变量 ID 和值的有效方法是什么?

Java 正则表达式匹配组为空,即使它匹配

android - 启用 WiFi 热点的设备上的服务器和客户端

data-structures - 数据结构对嵌入式软件工程师的重要性

mysql - 如何在数据库中每行保存 4096 个 double