Treeset.contains() problem

后端 未结 3 1128
甜味超标
甜味超标 2021-02-14 13:57

So I\'ve been struggling with a problem for a while now, figured I might as well ask for help here.

I\'m adding Ticket objects to a TreeSet, Ticket implements Comparable

3条回答
  •  别那么骄傲
    2021-02-14 14:32

    This could happen if your compareTo method isn't consistent. I.e. if a.compareTo(b) > 0, then b.compareTo(a) must be < 0. And if a.compareTo(b) > 0 and b.compareTo(c) > 0, then a.compareTo(c) must be > 0. If those aren't true, TreeSet can get all confused.

提交回复
热议问题