HashMap get() 的作用机制是什么?
如果我有一个 InetSocketAddress 作为我的 key ,查找是否会返回一个值,该值的 key 与 get() 参数具有相同的 InetAddress 和相同的端口?
最佳答案
Map#get()
方法比较两个对象是否 equals()
。 In the case of an InetSocketAddress
,
Compares this object against the specified object. The result is
true
if and only if the argument is notnull
and it represents the same address as this object.Two instances of
InetSocketAddress
represent the same address if both theInetAddress
es (or hostnames if it is unresolved) and port numbers are equal. If both addresses are unresolved, then the hostname and the port number are compared. Note: Hostnames are case insensitive. e.g. "FooBar" and "foobar" are considered equal.
因此,您应该能够拥有 Map<InetSocketAddress,Foo>
查找有关特定连接的信息。
关于java - HashMap get() 如何比较键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33966204/