Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 是"的情况,;key==这个;可能的_Java_Hashtable - Fatal编程技术网

Java 是"的情况,;key==这个;可能的

Java 是"的情况,;key==这个;可能的,java,hashtable,Java,Hashtable,我正在读哈希表的代码。我对toString()方法感到困惑,代码如下: public synchronized String toString() { int max = size() - 1; if (max == -1) return "{}"; StringBuilder sb = new StringBuilder(); Iterator<Map.Entry<K,V>> it = entrySet().iterat

我正在读哈希表的代码。我对
toString()
方法感到困惑,代码如下:

public synchronized String toString() 
{
    int max = size() - 1;
    if (max == -1)
       return "{}";

    StringBuilder sb = new StringBuilder();
    Iterator<Map.Entry<K,V>> it = entrySet().iterator();
    sb.append('{');

    for (int i = 0; ; i++)
    {
        Map.Entry<K,V> e = it.next();
        K key = e.getKey();
        V value = e.getValue();

        // Is "key == this" possible ? What the "this" stands for ?
        sb.append(key   == this ? "(this Map)" : key.toString());
        sb.append('=');
        sb.append(value == this ? "(this Map)" : value.toString());

        if (i == max)
            return sb.append('}').toString();

        sb.append(", ");
    }
}
公共同步字符串toString()
{
int max=size()-1;
如果(最大值==-1)
返回“{}”;
StringBuilder sb=新的StringBuilder();
迭代器it=entrySet().Iterator();
某人附加('{');
对于(int i=0;i++)
{
Map.Entry e=it.next();
K key=e.getKey();
V value=e.getValue();
//“key==this”可能吗?this代表什么?
sb.append(key==this?”(thismap)”:key.toString();
某人附加('=');
sb.append(value==this?”(thismap)”:value.toString();
如果(i==最大值)
返回sb.append('}').toString();
某人加上(“,”);
}
}

所以,如果代码没有检查“key=this”与否,那么toString()方法可能是无止境循环

    // Is "key == this" possible ? What the "this" stands for ?
“this关键字”指的是对象的当前实例。“key==this”检查key是否引用对象的当前insatnce


“this关键字”指的是对象的当前实例。“key==this”检查key是否指对象的当前状态。

当然可能:

Hashtable table = new Hashtable();
table.put(table, table);
System.out.println("table = " + table);
产出:

table = {(this Map)=(this Map)}
table = {(this Map)=(this Map)}
table = {abc=def, (this Map)=(this Map)}
table = {abc=def, (this Map)=(this Map)}
但是请注意,这样一个映射的行为可能会令人惊讶(因为它的hashcode和equals将发生变化)。例如,在下面的示例中,添加另一个条目后,无法从地图本身删除地图:

Hashtable table = new Hashtable();
table.put(table, table);
System.out.println("table = " + table);
table.put("abc", "def");
System.out.println("table = " + table);
table.remove(table); //does not work as expected !!!
System.out.println("table = " + table);
产出:

table = {(this Map)=(this Map)}
table = {(this Map)=(this Map)}
table = {abc=def, (this Map)=(this Map)}
table = {abc=def, (this Map)=(this Map)}

当然有可能:

Hashtable table = new Hashtable();
table.put(table, table);
System.out.println("table = " + table);
产出:

table = {(this Map)=(this Map)}
table = {(this Map)=(this Map)}
table = {abc=def, (this Map)=(this Map)}
table = {abc=def, (this Map)=(this Map)}
但是请注意,这样一个映射的行为可能会令人惊讶(因为它的hashcode和equals将发生变化)。例如,在下面的示例中,添加另一个条目后,无法从地图本身删除地图:

Hashtable table = new Hashtable();
table.put(table, table);
System.out.println("table = " + table);
table.put("abc", "def");
System.out.println("table = " + table);
table.remove(table); //does not work as expected !!!
System.out.println("table = " + table);
产出:

table = {(this Map)=(this Map)}
table = {(this Map)=(this Map)}
table = {abc=def, (this Map)=(this Map)}
table = {abc=def, (this Map)=(this Map)}

这样,如果将
哈希表
放入自身,就不会得到无限循环。考虑:

final Map map = new Hashtable();
map.put(map, "test");

这样,如果将
哈希表
放入自身,就不会得到无限循环。考虑:

final Map map = new Hashtable();
map.put(map, "test");
是的。这个

HashTable<Object, Object> maptest = new HashTable<Object, Object>();
mapTest.put(mapTest, 1);
HashTable maptest=newhashtable();
mapTest.put(mapTest,1);
将具有
key==此
返回
true

是。这个

HashTable<Object, Object> maptest = new HashTable<Object, Object>();
mapTest.put(mapTest, 1);
HashTable maptest=newhashtable();
mapTest.put(mapTest,1);

将具有
key==this
返回
true

有可能将
map
作为键保留在同一
map

HashMap<Object, Object> selfmap = new HashMap<Object, Object>();
selfmap.put(selfmap, selfmap);
HashMap selfmap=newhashmap();
selfmap.put(selfmap,selfmap);

有可能将
地图
作为键保留在同一
地图

HashMap<Object, Object> selfmap = new HashMap<Object, Object>();
selfmap.put(selfmap, selfmap);
HashMap selfmap=newhashmap();
selfmap.put(selfmap,selfmap);

如果
这个
对象(如您所说的哈希表)引用等于,那么条件
键==这个
真的
如果
这个
对象(如您所说的哈希表)引用等于,那么条件
键==这个
真的

所以,如果代码没有检查“key等于this”,那么toString()方法可能是无止境循环?因此,如果代码没有检查“key等于this”,那么toString()可能是方法可以是无限循环?当您尝试调用hashCode或toString时,这可能会导致无限递归。当您尝试调用hashCode或toString时,这可能会导致无限递归。