tos.entry是一个接口,在其中定义了map.entry toString()方法实现



在这里我们可以看到map.entry tostring()方法。从map.entry是接口。

,在哪里定义。
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()方法

在我对Java标准库的参考实现中,HashMap中有一个静态内部类,隐含了Map.Entry接口。

,内在类是更好或更糟糕的"也","也"名为Entry

您可以在这里看到它。它的toString实现如下:

     public final String toString() {
         return getKey() + "=" + getValue();
     }    

最新更新