Partial ordered Comparator

后端 未结 6 1480
灰色年华
灰色年华 2020-12-18 22:35

How to implement java.util.Comparator that orders its elements according to a partial order relation?

For example given a partial order relation a

6条回答
  •  一生所求
    2020-12-18 22:54

    It seems to be more of an answer than a comment so I'll post it

    The documentation says:

    It follows immediately from the contract for compare that the quotient is an equivalence relation on S, and that the imposed ordering is a total order on S."

    So no, a Comparator requires a total ordering. If you implement this with a partial ordering you're breaching the interface contract.

    Even if it might work in some scenario, you should not attempt to solve your problem in a way that breaches the contract of the interface.

    See this question about data structures that do fit a partial ordering.

提交回复
热议问题