?
Solved

Using the Set API

Posted on 2004-11-04
8
Medium Priority
?
213 Views
Last Modified: 2010-03-31
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.
0
Comment
Question by:twibblej
  • 4
  • 3
8 Comments
 
LVL 10

Assisted Solution

by:NetworkArchitek
NetworkArchitek earned 800 total points
ID: 12501366
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
 

Author Comment

by:twibblej
ID: 12501476
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
 

Author Comment

by:twibblej
ID: 12501504
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:twibblej
ID: 12501514
I thik i have to use the itterator to get them all out... but I'm not sure how to do it.

Thanks.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12501602
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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12501612
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
 
LVL 14

Accepted Solution

by:
sudhakar_koundinya earned 1200 total points
ID: 12501624
 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
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12501691
:-)   (-:
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question