首页 > 一个关于Java中SortedMap定义的问题

一个关于Java中SortedMap定义的问题

一个关于Java中SortedMap定义的问题

在SortedMap接口的解释中有这么一段话:

 * <p>Note that the ordering maintained by a sorted map (whether or not an
 * explicit comparator is provided) must be <i>consistent with equals</i> if
 * the sorted map is to correctly implement the <tt>Map</tt> interface.  (See
 * the <tt>Comparable</tt> interface or <tt>Comparator</tt> interface for a
 * precise definition of <i>consistent with equals</i>.)  This is so because
 * the <tt>Map</tt> interface is defined in terms of the <tt>equals</tt>
 * operation, but a sorted map performs all key comparisons using its
 * <tt>compareTo</tt> (or <tt>compare</tt>) 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 <i>is</i> well-defined even if its
 * ordering is inconsistent with equals; it just fails to obey the general
 * contract of the <tt>Map</tt> interface.

请问这段话是什么意思?


就是要求map里面的对象,做compareTo和equals来比较的时候结果是一致的。比如compareTo返回0,那么equal一定是true。但有的对象可不是这样的,比如BigDecimal

【热门文章】
【热门文章】