collections - java.lang.IllegalArgumentException: Comparison method violates its general contract (not reproducible) -
this question has answer here:
i trying reproduce exception (java.lang.illegalargumentexception: comparison method violates general contract!) need debug piece of code, code below never throws it,
try { arraylist al = new arraylist(); (int = 1; <= 36; i++) { typeadaptersort t = new typeadaptersort(); t.order = i; al.add(t); } system.out.println(al.size()); collections.sort(al, new comparator() { public int compare(object o1, object o2) { typeadaptersort tas1 = (typeadaptersort) o1; typeadaptersort tas2 = (typeadaptersort) o2; if (tas1.order < tas2.order) return -1; else return 1; } }); } catch (exception e) { system.out.println(e); } also, when checked jdk code seems exception thrown collections.sort method when size of collection sorted greater 32?. change should made in code block collections.sort throws exception.
this problem
if (tas1.order < tas2.order) return -1; else return 1; if order equal you'll different result depending on goes comparator first, not right.
the contract if < b , b < c < c, in case can broken depending order arguments passed.
try like
return tas1.order -tas2.order; this explains why can't reproduce test data never has duplicates. try adding dupes in test data , see if can reproduce (before applying fix obviously...)
Comments
Post a Comment