首页 > 解决方案 > 如何从 java 中 POJO 中的 ArrayList 获取特定值

问题描述

Key : 22 Value: Abey
Key : 22 Value : Dawn
Key : 22 Value : Sherry
Key : 22 Value : Sherry
Key : 22 Value : Sherry

我只想打印带有雪利酒的钥匙,如何使用 Java 来打印?

更改问题的标题,因为 HashMap 不存储重复的键

标签: javaarraysarraylistpojo

解决方案


HashMap 不接受重复的键。仅重复值。在您的方法中,您无法存储所显示的所有记录。例如:

Map<Integer, String> map = new HashMap<>();
map.put(22, "Abey");
map.put(22, "Dawn");
map.put(22, "Sherry");
map.put(22, "Sherry");
map.put(22, "Sherry");

当您打印 HashMap 的输出时。您只会得到一个输出,因为相同的键将替换为最新值。

输出:

{22=Sherry}

如果您真的想存储重复键,请了解有关如何在 HashMap 中存储重复键的更多信息。


推荐阅读