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

前端 未结 4 1492
青春惊慌失措
青春惊慌失措 2021-01-30 18:29

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 strin

相关标签:
4条回答
  • 2021-01-30 18:41

    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.

    0 讨论(0)
  • 2021-01-30 18:49

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

    0 讨论(0)
  • 2021-01-30 19:00

    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

    0 讨论(0)
  • 2021-01-30 19:06

    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 讨论(0)
提交回复
热议问题