Angular_Dracula
2016-03-03 16:24
提个问题: item实体类中重写的的equals方法不知道哪里有掉用过,,,
public boolean addGoodsInCart(items item, int number) {
if (goods.containsKey(item)) {
goods.put(item, goods.get(item) + number);
} else {
goods.put(item, number);
}
calTotalPrice();
return true;
}map和set集合比较是否包含某一个元素,需要重写equals和hashcode方法。
list集合比较是否包含某一个元素,需要重写equals方法。
建议再好好学习下泛型,基础知识不够牢固。
如果不重新写,goods会把两个同样的item给添加进来。因为这两个item虽然属性相同但其实是不同的对象。
final Node<K,V> getNode(int hash, Object key) {
Node<K,V>[] tab; Node<K,V> first, e; int n; K k;
if ((tab = table) != null && (n = tab.length) > 0 &&
(first = tab[(n - 1) & hash]) != null) {
if (first.hash == hash && // always check first node
((k = first.key) == key || (key != null && key.equals(k))))
return first;
if ((e = first.next) != null) {
if (first instanceof TreeNode)
return ((TreeNode<K,V>)first).getTreeNode(hash, key);
do {
if (e.hash == hash &&
((k = e.key) == key || (key != null && key.equals(k))))
return e;
} while ((e = e.next) != null);
}
}
return null;
}
/**
* Returns <tt>true</tt> if this map contains a mapping for the
* specified key.
*
* @param key The key whose presence in this map is to be tested
* @return <tt>true</tt> if this map contains a mapping for the specified
* key.
*/
public boolean containsKey(Object key) {
return getNode(hash(key), key) != null;
}JAVA遇见HTML——Servlet篇
160608 学习 · 1093 问题
相似问题