Alternate approach:

for (String key : a.keySet()) {
   Test value = a.get(key);
  // process key/value
}

On Wed, Feb 17, 2010 at 4:29 PM, Nic Fox <[email protected]> wrote:

> In your case which uses a Map, you could try it using the Map.Entry object
> which contains complete key-value pair for each entry in the Map. The loop
> code would be something like this (untested though...):
>
> for (Map.Entry<String, Test> entry : a.entrySet()) {
>    String key = entry.getKey();
>    Test value = entry.getValue();
>    //now do things with key and value...
> }
>
> This example uses the enhanced for-loop which in the above case puts each
> key-value pair into the 'entry' object in turn so that you can work with
> them inside the for-loop. Because of that you actually don't need to create
> the Iterator objects.
>
> This should be OK unless of course you need to use the remove() method or
> any of the other things that the for-each loop doesn't support. In that case
> I guess you'd use an Iterator for which I found a Generics based approach
> and one that uses casts here:
>
> http://www.sergiy.ca/how-to-iterate-over-a-map-in-java/
>
> Have fun.
>
>
> On 15/02/2010, at 9:29 AM, Montreal Canadien wrote:
>
>  import java.awt.Point;
>> import java.util.Iterator;
>> import java.util.Map;
>> import java.util.TreeMap;
>>
>> public class Main {
>>
>>   public static void main(String[] argv) {
>>
>>       TreeMap<String, Test> a;
>>       a = new TreeMap<String, Test>();
>>       a.put("10100", new Test(new Point(1, 2)));
>>       Iterator<String> b = a.keySet().iterator();
>>       Iterator<Test> c = a.values().iterator();
>>
>>
>>
>>   }
>> }
>>
>> --
>> To post to this group, send email to
>> [email protected]
>> To unsubscribe from this group, send email to
>> [email protected]<javaprogrammingwithpassion%[email protected]>
>> For more options, visit this group at
>> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>>
>
> --
> To post to this group, send email to
> [email protected]
> To unsubscribe from this group, send email to
> [email protected]<javaprogrammingwithpassion%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/javaprogrammingwithpassion?hl=en
>

-- 
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en

Reply via email to