Re: I need something like a "Hashtable<String[], Integer>" kind ofdata type ...

Discussion in 'Java' started by Tom Anderson, Nov 28, 2010.

  1. Tom Anderson

    Tom Anderson Guest

    On Sat, 27 Nov 2010, Patricia Shanahan wrote:

    > 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
    Tom Anderson, Nov 28, 2010
    #1
    1. Advertising

  2. Tom Anderson

    Eric Sosman Guest

    On 11/28/2010 4:28 PM, Tom Anderson wrote:
    > On Sat, 27 Nov 2010, Patricia Shanahan wrote:
    >
    >> 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
    lid
    Eric Sosman, Nov 28, 2010
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Tkg=?=

    simple problem - need something like IsNull

    =?Utf-8?B?Tkg=?=, Sep 5, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    415
    =?Utf-8?B?Tkg=?=
    Sep 5, 2005
  2. Replies:
    0
    Views:
    359
  3. Diego Martins
    Replies:
    5
    Views:
    471
    Diego Martins
    Sep 5, 2006
  4. Thibaut Barrère
    Replies:
    5
    Views:
    155
    Thibaut Barrère
    Dec 31, 2008
  5. dmitrey
    Replies:
    5
    Views:
    394
    Jussi Piitulainen
    Apr 20, 2012
Loading...

Share This Page