对象+列表中的对象列表?

时间:2019-02-03 11:32:42

标签: java list

我有一个有趣且麻烦的任务要解决。 我需要创建一个包含歌曲和其他子播放列表的播放列表(某种列表)...每个播放列表都有一个播放模式(随机,顺序等)。可以创建这样的播放列表吗? 我曾想过要破解子播放列表,然后将多余的歌曲添加到主播放列表,或者为添加到主播放列表的每首歌曲创建一个子播放列表(我不太喜欢这个想法) 它以某种方式解决了问题,但是有必要保持每个播放列表的播放模式...

例如:

主播放列表(音序模式)具有: (1)歌曲1-1 / (2)具有随机播放模式的子播放列表(歌曲2-1,歌曲2-2,歌曲2-3)/ (3)song1-2

理想的结果: (1)歌曲1-1 / (2)song2-3(开始随机子播放列表)/ (3)歌曲2-1 / (4)歌曲2-2 / (5)song1-2 /

我应该如何处理?

3 个答案:

答案 0 :(得分:3)

由于我怀疑这是某种家庭作业,因此我仅向您提供部分实现,因此您了解如何进行操作。

创建一个抽象类PlaylistElement,该抽象类以后可以是Song或另一个Playlist

abstract class PlaylistElement {
    public abstract List<Song> printSongs();
}

实施扩展Playlist的类PlaylistElement

class Playlist extends PlaylistElement {
    private List<PlaylistElement> elements;
    private PlaybackMode playbackMode;
    @Override
    public List<Song> printSongs() {
        if(this.playbackMode == PlaybackMode.RANDOM) {
            List<Song> songs = new ArrayList<>();
            List<PlaylistElement> shuffleElements = new ArrayList<>();

            //Add all PlaylistElements from elements into shuffleElements
            //and shuffle the shuffleElements collection

            //insert your songs into the songs collection here by sequentially
            //going through your
            //PlaylistElements and inserting the result of their printSongs()
            //implementation (e.g. in a for-loop)

            return songs;
        }
        else if(this.playbackMode == PlaybackMode.SEQUENTIAL) {
            //you can do this on your own
        }
        return null;
    }
}

实施扩展Song的类PlaylistElement

class Song extends PlaylistElement {
    private String title;
    private String artist;
    .
    .
    .
    @Override
    public List<Song> printSongs() {
        //return a List with only this Song instance inside
        return Arrays.asList(new Song[] { this });
    }
}

为您的播放列表播放模式创建一个枚举。

enum PlaybackMode {
    SEQUENTIAL, RANDOM;
}

希望这给您一个总体思路!为了简洁起见,省略了Getters / Setters和其他重要部分。

答案 1 :(得分:1)

尽管已经有了一些答案,但我答应提供一个示例实现。首先,我们有一个公共接口Playable,这是要为复合设计模式实现的类。

public interface Playable {
    String getSongName();
}

接下来,Song类代表一首歌曲。

public class Song implements Playable {

    private String name;

    public Song(String name) {
        this.name = name;
    }

    @Override
    public String getSongName() {
        return name;
    }
}

Playlist类做准备,一个枚举代表差异播放模式。

public enum PlayingMode {
    SEQUENCE, RANDOM
}

现在,最后是播放列表类。

public class Playlist implements Playable {

    private String name;
    private List<Playable> playables = new ArrayList<>();
    private PlayingMode mode;

    private Playable currentItem;
    private List<Playable> next = new ArrayList<>();

    public Playlist(String name, PlayingMode mode) {
        this.name = name;
        this.mode = mode;
    }

    @Override
    public String getSongName() {
        if (playables.isEmpty()) {
            return null;
        }

        if (currentItem == null) {
            // initialize the playing songs
            next.addAll(playables);
            if (mode == PlayingMode.RANDOM) {
                Collections.shuffle(next);
            }
            currentItem = next.get(0);
        } else {
            // if we have a playlist, play its songs first
            if (currentItem instanceof Playlist) {
                String candidate = currentItem.getSongName();
                if (candidate != null) {
                    return candidate;
                }
            }

            int index = next.indexOf(currentItem);
            index++;
            if (index < next.size()) {
                currentItem = next.get(index);
            } else {
                currentItem = null;
            }
        }

        return currentItem != null ? currentItem.getSongName() : null;
    }

    private void addToNext(Playable playable) {
        if (currentItem == null) {
            return;
        }

        // if the playlist is playing, add it to those list as well
        if (mode == PlayingMode.SEQUENCE) {
            next.add(playable);
        } else if (mode == PlayingMode.RANDOM) {
            int currentIndex = next.indexOf(currentItem);
            int random = ThreadLocalRandom.current().nextInt(currentIndex, next.size());
            next.add(random, playable);
        }
    }

    public void addPlayable(Playable playable) {
        Objects.requireNonNull(playable);
        playables.add(playable);
        addToNext(playable);
    }
}

一些例子:

public static void main(String[] args) {
    Song song1 = new Song("Song 1");
    Song song2 = new Song("Song 2");

    Playlist subPlaylist1 = new Playlist("Playlist 1", PlayingMode.RANDOM);
    subPlaylist1.addPlayable(new Song("Song A"));
    subPlaylist1.addPlayable(new Song("Song B"));
    subPlaylist1.addPlayable(new Song("Song C"));

    Song song3 = new Song("Song 3");

    Playlist main = new Playlist("Main", PlayingMode.SEQUENCE);
    main.addPlayable(song1);
    main.addPlayable(song2);
    main.addPlayable(subPlaylist1);
    main.addPlayable(song3);

    String songName = main.getSongName();
    while (songName != null) {
        System.out.println("Current song is: " + songName);
        songName = main.getSongName();

    }
}

可以提供输出:

Current song is: Song 1
Current song is: Song 2
Current song is: Song B
Current song is: Song A
Current song is: Song C
Current song is: Song 3

您还可以在播放时添加歌曲:

while (songName != null) {
    System.out.println("Current song is: " + songName);
    songName = main.getSongName();

    // add songs while playing
    if ("Song A".equals(songName)) {
        subPlaylist1.addPlayable(new Song("Song D"));
        subPlaylist1.addPlayable(new Song("Song E"));
        subPlaylist1.addPlayable(new Song("Song F"));
    }
}

这可能导致:

Current song is: Song 1
Current song is: Song 2
Current song is: Song B
Current song is: Song A
Current song is: Song E
Current song is: Song D
Current song is: Song F
Current song is: Song C
Current song is: Song 3

一些最后的笔记:

  • getIndex方法的运行时间确实是最差的 O(n),如果播放列表中有很多歌曲,则可能会出现问题。像CollectionSet之类的更快的Map可以提供更好的性能,但是实现起来会更加复杂。
  • 类已简化,这意味着一些 getters setters 以及 equals hashCode 为简洁起见,我们省略了它。

答案 2 :(得分:0)

方法1: 创建一个名为playlist和playlist的类,该类可以保存songIds的列表,该列表可以保存来自不同播放列表或歌曲ID的一组歌曲。

class PlayList{
  List<PlayListItem> playlistItems;
}

class PlayListItem{
  List<String> songIds;
}

如果您想识别通过特定子播放列表添加的歌曲集,这将对您有所帮助。但是,与方法2相比,此方法使迭代几乎没有困难

方法2: 此处,在播放列表项中避免使用该列表,因此显示播放列表时的迭代很简单。但是,要确定通过特定SubPlaylist添加的songId列表,必须进行计算。

class PlayList{
  List<PlayListItem> playlistItems;
}
class PlayListItem{
  String songId;
  String referencePlayListId;
}