请看代码:
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 ?
求教

解决方案 »

  1.   

    楼上说的对
    map为键值对应,即一个键(key)只能对应一个值(value)。所以同一个key,无论你put多少次,也只有一个value,后面put的value会覆盖掉前面的value。
      

  2.   


    楼主不知道HashMap键(key)不能重复吗?除非相同的键(key)你生成不同的hashcode
      

  3.   

    public V put(K key,V value)
    在此映射中关联指定值与指定键。如果该映射以前包含了一个该键的映射关系,则旧值被替换。
      

  4.   

    你查查资料~  hashMap的key是不能重复的,value可以重复,根据key找到对应的value
      

  5.   

    map的存储方式为:key -- value的形式简单的说就像是 我们每个人身份证号 就是一个key,人就是vlaue。
    你想想会出现同一个身份证号代表两个人的吗?其实你这个循环只是对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不会出现重复的。希望能帮助你理解!
      

  6.   

    他那个三次试name,passwd,sex,和循环次数没关系 一个key对应一个value,key相同value则会覆盖前面的
      

  7.   


    你给的很有作用!            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>是什么呢 ?
      

  8.   

    Entry 实体中K 这个就是 key ,V 代表value
      

  9.   


    你给的很有作用!            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,但是在后续使用时我们要强制转换成我们想要的类型。不知道你是否明白,想要深入了解可以看看泛型的使用。
      

  10.   

    public class test {
        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是什么意思
      

  11.   

    public V put(K key, V value)
    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中已经说的很清楚了。
      

  12.   

    Entry<K,V>
    Entry 是Map的内部类,它是map元素的一个整体封装 包含:key,value信息。
    <K,V>是泛型。K代码某类型的key,V代表某类型的Value可以在api中搜索Map.Entry查找
      

  13.   

    同一个key的value值被覆盖了。楼主可以打印出来value。
      

  14.   

    确实如楼上各位所说,你循环一万次size永远都是3,它是通过键值对应来取值的,所以你循环多少它都只是覆盖了之前的值,好好理解一下吧。
      

  15.   

    自己跟踪调试下吧,watch一下map的值,会让你记得牢些
      

  16.   

    孩子,你用put传入数据map里面就传入三个,当然map.size()里面就是3了!
      

  17.   

    1楼已解释,key相同覆盖。建议用List存数据,这样就不会覆盖你的数据。
      

  18.   

    HashMap的key是不能重复的,不论你循环多少次,只是对value做了更新,key的数量是不会改变的
      

  19.   


    Entry<K,V>也不是工厂封装的方式,既然如此,为什么定义内部类用Entry<K,V>来处理 ?我自己设计的时候,从来只有对外不公开的时候,才会用内部类这种方式去保证内部创建机制的安全!可是,我没能理解HashMap<K,V>{...Entry<K,V>...}这种方式。
    请指教 !
    这是我问题的核心,至于原始问题,已不重要,我当时小白了一下!
      

  20.   

    HashMap为了要封装 key  和value值,所以它定义了一个内部类Entry对象来代表,
    key和value就是Entry的属性了,这样对于Map来说,它只要处理Entry就可以了,
    HashMap它内部做了许多算法来处理自己的事情.
    比如说key值重复时,它就替换原来的值.
    同时它也做了链表,来处理hash冲突的情况.
      

  21.   

    明白了就好,在网上被虐比在实际中被虐强呀,JDK帮助,baidu都是必须滴 呵呵