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
 
 
 
 
Nigel Wade
Guest
Posts: n/a
 
      02-12-2007
wrote:

> 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


Won't SortedMap do what you want? It should be sorted according to the [natural]
sorting order of whatever object you use for the key. Since the keys are
sorted, if you iterate the SortedMap by key using keySet() method you should
get the items back according to the sorting order of the keys.

TreeMap<KeyObject,ItemObject> sortedMap = new TreeMap();
//populate your map with items...
for(KeyObject key : sortedMap.keySet()) {
ItemObject item = sortedMap.get(key);
}

--
Nigel Wade, System Administrator, Space Plasma Physics Group,
University of Leicester, Leicester, LE1 7RH, UK
E-mail :
Phone : +44 (0)116 2523548, Fax : +44 (0)116 2523555
 
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
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57