Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

How to obtain a Map index

 
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I look at a Map object (HashMap or Hashtable) in the Eclipse debugger, I see that the Map object maintains an internal table into which key/value pairs have been inserted. Is there any way to obtain the table index number for a specific key value?

The reason I ask is because the application I am currently working on creates large HashMaps. To aid in debugging, I created a class that takes a Map object and prints it out in alpha order. However, I would also like to be able to display the corresponding index into the Map table if that is possible.
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jay,

This seems wierd to me. I would think that an IDE would use reflection to determine the fields and methods in a particular object, but I just tried using reflection on a HashMap object to no avail.

So in the following code, the length of classes and fields arrays are both zero:



The fact that there are no fields makes me think that there shouldn't be anything visible to the IDE inside a HashMap object.

Can anyone add anything to this?

Tony
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony:

getFields() returns the public fields of a class and its superclasses. You want getDeclaredFields(), which lists all the fields in a given class (except the inherited ones.)

Jay:

The index in that array (and indeed, the size of the array itself) can change over time as the HashMap grows and is rehashed. The index isn't really meaningful to anyone but HashMap's implementers, so I don't know why you'd want to display it.

Furthermore, the name, type, and other information about that array -- or even the existence of the array at all -- can theoretically change in each JDK version, so I wouldn't get too comfy with any solution you come up with.

But in any case, looking at the JDK 1.4.2 source, there's this:



and there's this:



It's used like this:



So what you could do would be to get the length of the table array using reflection, and then these routines will tell you the index. You could put them into your code directly, or you could use reflection to call them in HashMap. It's your call.
 
Tony Jackson
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ernest.

Tony
 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

Thanks for the information. You're right, I don't really need to display the index. I just wanted to do so for sake of completeness (and also to be able to cross-reference the results of my class output to what I observe in the debugger).

Jay
 
Jay Damon
Ranch Hand
Posts: 282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest,

Thanks for your help. Here is one solution. Note that it only works for HashMaps not Hashtables.

 
She said she got a brazillian. I think owning people is wrong. That is how I learned ... tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic