“Comparison method violates its general contract!”

前端 未结 11 2083
梦如初夏
梦如初夏 2020-11-21 06:07

Can someone explain me in simple terms, why does this code throw an exception, \"Comparison method violates its general contract!\", and how do I fix it?

pri         


        
11条回答
  •  星月不相逢
    2020-11-21 06:43

    Java does not check consistency in a strict sense, only notifies you if it runs into serious trouble. Also it does not give you much information from the error.

    I was puzzled with what's happening in my sorter and made a strict consistencyChecker, maybe this will help you:

    /**
     * @param dailyReports
     * @param comparator
     */
    public static  void checkConsitency(final List dailyReports, final Comparator comparator) {
      final Map> objectMapSmallerOnes = new HashMap>();
    
      iterateDistinctPairs(dailyReports.iterator(), new IPairIteratorCallback() {
        /**
         * @param o1
         * @param o2
         */
        @Override
        public void pair(T o1, T o2) {
          final int diff = comparator.compare(o1, o2);
          if (diff < Compare.EQUAL) {
            checkConsistency(objectMapSmallerOnes, o1, o2);
            getListSafely(objectMapSmallerOnes, o2).add(o1);
          } else if (Compare.EQUAL < diff) {
            checkConsistency(objectMapSmallerOnes, o2, o1);
            getListSafely(objectMapSmallerOnes, o1).add(o2);
          } else {
            throw new IllegalStateException("Equals not expected?");
          }
        }
      });
    }
    
    /**
     * @param objectMapSmallerOnes
     * @param o1
     * @param o2
     */
    static  void checkConsistency(final Map> objectMapSmallerOnes, T o1, T o2) {
      final List smallerThan = objectMapSmallerOnes.get(o1);
    
      if (smallerThan != null) {
        for (final T o : smallerThan) {
          if (o == o2) {
            throw new IllegalStateException(o2 + "  cannot be smaller than " + o1 + " if it's supposed to be vice versa.");
          }
          checkConsistency(objectMapSmallerOnes, o, o2);
        }
      }
    }
    
    /**
     * @param keyMapValues 
     * @param key 
     * @param  
     * @param  
     * @return List
     */ 
    public static  List getListSafely(Map> keyMapValues, Key key) {
      List values = keyMapValues.get(key);
    
      if (values == null) {
        keyMapValues.put(key, values = new LinkedList());
      }
    
      return values;
    }
    
    /**
     * @author Oku
     *
     * @param 
     */
    public interface IPairIteratorCallback {
      /**
       * @param o1
       * @param o2
       */
      void pair(T o1, T o2);
    }
    
    /**
     * 
     * Iterates through each distinct unordered pair formed by the elements of a given iterator
     *
     * @param it
     * @param callback
     */
    public static  void iterateDistinctPairs(final Iterator it, IPairIteratorCallback callback) {
      List list = Convert.toMinimumArrayList(new Iterable() {
    
        @Override
        public Iterator iterator() {
          return it;
        }
    
      });
    
      for (int outerIndex = 0; outerIndex < list.size() - 1; outerIndex++) {
        for (int innerIndex = outerIndex + 1; innerIndex < list.size(); innerIndex++) {
          callback.pair(list.get(outerIndex), list.get(innerIndex));
        }
      }
    }
    

提交回复
热议问题