8 Replies - 2896 Views - Last Post: 20 January 2011 - 05:03 AM Rate Topic: -----

#1 Guest_hasher*


Reputation:

index access a hashtable

Posted 19 January 2011 - 11:42 PM

how do i have direct access to a key position in a hashtable ?
Is This A Good Question/Topic? 0

Replies To: index access a hashtable

#2 masijade   User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: index access a hashtable

Posted 20 January 2011 - 12:18 AM

You don't. You use the actual key.
Was This Post Helpful? 0
  • +
  • -

#3 Guest_hasher*


Reputation:

Re: index access a hashtable

Posted 20 January 2011 - 12:31 AM

i know there is a way to sequentially access elements. (Enumeration.) But i think there should be a way of directly accessing elements.
Was This Post Helpful? 0

#4 Guest_hasher*


Reputation:

Re: index access a hashtable

Posted 20 January 2011 - 12:32 AM

i know there is a way to sequentially access elements. (Enumeration.) But i think there should be a way of directly accessing elements.
Was This Post Helpful? 0

#5 masijade   User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: index access a hashtable

Posted 20 January 2011 - 12:54 AM

uhm get(key)? see the API docs.
Was This Post Helpful? 0
  • +
  • -

#6 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: index access a hashtable

Posted 20 January 2011 - 02:07 AM

As masijade says, get(key) is the direct way to access the elements.

If this isn't fulfilling your needs maybe you could describe the task you are trying to solve. Gaining access the the Hashtable's underlying array is unlikely to be a good solution so understanding your task would help us suggest a different one.
Was This Post Helpful? 0
  • +
  • -

#7 Guest_hasher*


Reputation:

Re: index access a hashtable

Posted 20 January 2011 - 02:32 AM

i need to randomly select a key.
Was This Post Helpful? 0

#8 masijade   User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: index access a hashtable

Posted 20 January 2011 - 02:33 AM

So get the keyset as a set, call toArray on that, and pull keys out of that array.
Was This Post Helpful? 1
  • +
  • -

#9 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: index access a hashtable

Posted 20 January 2011 - 05:03 AM

I'd go with the keySet() technique too. If the toArray bothers you, use an iterator to get to the generated index:

int requiredIndex = /*someRandomNumber*/;
assert requiredIndex < map.size();
Set<K> keys = map.keySet();
Iterator<K> = it = keys.iterator();
for(int i = 0; i < requuiredIndex; i++) {
  it.next();
}
K randomKey = it.next();


If it seems wasteful using the keySet technique, try it out and see if it really is slow. If you find it too slow after testing, consider keeping an ArrayList of keys separately. Best to avoid this though. This is where it becomes easy to introduce bugs.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1