Eclipse debugging HashMap: Logical Structure using Key and Value's toString() method

23,546

Solution 1

I find that when I select a value in the "Variables" pane in the debugger, its value is shown below using the toString() method. This works nicely for maps, for example.

Solution 2

You need to create a detail formatter on top of the logical structure. In the example screenshot you provided, your logical structure is ConcurrentHashMap$WriteThroughEntry. You can add the detail formatter by right clicking on a row containing a ConcurrentHashMap$WriteThroughEntry and selecting 'add detail formatter'.

I just knocked up this example using HashMap.

enter image description here

java.util.HashMap$Entry //  key + " - " + value

For a

HashMap<Integer,String> map;

.. I quickly populated with rubbish, I now see:

enter image description here

Solution 3

Well I made an ugly workaround. Set up this detail formatter for Map:

StringBuilder detail = new StringBuilder();
int i = 0;
for (Object k : keySet()) {
    detail.append((i++) + ": " + k + "\n");
}
return detail;

Then in the output of that, find the index of the entry with the key you want, then find that entry in the logical structure tree.

It works, but the detail formatter itself is a bit slow, and it requires an extra step. Also the keys are not sorted so finding the key you want in a large map may be difficult (though sorting could theoretically be done in the formatter).

Solution 4

Just use the Show logical structure under the expressions window to make the values more readable. check this out https://blog.codecentric.de/en/2013/04/again-10-tips-on-java-debugging-with-eclipse/

Share:
23,546
ltfishie
Author by

ltfishie

Updated on July 09, 2022

Comments

  • ltfishie
    ltfishie almost 2 years

    I have recently started to use Eclipse after using IntelliJ for a few years. When debugging Map using IntelliJ, if the key or object implements toString(), a nice list of string representation of key-value is displayed.

    In Eclipse, when I select Show Logical Structure, I see something like the following:

    enter image description here

    The problem with this view is that you will need to expand each entry to see the actual key and value. If you need to find something in a map of more than 10 elements, it becomes very tedious.

    I understand that you can make custom Logical Structure and the default for Map look the this:

    return entrySet().toArray();
    

    Is there any way, either through custom Logical Structure or plugin to view Map Entries more useful than

    ConcurrentHashMap$WriteThroughEntry (id=193)
    
  • ltfishie
    ltfishie over 12 years
    Thank you, I am aware I can expand each MapEntry to see the key and value. However, this is very tedious if you want to locate an entry in a map of any reasonable size.
  • ltfishie
    ltfishie over 12 years
    Can you please remove your answer since it does not address my question?
  • Mike Sokolov
    Mike Sokolov over 12 years
    I'm sorry, but I don't think you understood my answer. If you simply select (ie click on it, making it be highlighted), the value you want to inspect, its toString() is shown in a nearby panel - no expanding required.
  • ltfishie
    ltfishie over 12 years
    Thanks for clarify, you are right. The window right below the variables panel shows the string equivalent...
  • sharakan
    sharakan about 12 years
    Interestingly, this doesn't work on OS X in eclipse 3.7, I think because of Apple's JVM. I get an error when I try to define a detail formatter for an inner class. But this is how to do it. Note that you can minimize the changes to the variables view by selecting the first radio button.
  • A.H.
    A.H. about 12 years
    Please make the screenshot of the "Variables" view a little larger and encircle the "Show logical Structure" button.
  • Bart van Heukelom
    Bart van Heukelom about 12 years
    Although it's not perfect (the keys are not sorted), this works well enough, so here's the bounty.
  • Gábor Lipták
    Gábor Lipták about 9 years
    Thanks. Works fine. +1
  • jpprade
    jpprade almost 9 years
    for me it worked like this : getKey() + " - " + getValue() otherwise I had an error : Detail formatter error: <key> cannot be resolved to a variable
  • Keith Tyler
    Keith Tyler almost 5 years
    For very large hashtables this is no good because the displayed value is truncated at 10,000 chars.