On Feb 10, 3:08 am, "Mike Schilling" <mscottschill...@hotmail.com>
wrote:
> "t...@nocomment.com" <tbi...@netscape.net> wrote in message
>
[quoted text clipped - 10 lines]
>
> Look at java.util.TreeMap.
How do you iterate through the TreeMap. So far, I have this but I am
not sure how to loop through the map and get all of the keys and
values.
package test;
import java.util.*;
public class HashtableToVector {
public static void main (String[] args)
{
Collection c;
Iterator iter;
TreeMap map = new TreeMap ();
map.put ("ccc", "third.6");
map.put ("bb3", "second.3");
map.put ("bb2", "second.2");
map.put ("bb1", "second.1");
map.put ("aaa", "third");
System.out.println ("map = " + map);
System.out.println ("map.get('bb3') = " + map.get("bb3"));
}
}
And I get as output
map = {aaa=third, bb1=second.1, bb2=second.2, bb3=second.3, ccc=third.
6}
map.get('bb3') = second.3
Patricia Shanahan - 10 Feb 2007 14:07 GMT
> On Feb 10, 3:08 am, "Mike Schilling" <mscottschill...@hotmail.com>
> wrote:
[quoted text clipped - 15 lines]
> not sure how to loop through the map and get all of the keys and
> values.
To get both key and value, use the entrySet method.
"Returns a set view of the mappings contained in this map. The set's
iterator returns the mappings in ascending key order."
Patricia
tim@nocomment.com - 10 Feb 2007 16:47 GMT
Here is the code I am trying to run. I am getting the error specified
after the code. I don't understand why I have to do so much for such a
basic thing but none the less...
package test;
import java.util.*;
import java.util.Map.Entry;
public class Map {
public static void main (String[] args) {
TreeMap map = new TreeMap ();
map.put ("ccc", "third.6");
map.put ("bb3", "second.3");
map.put ("bb2", "second.2");
map.put ("bb1", "second.1");
map.put ("aaa", "third");
Iterator i = map.entrySet().iterator();
while (i.hasNext()) {
Map.Entry e = (Map.Entry)i.next();
if (e != null) {
String key = (String)e.getKey();
if (key != null && key.length() > 0) {
String value = (String)e.getValue();
} // end if key not null
} // end if entry not null
} // end while
}
}
I am getting the error:
Map.Entry cannot be resolved to a type
I am importing Map.Entry. Anyone have any ideas as to what is causing
this?
Chris Uppal - 10 Feb 2007 17:32 GMT
> public class Map {
> I am getting the error:
> Map.Entry cannot be resolved to a type
It's because you have called your test class "Map" which hides the definition
of "Map" in java.util. Importing Map.Entry makes no difference at all (in fact
I don't think it means anything -- its trying to import a class called Entry
from a package called Map). Change the name to, say, MapTest, and it'll work.
Alternatively, if you insist (perhaps just out of curiosity) in calling your
class "Map" then you'll have to use the fully-qualified name of the Map from
Java utils everywhere, thus:
java.util.Map.Entry e = (java.util.Map.Entry)i.next();
BTW, you don't need any of those null tests. Nor the test whether
key.length>0, since "" is a perfectly valid key (if you had added it in the
first place).
-- chris
Patricia Shanahan - 10 Feb 2007 17:34 GMT
> Here is the code I am trying to run. I am getting the error specified
> after the code. I don't understand why I have to do so much for such a
[quoted text clipped - 31 lines]
> I am importing Map.Entry. Anyone have any ideas as to what is causing
> this?
Possibly the fact that you are declaring a class Map, so the compiler is
going to look for a class Entry inside it?
If that is the problem, it can be fixed by either fully qualifying:
java.util.Map.Entry
or by picking a better name for your class. It is presumably some
particular type of map that you are implementing, not the overall
general concept of map.
Patricia
Muggle - 10 Feb 2007 16:54 GMT
> t...@nocomment.com wrote:
> > On Feb 10, 3:08 am, "Mike Schilling" <mscottschill...@hotmail.com>
[quoted text clipped - 23 lines]
>
> Patricia
Try something like this :
iter = map.keySet().iterator();
while (iter.hasNext()) {
String key = (String) iter.next();
String value = (String) map.get(key);
//.......
}
Thank you
Muggle
tim@nocomment.com - 10 Feb 2007 17:37 GMT
> > t...@nocomment.com wrote:
> > > On Feb 10, 3:08 am, "Mike Schilling" <mscottschill...@hotmail.com>
[quoted text clipped - 37 lines]
>
> - Show quoted text -
Thanks a lot, that was the missing piece to my puzzle. Try as I might,
I still need examples for new things.