ordering a hashset example?

前端 未结 3 1229
一向
一向 2020-12-15 05:27

I need an example on how to use a comparable class on a HashSet to get an ascending order. Let’s say I have a HashSet like this one:



        
3条回答
  •  不知归路
    2020-12-15 06:14

    HashSets do not guarantee iteration order:

    This class implements the Set interface, backed by a hash table (actually a HashMap instance). It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element.

    You probably need to choose a different datastructure if you want to be able to control the iteration order (or indeed have one at all!)

提交回复
热议问题