String s1 = new String("zhaoxudong");
        Map table  = new Hashtable() ;
        Map map = new HashMap() ;
        map.put("user", s1 ) ;
        table.put("user", s1 ) ;
        System.out.println(table.equals(map1));结果为true,为什么呀?

解决方案 »

  1.   


        public synchronized boolean equals(Object o) {
    if (o == this)
        return true; if (!(o instanceof Map))
        return false;
    Map<K,V> t = (Map<K,V>) o;
    if (t.size() != size())
        return false;        try {
                Iterator<Map.Entry<K,V>> i = entrySet().iterator();
                while (i.hasNext()) {
                    Map.Entry<K,V> e = i.next();
                    K key = e.getKey();
                    V value = e.getValue();
                    if (value == null) {
                        if (!(t.get(key)==null && t.containsKey(key)))
                            return false;
                    } else {
                        if (!value.equals(t.get(key)))
                            return false;
                    }
                }
            } catch (ClassCastException unused)   {
                return false;
            } catch (NullPointerException unused) {
                return false;
            } return true;
        }上面一断代码是Hashtable的equals方法,看看就知道只要内部元素都equals,并且是map类型,那么两个map就equals
      

  2.   

    哦,谢了。没认真看Hashtable的equals方法