老师为什么我的查询放方法一直会返回null?

老师为什么我的查询放方法一直会返回null?

//测试方法
public void testPlayListCollection() {
Song song_1 = new Song("s001", "小星星", "A");
Song song_2 = new Song("s002", "两只老虎", "B");
Song song_3 = new Song("s003", "小太阳", "C");
Song song_4 = new Song("s004", "小兔子", "D");
// 创建主播房列表
PlayList mainPlayList = new PlayList("主播放列表");
// 将歌曲添加到主播房列表
mainPlayList.addToPlayList(song_1);
mainPlayList.addToPlayList(song_2);
mainPlayList.addToPlayList(song_3);
// 将播放列表的歌曲添加到心得播放列表
PlayList favouritePlayList = new PlayList("最喜爱的歌曲");
favouritePlayList.addToPlayList(mainPlayList.getMusicList().get(0));
favouritePlayList.addToPlayList(mainPlayList.getMusicList().get(1));
favouritePlayList.addToPlayList(mainPlayList.getMusicList().get(2));
PlayListCollection plc = new PlayListCollection();
plc.addPlayList(mainPlayList);
plc.addPlayList(favouritePlayList);
plc.dispalyPlayListName();
PlayList playList =plc.searchPlayList("主播放列表");
playList.displayAllSong();

}
​public PlayList searchPlayList(String playListName) {
PlayList playList = null;
Set<String> set = playListMap.keySet();
for (String string : set) {
if (string.equals(playListName)) {
playList = (PlayList)playListMap.get(string);
break;
}
}
return playList;
}

