-4

I've had this question for a very very long time.

Question is a bit long. Please bear with me :)

In Summary, how does a collection datastructure such as TreeSet know when the underlying data it stores gets modified and how does it manage such cases??

Example

//Simple person class with name data member
public static class Person {
    String name;

    public Person(String name) {
        this.name = name;
    }
}


1. Create a TreeSet and add 3 Person instances p1, p2, p3. (Comparator sorts name).

TreeSet<Person> set = new TreeSet<>(new Comparator<Person>() {
    @Override
    public int compare(Person person1, Person person2) {
        return person1.name.compareTo(person2.name);
    }
});

// Creating 3 Person instances and adding to set.
Person p1 = new Person("Zach"),
        p2 = new Person("Henry"),
        p3 = new Person("Adam");

// Adding to set
set.add(p1);  set.add(p2);  set.add(p3);


2. Printing the First Element (Prints the lexicographical smallest string in balanced BST)

// This will name of P3 instance, i.e. Adam (obvious and expected) 
System.out.println(set.first().name);
// "Adam" is printed which is expected.


3. Modifying the Person Instance P3 to have "zebra" name. i.e. Adam -> Zebra

p3.name = "Zebra";

System.out.println(set.first().name);


QUESTION
In Section 3, I modified p3 instance to hold "Zebra" instead of "Adam".
Question is, How did TreeSet know that the P3 instance has been modified???

TreeSet is built using some Balanced BST (RB Trees usually). Hence, when I change some data, It has to reorder the internal nodes of the Tree to maintain adherence with the rules of the Comparator.
So, how did the TreeSet get notified that the underlying data has been modified and that it has to reorder tree nodes again???

I really want to know how does it work internally. Is it observer pattern? Requesting a bit elaborate and comprehensive answers :)

Adithya Upadhya
  • 2,239
  • 20
  • 28
  • 2
    Have you looked at the source code? It's readily available and easy to find -- isn't that where you should look first? – Hovercraft Full Of Eels Sep 03 '17 at 15:14
  • 1
    I've run your test, and it does NOT print Henry. It prints Zebra, as I expected it. Just running your own test would have given you the answer: it doesn't know about the changes, and it's a big to change the state of an object after it's been stored in a Set. – JB Nizet Sep 03 '17 at 15:18
  • [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) The answer, *"A lot. An absurd amount. More than you think you are capable of. After you have reached the end of your rope and the pain of not having the answer exceeds the vast amount of shame received by posting your question, that's when you can go ahead and ask. "* **Looking at the source would have taken less time than writing this question!** –  Sep 03 '17 at 15:25
  • Guys, I am really sorry for posting a wrong question with wrong content. I remember testing a similar code and finding a similar anomaly. Rest Assured : I will be a lot more careful next time. Thank you for your efforts and suggestions :) I deserve the down-votes and I accept it with humility. – Adithya Upadhya Sep 03 '17 at 15:32

1 Answers1

1

How does a collection datastructure such as TreeMap know when the underlying data it stores gets modified and how does it manage such cases?

It doesn't. For values it doesn't matter, but if you modify keys it can corrupt the whole collection. This is why you should prefer immutable keys for maps, or at least make sure they're not modified after being used as keys.

Note also that TreeSet is backed by a TreeMap, and HashSet is backed by a HashMap, so the same goes with them (with the set values being the map keys).

Kayaman
  • 72,141
  • 5
  • 83
  • 121