一个关于Java中SortedMap定义的问题
在SortedMap接口的解释中有这么一段话:
*Note that the ordering maintained by a sorted map (whether or not an * explicit comparator is provided) must be consistent with equals if * the sorted map is to correctly implement the Map interface. (See * the Comparable interface or Comparator interface for a * precise definition of consistent with equals.) This is so because * the Map interface is defined in terms of the equals * operation, but a sorted map performs all key comparisons using its * compareTo (or compare) method, so two keys that are * deemed equal by this method are, from the standpoint of the sorted map, * equal. The behavior of a tree map is well-defined even if its * ordering is inconsistent with equals; it just fails to obey the general * contract of the Map interface.
请问这段话是什么意思?
就是要求map里面的对象,做compareTo和equals来比较的时候结果是一致的。比如compareTo返回0,那么equal一定是true。但有的对象可不是这样的,比如BigDecimal