Java – is sorting by a non transitive comparator “working”?

If I report to collections What happens if sort provides a non transitive comparator? Can I enter an infinite cycle?

A little test I wrote produced an output, but I want to make sure that this will always be the case

The problem is that in some cases, my comparator can generate cycles. In this case, I just want to ensure that it does not enter an infinite loop I don't care about the actual results

Solution

Java docs says you have to make sure the comparator is transitive If you provide a comparator that does not meet the requirements, all bets will be turned off It may work for a given implementation, but it may crash in another instance (STD:: sort in C)

In short, you should not rely on it to work, even if it is for some or other examples

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>