Eclipse debugging HashMap: Logical Structure using Key and Value's toString() method
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.
java.util.HashMap$Entry // key + " - " + value
For a
HashMap<Integer,String> map;
.. I quickly populated with rubbish, I now see:
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/
ltfishie
Updated on July 09, 2022Comments
-
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:
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 over 12 yearsThank 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 over 12 yearsCan you please remove your answer since it does not address my question?
-
Mike Sokolov over 12 yearsI'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 over 12 yearsThanks for clarify, you are right. The window right below the variables panel shows the string equivalent...
-
sharakan about 12 yearsInterestingly, 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. about 12 yearsPlease make the screenshot of the "Variables" view a little larger and encircle the "Show logical Structure" button.
-
Bart van Heukelom about 12 yearsAlthough it's not perfect (the keys are not sorted), this works well enough, so here's the bounty.
-
Gábor Lipták about 9 yearsThanks. Works fine. +1
-
jpprade almost 9 yearsfor me it worked like this : getKey() + " - " + getValue() otherwise I had an error : Detail formatter error: <key> cannot be resolved to a variable
-
Keith Tyler almost 5 yearsFor very large hashtables this is no good because the displayed value is truncated at 10,000 chars.