I am trying to understand TreeSet.tailSet it was easy to implement but I am confused about the documentation :
public SortedSet tailSet(E fromElement) Description copied from interface: NavigableSet Returns a view of the portion of this set whose elements are greater than or equal to fromElement. The returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa. The returned set supports all optional set operations that this set supports. The returned set will throw an IllegalArgumentException on an attempt to insert an element outside its range.
https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html#tailSet(E)
it says
he returned set is backed by this set, so changes in the returned set are reflected in this set, and vice-versa
but i dont see it working on my code testing
Note : this is for my OCPJP 8 preparation.
Here is my code snippet
System.out.println(" 3 floor : " + persons.floor(new Person("3Mark Anthony", "Ortiz")) +
", 3 ceiling : " + persons.ceiling(new Person("3Mark Anthony", "Ortiz")));
System.out.println(" 5 higher : " + persons.higher(new Person("5Mark Anthony", "Ortiz")) );
SortedSet<Person> personSet = persons.tailSet(new Person("3Mark Anthony", "Ortiz"));
System.out.println(personSet.contains(new Person("3Mark Anthony", "Ortiz")));
Person p3 = personSet.first();
System.out.println(p3);
System.out.println(persons.floor(new Person("3Mark Anthony", "Ortiz")).equals(p3));
p3.firstName = "John Dave";
System.out.println(p3);
System.out.println(persons.floor(new Person("3Mark Anthony", "Ortiz")));
System.out.println(personSet.first());
persons.floor(new Person("3Mark Anthony", "Ortiz")).firstName = "Mike";
System.out.println(p3);
System.out.println(persons.floor(new Person("Mike", "Ortiz")));
and here is the output
3 floor : 2, 3 ceiling : 4
3 floor : [firstName:"3Mark Anthony", lastName: "Ortiz"], 3 ceiling : [firstName:"3Mark Anthony", lastName: "Ortiz"]
5 higher : [firstName:"6Mark Anthony", lastName: "Ortiz"]
true
[firstName:"3Mark Anthony", lastName: "Ortiz"]
true
[firstName:"John Dave", lastName: "Ortiz"]
[firstName:"2Mark Anthony", lastName: "Ortiz"]
[firstName:"John Dave", lastName: "Ortiz"]
[firstName:"John Dave", lastName: "Ortiz"]
[firstName:"Mike", lastName: "Ortiz"]
Now as you can see, I tried to update
p3.firstName = "John Dave";
and check it from its source Set object by
System.out.println(persons.floor(new Person("3Mark Anthony", "Ortiz")));
and still "3Mark Anthony" exist in the source set object. it was not update. and vice versa as you can see on the next line of codes.
Can someone help me understand what is wrong? I doubt javadoc missed it, it might be my understanding which is wrong.
PS. road to OCPJP 8