我试图理解TreeSet.tailSet它很容易实现,但我对文档感到困惑:
public SortedSet tailSet(E fromElement) 从interface:NavigableSet复制的描述 返回此set的部分视图,其元素大于或等于fromElement。返回的集由此集支持,因此返回集中的更改将反映在此集中,反之亦然。返回的集支持此集支持的所有可选集操作。 返回的集将在尝试插入超出其范围的元素时抛出IllegalArgumentException。
https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html#tailSet(E)
它说
他返回的集由此集支持,因此返回集中的更改将反映在此集中,反之亦然
但我认为它不能用于我的代码测试
注意:这是我的OCPJP 8准备工作。
这是我的代码段
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")));
这是输出
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"]
现在你可以看到,我试图更新
p3.firstName = "John Dave";
并通过
从源Set对象中检查它System.out.println(persons.floor(new Person("3Mark Anthony", "Ortiz")));
并且仍然存在“3Mark Anthony”存在于源集对象中。它没有更新。反之亦然,你可以在下一行代码中看到。
有人能帮我理解什么是错的吗?我怀疑javadoc错过了它,可能是我的理解错了。
PS。通往OCPJP 8的道路