请看代码:
public class test {
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<String, String>();
System.out.println(map.size());
for(int i =0 ; i<4 ; i++){
map.put("name", "zxw"+i);
map.put("passwd", (i<<2+i<<1+i)+"");
map.put("sex", "男");
System.out.println(map.get("name"));
}
System.out.println(map.size());
}
}我循环了4次,问什么map.size()却等于3 ?
求教
public class test {
public static void main(String[] args) {
HashMap<String,String> map = new HashMap<String, String>();
System.out.println(map.size());
for(int i =0 ; i<4 ; i++){
map.put("name", "zxw"+i);
map.put("passwd", (i<<2+i<<1+i)+"");
map.put("sex", "男");
System.out.println(map.get("name"));
}
System.out.println(map.size());
}
}我循环了4次,问什么map.size()却等于3 ?
求教
map为键值对应,即一个键(key)只能对应一个值(value)。所以同一个key,无论你put多少次,也只有一个value,后面put的value会覆盖掉前面的value。
楼主不知道HashMap键(key)不能重复吗?除非相同的键(key)你生成不同的hashcode
在此映射中关联指定值与指定键。如果该映射以前包含了一个该键的映射关系,则旧值被替换。
你想想会出现同一个身份证号代表两个人的吗?其实你这个循环只是对map集合中数据的一个刷新。Hashmap源码:
/**
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for the key, the old
* value is replaced.
*
* @param key key with which the specified value is to be associated
* @param value value to be associated with the specified key
* @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>.)
*/
public V put(K key, V value) {
if (key == null)
return putForNullKey(value);
int hash = hash(key.hashCode());
int i = indexFor(hash, table.length);
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
Object k;
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
} modCount++;
addEntry(hash, key, value, i);
return null;
}可以看到源码中的注释:
If the map previously contained a mapping for the key, the old value is replaced.
如果map中key已经存在,那么key原来的value将会被替换。
也就是说,我们的key不会出现重复的。希望能帮助你理解!
你给的很有作用! if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
不过,我这里要有一个疑问,
for (Entry<K,V> e = table[i]; e != null; e = e.next)
这个for的初始化条件是Entry<K,V> e,这里的Entry<K,V>是什么呢 ?
你给的很有作用! if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
V oldValue = e.value;
e.value = value;
e.recordAccess(this);
return oldValue;
}
不过,我这里要有一个疑问,
for (Entry<K,V> e = table[i]; e != null; e = e.next)
这个for的初始化条件是Entry<K,V> e,这里的Entry<K,V>是什么呢 ? * @param <K> the type of keys maintained by this map
* @param <V> the type of mapped values
这里的K 代表是 key的类型,V代表是value的类型。
你想想HashMap里面可能存储各种类型的值,那么JDK就为我们引入了个泛型的概念,就是我们在实例化一个map时可以这么写:
Map<String, String> map = new HashMap<String, String>();
当然也可以不写,jdk会默认key value都为object,但是在后续使用时我们要强制转换成我们想要的类型。不知道你是否明白,想要深入了解可以看看泛型的使用。
public static void main(String[] args) {
List result=new ArrayList();
HashMap<String,String> map = new HashMap<String, String>();
System.out.println(map.size());
for(int i =0 ; i<4 ; i++){
map.put("name"+i, "zxw"+i);
map.put("passwd"+i, (i<<2+i<<1+i)+"");
map.put("sex"+i, "男");
result.add(map);
}
System.out.println(result.size());
}
}结果就是4了 楼主你没理解map是什么意思
Associates the specified value with the specified key in this map. If the map previously contained a mapping for the key, the old value is replaced. JDK的api中已经说的很清楚了。
Entry 是Map的内部类,它是map元素的一个整体封装 包含:key,value信息。
<K,V>是泛型。K代码某类型的key,V代表某类型的Value可以在api中搜索Map.Entry查找
Entry<K,V>也不是工厂封装的方式,既然如此,为什么定义内部类用Entry<K,V>来处理 ?我自己设计的时候,从来只有对外不公开的时候,才会用内部类这种方式去保证内部创建机制的安全!可是,我没能理解HashMap<K,V>{...Entry<K,V>...}这种方式。
请指教 !
这是我问题的核心,至于原始问题,已不重要,我当时小白了一下!
key和value就是Entry的属性了,这样对于Map来说,它只要处理Entry就可以了,
HashMap它内部做了许多算法来处理自己的事情.
比如说key值重复时,它就替换原来的值.
同时它也做了链表,来处理hash冲突的情况.