Search code examples
androidlistarraylisthashmapdynamic-data

How to retrieve data from HashMap<List<String>, List<String>> in android using java


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}

I have created a hashmap like this:

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

I have another ArrayList like this:

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, .....}

Now, I need an ArrayList containing the usernames for each ids in finaluserreceiverids corresponding to the exact indexes. Like this:

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, .....}

I'm getting all these data from various database tables dynamically, so I can't put the names manually into the respective indexes.


Solution

  • I assume your HashMap is wrong. If so, please take a look on the following example:

    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));
    }