【发布时间】:2014-09-16 12:11:25
【问题描述】:
我知道 HashMap 不允许重复键(它确实允许重复值)。 但是在这个完美运行的示例中,所有值都具有相同的键(这意味着键不是唯一的)也许我误解了代码。有人可以帮助我正确理解它。
这是代码
public class PlayListManager {
//**********************************hashmap keys************************
public static final String ALL_VIDEOS = "AllVideos";
public static final String ALL_Songs = "AllSongs";
//***************************************************************************
..
...
....
while (cursor.moveToNext()) {
Songs song = new Songs();
song.songId = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
song.artist = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
song.title = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
song.songName = cursor
.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
song.duration = Integer.parseInt(cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
song.albumId = Integer.parseInt(cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID)));
song.songData = cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
//*******here it uses the same ky for all the videos?!!*************
HashMap<String, Object> songMap = new HashMap<String, Object>();
songMap.put(ALL_Songs, song);
songsList.add(songMap);
}
}
【问题讨论】:
-
当您尝试打印地图中的键时,输出是什么? docs.oracle.com/javase/7/docs/api/java/util/… 见此链接。您一直在替换此密钥。
-
Here 是一个解释。