Java has ordered, unordered, and sorted collections. How are they different?
Sorted stores items in ascending order, for example SortedSet . Ordered store items in a specific order, not necessarily in ascending order, for example List .
How are items stored in unordered collections? In fact, there is also some sort of order there, they do not mix up after adding each element.

  • It is mixed. Not after each addition - but sometimes mixed. - Pavel Mayorov
  • @Pavel Mayorov, I wonder, for what purpose? - jisecayeyo
  • 2
    @jisecayeyo, it's not about the goal, but about the features of the internal structure of a particular data structure. The hashmap partially redistributes the values ​​in the baskets after increasing its size. - Nofate

1 answer 1

In disordered collections, order (namely, mathematical order) is not guaranteed due to the nature of storage.

HashSet , for example, is internally implemented on the basis of a HashMap . Somehow it can be bypassed (by taking an iterator ), but there will be no deeper order of the bypass order (since it will depend on the size of the hashmap and the implementation of Object.hashCode() ).

Unordered sets are primarily needed for quick checks on the occurrence of an object in this set, and not to bypass elements.