java中的Map.Entry接口
据我所知,java.util.Map.Entry是java.util包中的公共静态接口 返回地图的集合视图,但到目前为止我对静态接口感到困惑 因为它是 Map.Entry 它是一个内部接口如果是的话我们如何在java中拥有内部静态接口
java.util.Map.Entry
as I know is a public static interface
in java.util
package that
returns collection view of a map but as far now I am confused with the static interface
and as it is Map.Entry is it an inner interface if so how do we have inner static interfaces in java
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Entry
的定义恰好位于Map
的定义内(java 允许)。静态
意味着您不需要Map
实例来引用Entry
。通过示例来展示如何使用 Map.Entry 是最简单的。以下是迭代地图的方法
The definition of
Entry
happens to live inside the definition ofMap
(allowed by java). Beingstatic
means you don't need an instance ofMap
to refer to anEntry
.It's easiest to show how to use
Map.Entry
by an example. Here's how you can iterate over a map确实没有什么可困惑的。
是的,Java 允许接口成为类或其他接口的成员。
不,这并不意味着什么特别的。它绝对不会改变您如何使用这样的界面或您可以用它做什么。
它仅更改该接口的名称,并在其与其封闭类型之间创建强大的概念链接。在这种情况下,
Map.Entry
表示Map
的一个条目。 API 的设计者显然认为通过将其设为成员类型来强调这种联系是有意义的。There isn't really anything to be confused about.
Yes, Java allows interfaces to be members of classes or other interfaces.
No, that does not mean anything special. It changes absolutely nothing about how you can use such an interface or what you can do with it.
It only changes the name of that interface and creates a strong conceptual link between it and its enclosing type. In this case, a
Map.Entry
represents an entry of aMap
. The designers of the API apparently felt that it made sense to stress this connection by making it a member type.示例:
Bar 是一个嵌套接口。嵌套接口默认是静态的,所以你也可以这样写:
现在,这里的静态意味着接口是静态成员,即类的成员。
您也可以使用类来执行此操作:
在这里,Node 甚至是私有的,这意味着它仅在 Tree 中可见。那么,这样做有什么好处呢?为什么不让 Node 成为公共类呢?因为封装比较好。首先,节点是树的实现细节,因此您不希望它可见。其次,如果您通过公共 API 公开 Node,某些客户端(程序员)可以在他的代码中使用它。现在,他对这个班级已经产生了严重的依赖。如果在某个时候您想要更改树的表示形式,并且更改/删除 Node 类,则客户端代码可能会中断。最后但并非最不重要的一点是,您的公共 API 变得更小,这也是可取的。
那么,什么时候使用静态成员类/接口呢?大多数情况下,如果您构建某种复合对象(如树或链接列表),或者该类仅在外部类的上下文中有意义。
Example:
Bar is a nested interface. Nested interfaces are static by default, so you could as well write:
Now, what static in this context means is that the interface is a static member, i.e. a member of the class.
You can do this with classes as well:
Here, Node is even private, meaning it's only visible within Tree. So, what's the benefit of this? Why not make Node a public class? Because of better encapsulation. First, the Node is an implementation detail of the Tree, so you don't want it to be visible. Second, if you expose Node via a public API, some client (programmer) could use it in his code. Now, he has a hard dependency on this class. If at some point you want to change the representation of you Tree, and you change/remove the Node class, the client code's may break. And last but not least, your public API becomes smaller, which is also desirable.
So, when to use static member classes/interfaces? Mostly, if you build some sort of Composite object (like a Tree, or a Linked List) or when the class only makes sense in the context of the outer class.
Java 允许嵌套接口。您可以将它们嵌套到类或接口中。例如,
Map.Entry
是在Map
接口中定义的嵌套接口。Map
实现(TreeMap
、HashMap
)提供Map.Entry
的私有实现,这些实现在类外部不可见。Bohemian 的答案解决了如何使用
Map.Entry.
Java allows nested interfaces. You can nest them into classes or interfaces. For instance,
Map.Entry
is a nested interface defined in theMap
interface.Map
implementations (TreeMap
,HashMap
) provide private implementations ofMap.Entry
, which are not visible outside the class.Bohemian's answer addresses how to use
Map.Entry
.是的,它是 < 的内部接口代码>地图界面。
有关接口的更多信息,请参阅接口教程和此< href="http://littletutorials.com/2008/03/06/static-nested-interfaces/" rel="nofollow">静态嵌套接口一文。
Yes, it's an inner interface of the
Map
interface.For more information about interfaces, see the Interfaces tutorial and this Static Nested Interfaces article.
内部接口是隐式公共和静态的。
你可以有内部接口,如下所示:
你可以通过AB访问上面的内部接口(B),其中A是一个类或一个接口,根据上述两种情况。
例如,
Inner interfaces are implicitly public and static.
You can have inner interfaces as follows :
You can access the above inner interface(B) by A.B where A is a class or an interface according to the above two cases.
For example,