简体   繁体   中英

ArrayList<HashMap<String,String>> keys iteration/output to arrays

If I have a type

ArrayList<HashMap<String,String>> keys = functionWhichReturnsThisType();

How can I iterate through so that I end up with all <1st string of hashmap> in a string array and likewise <2nd string of hashmap> into another string array.

I have tried to use the iterator but the hierarchy in the data type is confusing me.

 appPrefs = new AppPreferences(context.getApplicationContext());
      ArrayList<HashMap<String,String>> keys = appPrefs.getDownloadUrls();
      ArrayList<String> urls = new ArrayList<String>();
      ArrayList<String> filenames = new ArrayList<String>();
      Iterator myIterator = keys.keySet().iterator();
      while(myIterator.hasNext()) {
          urls.add((String)myIterator.next());
          filenames.add((String)keys.get(myIterator.next()));
      }

If the order doesn't matter, you can try

for (HashMap<String, String> map : keys) {
    urls.addAll(map.keys());
    filenames.addAll(map.values());
}

If you want to keep the order, you can try

for (HashMap<String, String> map : keys) {
    for (Map.Entry<String, String> entry : map.entrySet()) {
        urls.add(entry.getKey());
        filenames.add(entry.getValue());
    }
}

OK, I'll walk through your sample code and show where you're running into issues, and suggest how you can get it to work.

  ArrayList<HashMap<String,String>> keys = appPrefs.getDownloadUrls();

This (above) is fine - but remember keys is an ArrayList . It's a list of HashMap objects, but it's still a list

  ArrayList<String> urls = new ArrayList<String>();
  ArrayList<String> filenames = new ArrayList<String>();

These are good, but in typical Java, it would be better to have List<String> urls = new ArrayList<String>(); to try and keep your variables using interfaces instead of concrete implementations.

  Iterator myIterator = keys.keySet().iterator();
  while(myIterator.hasNext()) {

This won't work, because keys is an ArrayList , and a list does not have a keySet() you want to do:

 Iterator<HashMap<String,String> listIterator = keys.iterator();
 while(listIterator.hasNext()) {
     HashMap<String,String> map = listIterator.next();
     Iterator<String> myIterator = map.keySet().iterator();
     while(myIterator.hasNext()) {

Or, even better would be to use the Java 1.5 for(each) loop:

 for( Map<String,String> map : keys ) {
    for( String url : map.keySet() ) {

--

      urls.add((String)myIterator.next());

The above would work, once you get myIterator to be an iterator over the map, rather than the list.

      filenames.add((String)keys.get(myIterator.next()));

But this won't for 2 reasons

  1. Because keys is still a list.
  2. If you call next on an iterator twice then you get 2 different objects.

You need to have:

      String url = myIterator.next();
      urls.add(url);
      filenames.add(map.get(url));

Or, if you use the for(each) loop I suggested above, then you can skip that first line.

Hope that helps - if something's unclear please add a comment.

Note: solilo 's solution is a lot simpler and is a good way to do it, my answer is here to help you see where you were running into trouble.

This method will work for you extract first and second strings

private void getFirstAndSecondStrings(ArrayList<HashMap<String,String>> keys){
    ArrayList<String> firstStrings = new ArrayList<String>();
    ArrayList<String> secondStrings = new ArrayList<String>();
    for (HashMap<String, String> map : keys) {
        Iterator iterator = map.entrySet().iterator();

        while (iterator.hasNext()) {
            Map.Entry pairs = (Map.Entry)iterator.next();
            firstStrings.add((String)pairs.getValue());
            secondStrings.add((String)pairs.getKey());
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM