Using the Set API

My instructor gave me a few lines of code that I am supposed to use in my program:

Set keys = map.keySet();
Iterator it = keys.iterator();

And I get this error when I try and compline my program:
Test2.java:66: cannot resolve symbol
symbol  : variable map
location: class Test2
                Set keys = map.keySet();
                           ^

Can anybody tell me what is wrong with it, because I cant make sense out of the info on the java site.  

Thanks,
Jonathan.
twibblejAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NetworkArchitekCommented:
Hi twibblej,
Well yes, that's not going to work. You need to make these import statements:

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;


then before you do Set keys = map.keySet(); ... do something like this:

        Map map = new HashMap();      
        Set keys = map.keySet();
        Iterator it = keys.iterator();

Hope this helps.





Cheers!
0
twibblejAuthor Commented:
Ok good deal.  So now it is compiling.

What I need to do now is to add string objects to the set.   When I try and add one that is already in the set I need to keep track of how many times a duplicate of each string is attempted to be added.  Is there a way to to add the string object, and an integer object  at once, and then if I try and add a duplicate I get its Integer out, increment it, and put it back in?

Thanks,
Jonathan.
0
twibblejAuthor Commented:
Ok, so I think I have figured out how to add the elements.  Now I need to get them back out and print them.  Any help as to which methods to use would be appreciate it.

Thanks.
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

twibblejAuthor Commented:
I thik i have to use the itterator to get them all out... but I'm not sure how to do it.

Thanks.
0
sudhakar_koundinyaCommented:
You need to import java.util.*; in your Test class

Regarding working with Maps keys and Iterators

see the example in this link
http://www.javaalmanac.com/egs/java.util/coll_MapOrder.html
0
sudhakar_koundinyaCommented:
ap map = new LinkedHashMap();
   
    // Add some elements
    map.put("1", "value1");
    map.put("2", "value2");
    map.put("3", "value3");
    map.put("2", "value4");
   
    // List the entries
    for (Iterator it=map.keySet().iterator(); it.hasNext(); ) {
        Object key = it.next();
        Object value = map.get(key);
           System.err.println("Key :"+key);
           System.err.println("Value :"+value);
    }
0
sudhakar_koundinyaCommented:
 Or



Map map = new LinkedHashMap();
   
    // Add some elements
    map.put("1", "value1");
    map.put("2", "value2");
    map.put("3", "value3");
    map.put("2", "value4");
  Set set=map.keySet();
   Iterator it=set.iterartor();
    // List the entries
    for (; it.hasNext(); ) {
        Object key = it.next();
        Object value = map.get(key);
           System.err.println("Key :"+key);
           System.err.println("Value :"+value);
    }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sudhakar_koundinyaCommented:
:-)   (-:
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.