2

Intellij IDEA debugger

Hi all!

I tried to debug TreeSet<String>/TreeMap<String> but found that debugger for some reason doesn't want to show me parent and left/right children of the nodes.

I expected to see nested nodes but only key/values are exposed. Of course, if I evaluate this.left/this.right it will give me the child.

Is it IDEA bug? Why does it show only key and value?

Also, I noticed that when we get elements from client code we receive String instead of TreeMap.Entry. What should we do in order to get a parent/left/right of the current item in client code?

Pasha
  • 1,768
  • 6
  • 22
  • 43
  • 1
    "What should we do in order to get a parent/left/right of the current item in client code?" This is _very deliberately_ not possible, to preserve flexibility in the implementation. – Louis Wasserman Sep 21 '18 at 17:26
  • This is how the code was compiled. Some local slots don't have debug information. It has inferred that slot 3 is `t` so you can see everything. – Peter Lawrey Sep 21 '18 at 17:34

2 Answers2

3

You’re using a TreeMap so you have key/values and it’s sorted by keys as if all the keys were in a TreeSet and a TreeSet does not refer to a binary tree with parent / left / rigth like

   parent
     |
   node
  /    \
left  right

A TreeSet is an ordered set following rules as natural order or a custom comparator


TreeSet Documentation : A NavigableSet implementation based on a TreeMap. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

So a TreeMap is a TreeSet where you just add a value to each key

azro
  • 53,056
  • 7
  • 34
  • 70
1

Some IDEs have an option to display a collection's contents in a more "friendly" way instead of displaying the collection's internal data structures. It's probably this way because most programmers are using collections but not debugging them, and therefore seeing the collection's internal structures obscures the collection's contents.

In the NetBeans, this option can be changed by going to Preferences in the Java > Java Debugger category, and setting various "formatters" such as those for Collection, Map, and Map.Entry.

In IntelliJ IDEA, bring up the Settings dialog, and look under Build, Execution, Deployment -> Debugger -> Data Views -> Java. The option is called "Enable alternative view for Collections classes." (Thanks, Matt Leidholm!)

As to the second part of your question, if you're using a TreeMap<K, V> the API exposes only the types K and V that you're using. The TreeMap.Entry structure is an internal data structure that's kept private from the client. The client can't navigate the tree structure (left/right/parent) itself. The intent of TreeMap is not to expose a tree structure, but to provide an abstraction of a map whose keys and values are exposed, sorted by key.

You can get Map.Entry references by calling entrySet(). These are references to actual TreeMap.Entry objects. However, the Map.Entry doesn't expose left/right/parent; it only exposes keys and values.

Stuart Marks
  • 127,867
  • 37
  • 205
  • 259
  • 1
    In IntelliJ IDEA, the preference you are talking about is in the Settings dialog, under Build, Execution, Deployment -> Debugger -> Data Views -> Java. It is called "Enable alternative view for Collections classes". – Matthew Leidholm Sep 21 '18 at 19:22
  • @MattLeidholm Thank you! I figured IDEA would have something equivalent. – Stuart Marks Sep 22 '18 at 01:07