http://img1.sycdn.imooc.com//climg/601ceb1009fa33c219381098.jpg


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学,你好!你可以排查一下,PlayListCollection 的结构,你不在PlayListCollection 中查询,为什么要去playListMap中查询呢?应该就是PlayListCollection和playListMap这两个纽带出的问题。playListMap里面都是什么,你可以输出打印一下。祝:学习愉快!



  • Ternence_T 提问者 #1
    package player;

    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;

    public class PlayListCollection {
    private Map<String, PlayList> playListMap; // 存放播放列表集合
    // 构造方法

    public PlayListCollection() {
    this.playListMap = new HashMap<String, PlayList>();
    }

    // getter和setter方法
    public Map<String, PlayList> getPlayListMap() {
    return playListMap;
    }

    public void setPlayListMap(Map<String, PlayList> playListMap) {
    this.playListMap = playListMap;
    }

    /**
    * 添加播放列表到播放器
    *
    * @param playList 要添加的播放列表
    */
    public void addPlayList(PlayList playList) {
    PlayList playList2 = searchPlayList(playList.getPalyListName());
    if (playList2 == null) {
    playListMap.put(playList.getPalyListName(), playList2);
    } else {
    System.out.println("歌单名为:" + playList.getPalyListName() + "的歌单已存在!");
    }
    }

    /**
    * 删除播放器列表
    *
    * @param playList 要删除的播放器列表
    */
    public void delete(PlayList playList) {
    PlayList playList2 = searchPlayList(playList.getPalyListName());
    if (playList2 != null) {
    playListMap.remove(playList.getPalyListName());
    } else {
    System.out.println("歌单名为:" + playList.getPalyListName() + "的歌单不存在!");
    }
    }

    /**
    * 通过名字查询播放器列表
    *
    * @param playListName 要查询的播放列表
    * @return 返回查询到的播放列表
    */
    public PlayList searchPlayList(String playListName) {
    PlayList playList = null;
    Set<String> set = playListMap.keySet();
    for (String string : set) {
    if (string.equals(playListName)) {
    playList = (PlayList)playListMap.get(string);
    break;
    }
    }
    return playList;
    }

    // 显示所有播放列表的名称
    public void dispalyPlayListName() {
    Set<String> set = playListMap.keySet();
    System.out.println("播放列表名称为:");
    for (String string : set) {
    System.out.println(string);
    }
    }
    }
    package player;

    public class Test {

    // 歌曲测试
    public void songTest() {
    Song song_1 = new Song("s001", "小星星", "A");
    Song song_2 = new Song("s002", "两只老虎", "B");
    Song song_3 = new Song("s003", "小太阳", "C");
    Song song_4 = new Song("s004", "小兔子", "D");
    // 测试toString方法
    // System.out.println(song_1);
    // 测试两个对象是否相等
    // System.out.println("song_1==song_2:"+song_1.equals(song_2));
    // System.out.println("song_1==song_4:"+song_1.equals(song_4));
    PlayList playList = new PlayList("主播放列表");
    // 添加歌曲到播放列表
    playList.addToPlayList(song_1);
    playList.addToPlayList(song_2);
    playList.addToPlayList(song_3);
    // playList.addToPlayList(song_4);
    // 显示所有歌曲
    // playList.displayAllSong();
    //
    // 通过ID查询歌曲
    // playList.searchSongById("s005");
    //
    // playList.displayAllSong();
    // 通过名字查询歌曲
    // playList.searchSongByName("小星");
    //
    // 更新歌曲信息
    // playList.updateSong("s001", song_4);
    // playList.displayAllSong();
    // 删除歌曲
    // playList.deleteSong("s006");
    // playList.displayAllSong();
    }

    // 播放列表测试
    public void testPlayListCollection() {
    Song song_1 = new Song("s001", "小星星", "A");
    Song song_2 = new Song("s002", "两只老虎", "B");
    Song song_3 = new Song("s003", "小太阳", "C");
    Song song_4 = new Song("s004", "小兔子", "D");
    // 创建主播房列表
    PlayList mainPlayList = new PlayList("主播放列表");
    // 将歌曲添加到主播房列表
    mainPlayList.addToPlayList(song_1);
    mainPlayList.addToPlayList(song_2);
    mainPlayList.addToPlayList(song_3);
    // 将播放列表的歌曲添加到心得播放列表
    PlayList favouritePlayList = new PlayList("最喜爱的歌曲");
    favouritePlayList.addToPlayList(mainPlayList.getMusicList().get(0));
    favouritePlayList.addToPlayList(mainPlayList.getMusicList().get(1));
    favouritePlayList.addToPlayList(mainPlayList.getMusicList().get(2));
    PlayListCollection plc = new PlayListCollection();
    plc.addPlayList(mainPlayList);
    plc.addPlayList(favouritePlayList);
    plc.dispalyPlayListName();
    PlayList playList = plc.searchPlayList("最喜爱的歌曲");
    playList.displayAllSong();


    }

    public static void main(String[] args) {
    Test test = new Test();
    test.testPlayListCollection();

    }

    }
    package player;

    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;

    public class PlayList {
    private String palyListName; // 播放列表名称
    private List<Song> musicList; // 歌曲列表

    /**
    * 构造方法
    *
    * @param palyListName 播放列表名称
    */
    public PlayList(String palyListName) {
    this.palyListName = palyListName;
    this.musicList = new ArrayList<Song>();
    }
    //getter和setter方法
    public String getPalyListName() {
    return palyListName;
    }

    public void setPalyListName(String palyListName) {
    this.palyListName = palyListName;
    }

    public List<Song> getMusicList() {
    return musicList;
    }

    public void setMusicList(List<Song> musicList) {
    this.musicList = musicList;
    }

    @Override
    public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((musicList == null) ? 0 : musicList.hashCode());
    result = prime * result + ((palyListName == null) ? 0 : palyListName.hashCode());
    return result;
    }

    @Override
    public boolean equals(Object obj) {
    if (this == obj)
    return true;
    if(obj.getClass()==this.getClass()) {
    PlayList playList = (PlayList)obj;
    return playList.getPalyListName().equals(this.getPalyListName())&&playList.getMusicList().equals(this.getMusicList());
    }
    return false;
    }

    /**
    * 将歌曲添加到播放列表
    *
    * @param song 要添加的歌曲
    */
    public void addToPlayList(Song song) {
    // 判断是否是重复歌曲
    boolean flag = false;
    for (Song song_1 : musicList) {
    if (song_1.equals(song)) {
    flag = true;
    break;
    }
    }
    if (flag) {
    System.out.println("此歌曲已经存在;");
    System.out.println(song);
    System.out.println("==========================");
    } else {
    musicList.add(song);
    }

    }

    /**
    * 显示播放列表中歌曲
    */
    public void displayAllSong() {
    for (Song song : musicList) {
    System.out.println(song);
    }
    }

    /**
    * 通过ID查询歌曲的方法
    *
    * @param id 查询的ID
    * @return 歌曲信息
    */
    public Song searchSongById(String id) {
    Song song = null;
    for (Song song2 : this.musicList) {
    if (song2.getId() == id) {
    song = song2;
    break;
    }
    }
    return song;
    }

    /**
    * 通过名称查询歌曲的方法
    *
    * @param name 歌曲名称
    * @return 歌曲信息
    */
    public Song searchSongByName(String name) {
    Song song = null;
    for (Song sg : this.musicList) {
    if (sg.getName() == name) {
    song = sg;
    break;
    }
    }
    return song;
    }

    /**
    * 修改歌曲
    *
    * @param id 需要修改的歌曲ID
    * @param song 新的歌曲
    */
    public void updateSong(String id, Song song) {
    Song son2= searchSongById(id);
    if(son2==null) {
    System.out.println("没有找到歌曲ID为:"+id+"的歌曲");
    }else {
    musicList.remove(son2);
    musicList.add(song);
    System.out.println("歌曲更新成功!");
    }
    }

    /**
    * 从播放列表删除歌曲
    *
    * @param id 歌曲ID
    */
    public void deleteSong(String id) {
    Song song=searchSongById(id);
    if(song==null) {
    System.out.println("没有找到歌曲ID为:"+id+"的歌曲");
    }else {
    musicList.remove(song);
    System.out.println("删除成功!");
    }
    }

    // 按歌曲名进行排序
    public void sortBySongName() {

    }
    }
    package player;

    public class Song {
    private String id; // id
    private String name;// 歌曲名字
    private String singer;// 演唱者
    // 构造方法

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

    // 属性的getter/setter方法
    public String getId() {
    return id;
    }

    public void setId(String id) {
    this.id = id;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getSinger() {
    return singer;
    }

    public void setSinger(String singer) {
    this.singer = singer;
    }

    // 重写toString方法,输出歌曲歌曲信息
    @Override
    public String toString() {
    return "歌曲信息 歌曲ID:" + id + " 歌曲名称:" + name + " 演唱者:" + singer;
    }

    @Override
    public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((id == null) ? 0 : id.hashCode());
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result + ((singer == null) ? 0 : singer.hashCode());
    return result;
    }

    @Override
    public boolean equals(Object obj) {
    // 判断对象是否相等,相等直接返回true
    if (obj == this) {
    return true;
    }
    if (obj.getClass() == Song.class) {
    Song song = (Song) obj;
    return (song.getId().equals(id)) && (song.getName().equals(name))
    && (song.getSinger().equals(singer));
    }
    return false;
    }

    }


    2021-02-05 16:33:15
  • Ternence_T 提问者 #2

    我就是在plc对象里面查询的啊,是在找不出问题了。老师你帮我看看代码

    2021-02-05 16:34:58
  • 好帮手慕雪 回复 提问者 Ternence_T #3

    这里应该是playList吧?改过来

    http://img1.sycdn.imooc.com//climg/601d103709b2b22805860191.jpg


    2021-02-05 17:30:56
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师