Although it is true that you cannot rely on a specific order if the Map implementation does not explicitly define this, there is a proposal in the API documentation, which implies the existence of a single common order for the map and all its types of collection:
the display order is defined as the order in which iterators in the map collection views return their elements.
(my emphasis)
In order for this to be done, the map has its own order (even if it cannot be indicated and can change when the map is changed), and all collection views must correspond to this order. Regardless of whether this is a guarantee, and in particular, whether all third-party card implementations will be respected, this is another question.
It is also worth noting that they are explicitly defined in the Map interface as representations that are supported by the map (for example, if you remove an element from keySet , the corresponding Map must be removed from the map). This means that in reality it is less likely that you will get different orders from the correct Map implementation than it would be if you made small copies of the collection views.
Having said all this, if the question is: "Is this a safe refactor?" then the answer is yes, until the source code breaks itself. "If the method depends on a specific order and, therefore, on a specific implementation of Map , the method should be declared to accept only this type of Map . Otherwise, you have a potential temporary bomb if the base Map implementation will change down the line (and I saw how a real software crash occurred due to this with the JDK update).
If a particular caller relies on a specific order because he knows that he is transmitting an ordered implementation of Map , thatβs fine, and that order will be saved after your refactor.
source share