Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How do I do this?

Reply
Thread Tools

How do I do this?

 
 
tim@nocomment.com
Guest
Posts: n/a
 
      02-10-2007
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

 
Reply With Quote
 
 
 
 
Mike Schilling
Guest
Posts: n/a
 
      02-10-2007

"(E-Mail Removed)" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>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.


 
Reply With Quote
 
 
 
 
tim@nocomment.com
Guest
Posts: n/a
 
      02-10-2007
On Feb 10, 3:08 am, "Mike Schilling" <(E-Mail Removed)>
wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) ups.com...
>
> >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.


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


 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      02-10-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Feb 10, 3:08 am, "Mike Schilling" <(E-Mail Removed)>
> wrote:
>> "(E-Mail Removed)" <(E-Mail Removed)> wrote in message
>>
>> news:(E-Mail Removed) ups.com...
>>
>>> 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.

>
> 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
 
Reply With Quote
 
tim@nocomment.com
Guest
Posts: n/a
 
      02-10-2007
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?


 
Reply With Quote
 
Muggle
Guest
Posts: n/a
 
      02-10-2007
On Feb 10, 9:07 am, Patricia Shanahan <(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> > On Feb 10, 3:08 am, "Mike Schilling" <(E-Mail Removed)>
> > wrote:
> >> "(E-Mail Removed)" <(E-Mail Removed)> wrote in message

>
> >>news:(E-Mail Removed) roups.com...

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

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


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

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      02-10-2007
(E-Mail Removed) wrote:

> 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



 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      02-10-2007
(E-Mail Removed) wrote:
> 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
 
Reply With Quote
 
tim@nocomment.com
Guest
Posts: n/a
 
      02-10-2007
On Feb 10, 11:54 am, "Muggle" <(E-Mail Removed)> wrote:
> On Feb 10, 9:07 am, Patricia Shanahan <(E-Mail Removed)> wrote:
>
>
>
>
>
> > (E-Mail Removed) wrote:
> > > On Feb 10, 3:08 am, "Mike Schilling" <(E-Mail Removed)>
> > > wrote:
> > >> "(E-Mail Removed)" <(E-Mail Removed)> wrote in message

>
> > >>news:(E-Mail Removed) roups.com...

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

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

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM



Advertisments