【问题标题】:Values() of HashMap returning incorrect output [duplicate]HashMap的Values()返回不正确的输出[重复]
【发布时间】:2017-07-20 09:40:25
【问题描述】:

据我了解,以下代码在两种情况下都应打印true,因为有些值没有改变。

但是,当我运行以下代码时,它正在打印 truefalse

   public class Test {
    public static void main(String[] args) {
        HashMap<String, Boolean> map1 = new HashMap<String, Boolean>();
        HashMap<String, Boolean> map2 = new HashMap<String, Boolean>();

        map1.put("true", Boolean.TRUE);
        map1.put("false", Boolean.FALSE);

        map2.put("true", Boolean.TRUE);
        map2.put("false", Boolean.FALSE);

        System.out.println(map1.equals(map2)); // prints true as expected

        // prints false even though the values are indeed equal:
        System.out.println(map1.values().equals(map2.values()));
    }

}

谁能帮我理解为什么它在第二种情况下打印false

【问题讨论】:

  • map.values() 返回一个Collection。你对它的实现一无所知。因此,你不能仅仅因为里面的元素相等就期望它返回 true。
  • 原因是返回的values没有实现equals()。无论使用什么版本的 equals 都不认为你的两个地图的值集合是相等的。

标签: java


【解决方案1】:
 System.out.println(map1.equals(map2)); // prints true as expected

这是因为HashMap's equals() method overiden 这样的方式是检查键和值。

 System.out.println(map1.values().equals(map2.values()));// false

当您执行 .values() 时,您将获得一个 Collection 对象,该对象不会超越 equals 并因此是默认行为。

【讨论】:

    【解决方案2】:

    HashMap.values() 返回Collection 的实现,它不会覆盖Objectequals,因此比较返回false,因为默认实现比较引用。

    final class Values extends AbstractCollection<V> {
        public final int size()                 { return size; }
        public final void clear()               { HashMap.this.clear(); }
        public final Iterator<V> iterator()     { return new ValueIterator(); }
        public final boolean contains(Object o) { return containsValue(o); }
        public final Spliterator<V> spliterator() {
            return new ValueSpliterator<>(HashMap.this, 0, -1, 0, 0);
        }
        public final void forEach(Consumer<? super V> action) {
            Node<K,V>[] tab;
            if (action == null)
                throw new NullPointerException();
            if (size > 0 && (tab = table) != null) {
                int mc = modCount;
                for (int i = 0; i < tab.length; ++i) {
                    for (Node<K,V> e = tab[i]; e != null; e = e.next)
                        action.accept(e.value);
                }
                if (modCount != mc)
                    throw new ConcurrentModificationException();
            }
        }
    }
    

    【讨论】:

    • 尽可能避免回答此类重复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-19
    相关资源
    最近更新 更多