0

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()));
      }
3
  • 1
    Please post what you have done so far and we will help you finish it off. Commented Mar 11, 2012 at 6:55
  • Link may help you.[stackoverflow.com/questions/7683960/… Commented Mar 11, 2012 at 6:58
  • As You can see I amtring to add themto a list in my code, i know how to convert this to string[] afterwards Commented Mar 11, 2012 at 6:59

3 Answers 3

5

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());
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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.

1 Comment

This is a very good explanation you cleared a few things up for me, thanks a lot.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.