Results 1 to 3 of 3

Thread: Iterator class

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    Iterator class

    Does anyone know the proper way to loop through a collection?
    This is a code snippet from a program i am working on and im not sure why i cant use an Iterator on a TreeMap.

    Code:
     Map stringMap = new TreeMap();
    
       for(Iterator i = stringMap.iterator(); i.hasNext();){            
             if(stringMap.containsKey(key)){
                 return stringMap.get(i.next());     
             }
          i.next();
       }
    I can use it on a LinkedList with no problem.
    Code:
        import java.util.*;
     
         public class Test{
            public static void main(String[] args){
              String greeting = "Hello";
              String departing = "GoodBye";
     
             List l = new LinkedList();
             l.add(greeting); 
             l.add(departing);
          
          for(Iterator i = l.iterator(); i.hasNext();){            
             System.out.println(i.next());     
          }

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Code:
     Map stringMap = new TreeMap();
    
       for(Iterator i = (stringMap.values()).iterator(); i.hasNext(){            
             if(stringMap.containsKey(key)){
                 return stringMap.get(i.next());     
             }
          i.next();
       }
    try that. Map/TreeMap does not have an iterator method, but values() returns a Collection, which does have an iterator.

    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width