How do I do this?

T

tim

I am trying to use an array of some sort which will allow me to access
a value based on a key (like in a Hashtable) but also allow me to
access all entries sorted by the key.
I have heard all kinds of solutions proposed but there seems to be
nothing as straight forward as there should be.
Can anyone give me some sample code where the table or tree or
whatever is accessed by both the key and iterated through in key
order?

Thanks
 
M

Mike Schilling

I am trying to use an array of some sort which will allow me to access
a value based on a key (like in a Hashtable) but also allow me to
access all entries sorted by the key.
I have heard all kinds of solutions proposed but there seems to be
nothing as straight forward as there should be.
Can anyone give me some sample code where the table or tree or
whatever is accessed by both the key and iterated through in key
order?


Look at java.util.TreeMap.
 
T

tim

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
 
P

Patricia Shanahan

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.

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
 
T

tim

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?
 
M

Muggle

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

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
 
C

Chris Uppal

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
 
P

Patricia Shanahan

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?

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
 
T

tim

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- Hide quoted text -

- 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top