如何从HashMap <list <string>,List <string>&gt;中检索数据在android中使用java </string> </list <string>

时间:2014-02-26 10:56:59

标签: android list arraylist hashmap dynamic-data

List<String> uniqueidslist = new ArrayList<String>();
uniqueidslist.size() is 7

sample uniqueidslist data --> uniqueidslist = {u1, u2, u3, u4, u5, u6, u7}

List<String> receiveruserfullnamelist= new ArrayList<String>();
receiveruserfullnamelist.size() is 7

sample receiveruserfullnamelist data --> receiveruserfullnamelist = {username1, username2, username3, username4, username5, username6, username7}

我创建了一个这样的hashmap:

HashMap<List<String>, List<String>> usermap = new HashMap<List<String>, List<String>>();
usermap.put(uniqueidslist, receiveruserfullnamelist);

我有另一个这样的ArrayList:

List<String> finaluserreceiverids = new ArrayList<String>();
finaluserreceiverids.size() is 51

sample finaluserreceiverids data --> finaluserreceiverids = {u1, u4, u7, u1, u1, u1, u2, u4, u5, u2, u2, u2, u3, .....}

现在,我需要一个ArrayList,其中包含finaluserreceiverids中与精确索引相对应的每个id的用户名。 像这样:

List<String> finaluserreceivernames = new ArrayList<String>();
finaluserreceivernames.size() is 51

sample finaluserreceivernames data --> finaluserreceivernames = {username1, username4, username7, username1, username1, username1, username2, username4, username5, username2, username2, username2, username3, .....}

我动态地从各种数据库表中获取所有这些数据,因此我无法手动将名称放入相应的索引中。

1 个答案:

答案 0 :(得分:0)

我认为你的HashMap是错误的。如果是这样,请查看以下示例:

List<String> uniqueidslist = new ArrayList<String>();
List<String> receiveruserfullnamelist= new ArrayList<String>();
HashMap<String, String> usermap = new HashMap<String, String>();
for (int i = 0; i<uniqueidslist.size();i++){
    usermap.put(uniqueidslist.get(i),receiveruserfullnamelist.get(i));
}
ArrayList<String> finaluserreceiverids = new ArrayList<>();
ArrayList<String> finaluserreceivernames = new ArrayList<>();
for (String x:finaluserreceiverids){
    finaluserreceivernames.add(usermap.get(x));
}