* @return the previous value associated with <tt>key</tt>, or * <tt>null</tt> if there was no mapping for <tt>key</tt>. * (A <tt>null</tt> return can also indicate that the map * previously associated <tt>null</tt> with <tt>key</tt>, * if the implementation supports <tt>null</tt> values.) 这是人家接口定义的声明,如果你不按照这个声明来,说明你实现是不符合该接口要求的
从未关注过map put时候的返回值 刚看了下源码,还没有得到答案 或许正如一楼大神所说,只有去问map接口的作者 * @param <K> the type of keys maintained by this map * @param <V> the type of mapped values * * @author Josh Bloch 了
首先, 你put之前, 肯定是有那个新值的, 返回的是旧值, 这样如果你有对比新旧两个值之间的差异, 那肯定就用到了... 如果按照你说的写成只返回一个boolean值, 那如果有之前说的需求, 是不是还要自己去重写, 或者最初的设计者是不是还要写另外一个方法呢?我用java开发了好几年, 说实话还真没用过这个返回值, 但不代表这样设计没有意义. 设计出来的东西只有好和更好, 我觉得这个方法写的比你说的方案更好, 更能处理更多的情况.3. 你看的类肯定不是map, 因为java里面 map 是一个interface... 别的类既然实现了map这个接口, 那肯定要重写这个方法的啊....
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
* (A <tt>null</tt> return can also indicate that the map
* previously associated <tt>null</tt> with <tt>key</tt>,
* if the implementation supports <tt>null</tt> values.)
这是人家接口定义的声明,如果你不按照这个声明来,说明你实现是不符合该接口要求的
刚看了下源码,还没有得到答案
或许正如一楼大神所说,只有去问map接口的作者
* @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
*
* @author Josh Bloch
了