0
這裏我們可以看到Map.Entry的toString()方法在運行。 由於Map.Entry是一個接口,定義在哪裏。由於Map.entry是一個接口,Map.Entry的toString()方法實現在哪裏定義?
import java.util.*;
import java.lang.*;
import java.io.*;
class Dog
{
public int i;
public int hashCode()
{
return i%3;
}
Dog(int i)
{
this.i = i;
}
public String toString()
{
return i + "" ;
}
}
class ShellClass
{
public static void main (String[] args) throws java.lang.Exception
{
HashMap m = new HashMap(5,(float)0.8);
for(int i=1; i<=4; i++)
{
m.put((new Dog(i)),"dog");
}
System.out.println(m); // line1
Set entrySet = m.entrySet();
System.out.println(entrySet); // line2
Iterator itr = entrySet.iterator();
while(itr.hasNext())
{
Map.Entry element = (Map.Entry)itr.next();
System.out.println(element); // line3
}
}
}
我得到以下輸出:
{3=dog, 1=dog, 4=dog, 2=dog} //output by line 1
[3=dog, 1=dog, 4=dog, 2=dog] //output by line 2 Map.Entry toString in action
3=dog // output by line 3 (Map.Entry toString in action)
1=dog // output by line 3 (Map.Entry toString in action)
4=dog // output by line 3 (Map.Entry toString in action)
2=dog // output by line 3 (Map.Entry toString in action)
能否請你分不清哪裏是Map.Entry的toString()方法來實現
感謝您的回答pnkfelix – shubh