gpt4 book ai didi

java - 在 Hashmap 数组中搜索特定键 android

转载 作者:搜寻专家 更新时间:2023-11-01 09:07:18 24 4
gpt4 key购买 nike

我有一个 HashMapArrayList,如下所示:

ArrayList<HashMap<String, String>> playListSongs = new ArrayList<HashMap<String, String>>();

我这样填充列表:

for (int i1 = 0; i1 < nl.getLength(); i1++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i1);
// adding each child node to HashMap key => value
map.put(KEY_SONGS, String.valueOf(i1));
map.put(KEY_FILE, parser.getValue(e, KEY_FILE));
map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
map.put(KEY_FILE, parser.getValue(e, KEY_FILE));
map.put(KEY_ALBUM, parser.getValue(e, KEY_ALBUM));
map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
map.put(KEY_ARTWORK, parser.getValue(e, KEY_ARTWORK));
// Songs List of selected playlist
playListSongs.add(map);
// list view bind
PopulateList(playListSongs);
}

现在我想从 HashMap-array-list(playListSongs) 获取特定艺术家的专辑和歌曲以及歌曲计数,以将其绑定(bind)到 ListView 。

我怎样才能做到这一点?

最佳答案

您可以使用此方法获取特定艺术家的歌曲:

private ArrayList<String> getSongsByArtist(String artist){
ArrayList<String> songs = new ArrayList<String>();
for (HashMap<String, String> hashMap : playListSongs) {
if(hashMap.get(KEY_ARTIST).equals(artist)){
songs.add(hashMap.get(KEY_TITLE));
}
}
return songs;
}

要获得不同的艺术家,请使用以下方法:

private ArrayList<String> getDistinctArtists(){
HashMap<String,String> duplicateTracker = new HashMap<String, String>();
ArrayList<String> distinctArtists = new ArrayList<String>();
for (HashMap<String, String> hashMap : playListSongs) {
// Check whether artist is already added or not
if(duplicateTracker.containsKey(hashMap.get(KEY_ARTIST))==false){
//Add artist name to hash map
duplicateTracker.put(hashMap.get(KEY_ARTIST), hashMap.get(KEY_ARTIST));
distinctArtists.add(hashMap.get(KEY_ARTIST));
}
}
return distinctArtists;
}

关于java - 在 Hashmap 数组中搜索特定键 android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11009576/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com