Java Collections 07 - TreeSet in java | Java TreeSet sorting with example | Java9s.com

preview_player
Показать описание
TreeSet is the concrete implementation of NavigableSet, SortedSet and Set interfaces.
Treeset is the collection which sorts the elements and maintains that order. TreeSet implements SortedSet, NavigableSet.
Any collection implementation that implements sorted set should sort the elements when they are getting added to the collection.
That means when we fetch the elements from collection they are already sorted.
These are the methods that are declared by SortedSet interface.
you can see that there is no sort method defined here.
That means any sorted set implementation like TreeSet is supposed to sort the elements when we add them to the collection.
you can notice that the methods present in sorted set interface are related to retrieving elements.
The methods first and Last help to get the first and last elements respectively. then we have headset and tailset methods. let me elaborate about these methods.
headSet returns all the elements that are on top of the specified elements
tailSet method will fetch all the elements below the specified element and return a set.
NavigableSet defines the utility methods which can help to navigate through the collection and help to fetch the closest match.
If you take a look at the methods that NavigableSet defines, you see that there is
Cieling method which returns least element in the set with respect to the given element,
Then we have
Floor method which gives the greatest element in the set with respect to the element passed.
Рекомендации по теме
Комментарии
Автор

What you post at 9:21 is a bit misleading. ceiling() returns the least element in the set that is >= the value you give, floor() returns the greatest element in the set that is <= to the value that you give. higher() and lower() are similar but won't include the element, i.e. they will only give you > and < respectively, and null if there is no such element in your set at the time, they will never give back the same value. The example code execution shows this correctly.

jvsnyc
Автор

Query: how would 'c' in sysout return 'Registration number' ?

saiganesh
Автор

One terminology thing. If you say "Before" and "After" instead of "Below" and "Above" then you can use the same terms whether you are sorting ascending or descending order. For example, if I am sorting descending, then all items with a value Below 50 will come Before, and all items with a value Above 50 will come After. If you keep using the terms Below and Above only, it is very confusing when you change from ascending to descending order.

jvsnyc
Автор

Another detail you got wrong. At 1:36 you correctly show the documentation which shows that headSet() gives you a subset containing everything strictly BEFORE the value you specify, which does NOT include your value. tailSet() gives you a subset containing your value, if it is present, plus everything strictly AFTER the value you specify. tailSet() will always also contain the value you specify if it is present. We actually see this at 8:08. Still a good video, I think that is the only incorrect detail.

jvsnyc
welcome to shbcf.ru