如何在包含具有多个键/值对的 HashMap 的列表上使用 indexOf
我有一个包含 HashMaps
的 List
。列表中的每个HashMap
可能有多个键/值
对。我想在列表上 indexOf
找出传入 HashMap 的元素的索引。但是,问题是 e 在比较时会查看整个entrySet
。这不是我想要的。
示例:
List<HashMap> benefit = new ArrayList<HashMap>();
HashMap map1 = new HashMap();
map1.put("number", "1");
benefit.add(map1);
HashMap map2 = new HashMap();
map2.put("number", "2");
map2.put("somethingelse", "blahblah"); //1
benefit.add(map2);
HashMap find = new HashMap();
find.put("number", "2");
int index = benefit.indexOf(find);
if (index >= 0)
System.out.println(benefit.get(index).get("number"));
上面的代码由于带有 //1
行而没有打印任何内容。
- 我需要做什么才能使上面的代码实际打印
2
? - 有没有办法在列表上实现可比较,以便我可以定义 我自己的?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您正在寻找
retainAll()
,因此您可以仅比较您感兴趣的元素:当然,可以声明您自己的 List 子类,该子类重写了 indexOf 方法这种行为。但是,我认为这不是一个好主意。它会违反 indexOf 方法的约定:
这会让维护代码的其他人感到困惑。然后您可能会认为可以子类化 HashMap 来重新定义 equals,但这会违反 Object.equals() 的对称属性。
I think you're looking for
retainAll()
, so you can compare only the elements you're interested in:It's possible, of course, to declare your own subclass of List that overrides the indexOf method with this behaviour. However, I don't think that's a good idea. It would violate the contract of the indexOf method:
This would be confusing to someone else maintaining the code. You might then think that you could subclass HashMap to redefine equals, but that would violate the symmetry property of Object.equals().
你试图实现目标的方式是错误的。
indexOf
方法的工作方式与本例中应有的方式完全相同。它试图找到完全匹配,而不是部分匹配。如果我理解正确的话,您要做的就是在地图列表中找到包含特定条目的地图。在这种情况下,您应该手动执行此搜索,方法是遍历所有映射,调用
containsKey ()
,然后将您期望找到的值与与该键关联的值进行比较。另一种方法是围绕您的
List
创建一个代理类,并添加一个新方法findMapWithEntry (String key, String value)
,它将为您执行此搜索(与我上面描述的搜索相同)。The way you are trying to achieve your goal is wrong. The
indexOf
method works exactly as it should in this case. It is trying to find an exact match, not a partial one.What you are trying to do, if I get it correctly, is to find a map in your list of maps that contains a specific entry. In this case, you should manually perform this search, by going through all the maps, calling
containsKey ()
, and then comparing the value you are expecting to find with the value associated with the key.The other way would be to create a proxy class around your
List
, and add a new methodfindMapWithEntry (String key, String value)
, which would perform this seach for you (the same search I described above).为什么不改变你的搜索方式呢?
Why not change the way you search?
您始终可以重写indexOf方法。查看 ArrayList 的源代码:
所以它根本不是一个非常复杂的搜索算法。您可能会看到这样的内容:
我的建议是考虑一种不同的数据结构,也许可以为其编写自己的数据结构。
You can always override the indexOf method. Looking at the source for ArrayList:
So it's not a very complex search algorithm at all. You may look at something like:
My advice would be to consider a different data structure, perhaps writing your own one for it.
鉴于您无法更改设计,编写自己的 find 方法会有帮助吗?
如果我理解你想要做什么并且它在 O(n) 中运行,下面的代码应该可以工作
Given that you cannot change the design, would writing your own find method help?
The code below should work if I understood what you're trying to do and it runs in O(n)