HashMap.values()和HashMap.keySet()如何返回值和键? [英] How do HashMap.values() and HashMap.keySet() return values and keys?

查看:273
本文介绍了HashMap.values()和HashMap.keySet()如何返回值和键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HashMap.values()的源代码如下所示:

  public Collection< V> values(){
Collection< V> vs =值;
return(vs!= null?vs:(values = new Values())); (

正如你所看到的,当 values() 方法首先调用,它只返回一个对象。 Values 对象是没有构造函数的 AbstractCollection 的子类,当然不包含任何元素。但是当我调用这个方法时,它会很快返回一个集合

  Collection< String> values = map.values(); 
System.out.println(values);

这太奇怪了。不仅 values(),还有 keySet() entrySet()方法返回这样的空对象。所以,这里是我的问题,这些方法何时以及如何用我们需要的元素返回对象?解析方案

这是一种误解,认为 class是当然是空的。仅仅因为没有方法调用它并且其构造函数没有任何参数并不意味着该集合是空的。



class是一个内部类(一个非静态的嵌套类)的 HashMap ,这意味着它对创建它的 HashMap 对象具有隐式引用。因此,它可以通过使用 HashMap.this 引用或通过访问 HashMap 会员直接。由于它是一个内部类,它甚至可以访问 HashMap 的私有成员。



你可以例如在 Values 类的实现中看到 size 方法:

  public int size(){
return size;

类没有 size 成员,因此 size 引用 HashMap 的大小。它相当于:

  public int size(){
return HashMap.this.size;
}

编辑:注意这也意味着您收到的集合不是副本,但仍然引用原始 HashMap 内容,因此在更新 HashMap

  //获取条目集(不是副本!)
Set< Entry< String,String>> ; entries = map.entrySet();

//将元素添加到地图后
map.put(abc,def);

//检出集合
中的条目(奇迹般包含获得集合后添加的元素)
System.out.println(entries); //[abc = def]


The source code of HashMap.values() is shown as follows

public Collection<V> values() {
    Collection<V> vs = values;
    return (vs != null ? vs : (values = new Values()));
}

As you can see, when the values() method first called, it just returns a Values object. The Values object is a subclass of AbstractCollection with no constructor, and of course contains no element. But when I called the method, it returned a collection rapidly

Collection<String> values = map.values();
System.out.println(values);

That's so weird. Not only values(), but also keySet() and entrySet() method return such empty objects. So, here is my question, when and how do these methods return objects with elements we need?

解决方案

It's a misconception that the Values class is "of course empty". Just because there is no method called on it and its constructor doesn't have any arguments doesn't mean that the collection is empty.

The Values class is an "inner class" (a non-static nested class) of HashMap, which means that it has an implicit reference to the HashMap object that created it. It can therefore access all elements of the HashMap, either explicitly by using the HashMap.this reference or by just accessing the members directly. Since it is an inner class, it is even allowed to access the HashMap's private members.

You can see that for example in the Values class' implementation of the size method:

public int size() {
    return size;
}

The Values class doesn't have a size member, so that size refers to the HashMap's size. It's equivalent to:

public int size() {
    return HashMap.this.size;
}

EDIT: Note that this also means that the collection you receive is not a copy, but still refers to the original HashMap contents and therefore changes when you update the HashMap:

    // Getting the entry set (not a copy!)
    Set<Entry<String, String>> entries = map.entrySet();

    // Add elements to the map afterwards
    map.put("abc", "def");

    // Check out the entries in the collection
    // (magically containing the elements added after getting the collection)
    System.out.println(entries); // "[abc=def]"

这篇关于HashMap.values()和HashMap.keySet()如何返回值和键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