Java docs says that a “TreeSet keeps its elements ordered internally”.
Here what does ordering mean? Does it mean sorted? If so, then what is the difference between sorting and ordering?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
“Ordered” means that there is a defined order in which the elements can be retrieved. This means that when you iterate over the Collection, you know in which order you will get the elements.
In this case it does mean “sorted” (because the TreeSet arranges elements according to a given Comparator, i.e. it sorts them).
In other cases (such as a Queue), it can mean “insertion order”.
In the case of a List, you can specify the order yourself (by assigning each element an index), and the List will keep them in this order (which is independent of insertion order or any sort order).
Contrast this to a HashSet, which makes no guarantees about the order of elements when retrieved.