Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Re: I need something like a "Hashtable<String[], Integer>" kind ofdata type ... (http://www.velocityreviews.com/forums/t739013-re-i-need-something-like-a-hashtable-string-integer-kind-ofdata-type.html)

Tom Anderson 11-28-2010 09:28 PM

Re: I need something like a "Hashtable<String[], Integer>" kind ofdata type ...
 
On Sat, 27 Nov 2010, Patricia Shanahan wrote:

> lbrtchx@gmail.com wrote:
>> I need something like a "Hashtable<String[], Integer>" kind of data
>> type ...
>>
>> I have a String[] (String array (a Sequence of String more precisely
>> since order matters)) which I need to use in a hashtable for fastest
>> access
>>
>> If the sequence of Strings is found in the hashmap all I need is the
>> index

>
> Use a HashMap<java.util.List<String>, Integer>. List has the equals and
> hashCode rules you need - a List is equal to another List that contains
> the same elements in the same order. An array is only equal to itself.


As something of an aside, if i understand "If the sequence of Strings is
found in the hashmap all I need is the index" to mean "I have a sequence
of sequences of strings, and i want to be able to find the index of a
given sequence of strings quickly", then is there a data structure which
will do a better job than a hashmap? You can certainly do it with a
hashmap, but hashmaps store arbitrary values, whereas here, the values are
more tightly constrained - they're indexes in the top-level sequence. Can
that be used to find a tighter fit? I have half an eye on the problem of
updating the collection here; with a hashmap, if i insert a new sequence
of strings in the middle, i'm going to need to update the stored indexes
for all those which come after it.

I can't think of anything very good.

tom

--
An unreliable programming language generating unreliable programs
constitutes a far greater risk to our environment and to our society than
unsafe cars, toxic pesticides, or accidents at nuclear power stations. --
C. A. R. Hoare

Eric Sosman 11-28-2010 10:39 PM

Re: I need something like a "Hashtable<String[], Integer>" kind ofdata type ...
 
On 11/28/2010 4:28 PM, Tom Anderson wrote:
> On Sat, 27 Nov 2010, Patricia Shanahan wrote:
>
>> lbrtchx@gmail.com wrote:
>>> I need something like a "Hashtable<String[], Integer>" kind of data
>>> type ...
>>>
>>> I have a String[] (String array (a Sequence of String more precisely
>>> since order matters)) which I need to use in a hashtable for fastest
>>> access
>>>
>>> If the sequence of Strings is found in the hashmap all I need is the
>>> index

>>
>> Use a HashMap<java.util.List<String>, Integer>. List has the equals
>> and hashCode rules you need - a List is equal to another List that
>> contains the same elements in the same order. An array is only equal
>> to itself.

>
> As something of an aside, if i understand "If the sequence of Strings is
> found in the hashmap all I need is the index" to mean "I have a sequence
> of sequences of strings, and i want to be able to find the index of a
> given sequence of strings quickly", then is there a data structure which
> will do a better job than a hashmap? You can certainly do it with a
> hashmap, but hashmaps store arbitrary values, whereas here, the values
> are more tightly constrained - they're indexes in the top-level
> sequence. Can that be used to find a tighter fit? I have half an eye on
> the problem of updating the collection here; with a hashmap, if i insert
> a new sequence of strings in the middle, i'm going to need to update the
> stored indexes for all those which come after it.


The O.P. didn't explain where his "index" comes from, or whether
it depends on the current repertoire of String[]'s in the collection.
If he wants it to reflect some kind of ordering on the String[]'s, so
that a late arrival can take over the "index" of an earlier-but-larger
String[], then I don't think HashMap is a suitable data structure. Not
by itself, anyhow, although it might be usable in combination with
something else.

On the other hand, if "index" is something that remains constant
for a given String[] -- an "insertion sequence number," say -- then a
HashMap would do just fine.

I think the O.P. should explain his situation more fully if he wants
advice better tailored to his circumstances. All we know at the moment
is that he has String[]'s, he associates each with an Integer "index"
of unknown provenance, and he's decided that "fast access" can be had
by hashing them. That's not much to go on; a few words about the nature
of the overarching problem seem in order ...

--
Eric Sosman
esosman@ieee-dot-org.invalid


All times are GMT. The time now is 01:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.