java - HashMap get() 如何比较键?

标签 java collections hashmap

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 not null and it represents the same address as this object.

Two instances of InetSocketAddress represent the same address if both the InetAddresses (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/

相关文章:

java - 尝试将 hashmap getValue() 解析为 int 时出错

java - 遍历对象类型的列表

java - 使用 Netbeans 构建可执行的 .jar 文件

java - 类型无法创建 List<FooClass> 的通用数组

javascript - 主干集合的基本问题

python - python 集合组合方法中的惊人顺序

Java:如何在数组中再次重新解析 'Array.toString'?

java - 如何使用 Java Stream API 在抽象数据类型中的 List<String> 中搜索作为其他抽象数据类型的属性

java - 禁用按钮后刷新整数值

java - 将具有不同值的相同对象写入集合