开发者

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

开发者 https://www.devze.com 2023-04-01 03:54 出处:网络
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-v

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:

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

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)


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.

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

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

For a

HashMap<Integer,String> map;

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

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


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).


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.


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消