亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Collections.sort報錯,大神求分析、求答案

Collections.sort報錯,大神求分析、求答案

千萬里不及你 2018-07-24 11:14:22
 Collections.sort(lcd1, new Comparator<CouponDto>(){                     @Override                     public int compare(CouponDto o1, CouponDto o2) {                             if(o1.getCoupon().getListOrder2() > o2.getCoupon().getListOrder2()){                                 return -1;                             }                             if(o1.getCoupon().getListOrder2() == o2.getCoupon().getListOrder2()){                                 return 0;                             }                             return  1;                     };                 });     報Comparison method violates its general contract異常             改成     Collections.sort(lcd1, new Comparator<CouponDto>(){                     @Override                     public int compare(CouponDto o1, CouponDto o2) {                             return -(o1.getCoupon().getListOrder2() - o2.getCoupon().getListOrder2());                     };                 });             就不再報錯;             各位大神,這個是怎么回事
查看完整描述

1 回答

?
不負相思意

TA貢獻1777條經驗 獲得超10個贊

你的代碼雖然滿足了自反性,但是不滿足偏序。jdk 1.7還要求偏序性。
比如a = 10 b = 8 c = 4,那么你的代碼出現 comp(a,b) == comp(a, c),sort會假設b=c但是實際上是b>c



查看完整回答
反對 回復 2018-08-12
  • 1 回答
  • 0 關注
  • 1226 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號